Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6352575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:13:56+00:00 2026-05-24T22:13:56+00:00

I want to pass some data around threads but want to refrain from using

  • 0

I want to pass some data around threads but want to refrain from using global variables if I can manage it. The way I wrote my thread routine has the user passing in a separate function for each “phase” of a thread’s life cycle: For instance this would be a typical usage of spawning a thread:

void init_thread(void *arg) {
  graphics_init(); 
}
void process_msg_thread(message *msg, void *arg) {
  if (msg->ID == MESSAGE_DRAW) {
    graphics_draw();
  }
}
void cleanup_thread(void *arg) {
  graphics_cleanup();
}

int main () {
  threadCreator factory;
  factory.createThread(init_thread, 0, process_msg_thread, 0, cleanup_thread, 0);
  // even indexed arguments are the args to be passed into their respective functions
  // this is why each of those functions must have a fixed function signature is so they can be passed in this way to the factory
}

// Behind the scenes: in the newly spawned thread, the first argument given to 
// createThread() is called, then a message pumping loop which will call the third 
// argument is entered. Upon receiving a special exit message via another function 
// of threadCreator, the fifth argument is called.

The most straightforward way to do it is using globals. I’d like to avoid doing that though because it is bad programming practice because it generates clutter.

A certain problem arises when I try to refine my example slightly:

void init_thread(void *arg) {
  GLuint tex_handle[50]; // suppose I've got 50 textures to deal with.
  graphics_init(&tex_handle); // fill up the array with them during graphics init which loads my textures
}
void process_msg_thread(message *msg, void *arg) {
  if (msg->ID == MESSAGE_DRAW) { // this message indicates which texture my thread was told to draw
    graphics_draw_this_texture(tex_handle[msg->texturehandleindex]); // send back the handle so it knows what to draw
  }
}
void cleanup_thread(void *arg) {
  graphics_cleanup();
}

I am greatly simplifying the interaction with the graphics system here but you get the point. In this example code tex_handle is an automatic variable, and all its values are lost when init_thread completes, so will not be available when process_msg_thread needs to reference it.

I can fix this by using globals but that means I can’t have (for instance) two of these threads simultaneously since they would trample on each other’s texture handle list since they use the same one.

I can use thread-local globals but is that a good idea?

I came up with one last idea. I can allocate storage on the heap in my parent thread, and send a pointer to in to the children to mess with. So I can just free it when parent thread leaves away since I intend for it to clean up its children threads before it exits anyway. So, something like this:

void init_thread(void *arg) {
  GLuint *tex_handle = (GLuint*)arg; // my storage space passed as arg
  graphics_init(tex_handle); 
}
void process_msg_thread(message *msg, void *arg) {
  GLuint *tex_handle = (GLuint*)arg; // same thing here
  if (msg->ID == MESSAGE_DRAW) { 
    graphics_draw_this_texture(tex_handle[msg->texturehandleindex]); 
  }
}
int main () {
  threadCreator factory;
  GLuint *tex_handle = new GLuint[50]; 
  factory.createThread(init_thread, tex_handle, process_msg_thread, tex_handle, cleanup_thread, 0);
  // do stuff, wait etc
  ...
  delete[] tex_handle;
}

This looks more or less safe because my values go on the heap, my main thread allocates it then lets children mess with it as they wish. The children can use the storage freely since the pointer was given to all the functions that need access.

So this got me thinking why not just have it be an automatic variable:

int main () {
  threadCreator factory;
  GLuint tex_handle[50]; 
  factory.createThread(init_thread, &tex_handle, process_msg_thread, &tex_handle, cleanup_thread, 0);
  // do stuff, wait etc
  ...
} // tex_handle automatically cleaned up at this point

This means children thread directly access parent’s stack. I wonder if this is kosher.
I found this on the internets: http://software.intel.com/sites/products/documentation/hpc/inspectorxe/en-us/win/ug_docs/olh/common/Problem_Type__Potential_Privacy_Infringement.htm

it seems Intel Inspector XE detects this behavior. So maybe I shouldn’t do it? Is it just simply a warning of potential privacy infringement as suggested by the the URL or are there other potential issues that may arise that I am not aware of?

P.S. After thinking through all this I realize that maybe this architecture of splitting a thread into a bunch of functions that get called independently wasn’t such a great idea. My intention was to remove the complexity of requiring coding up a message handling loop for each thread that gets spawned. I had anticipated possible problems, and if I had a generalized thread implementation that always checked for messages (like my custom one that specifies the thread is to be terminated) then I could guarantee that some future user could not accidentally forget to check for that condition in each and every message loop of theirs.

The problem with my solution to that is that those individual functions are now separate and cannot communicate with each other. They may do so only via globals and thread local globals. I guess thread local globals may be my best option.

P.P.S. This got me thinking about RAII and how the concept of the thread at least as I have ended up representing it has a certain similarity with that of a resource. Maybe I could build an object that represents a thread more naturally than traditional ways… somehow. I think I will go sleep on it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:14:00+00:00Added an answer on May 24, 2026 at 10:14 pm

    Put your thread functions into a class. Then they can communicate using instance variables. This requires your thread factory to be changed, but is the cleanest way to solve your problem.

    Your idea of using automatic variables will work too as long as you can guarantee that the function whose stack frame contains the data will never return before your child threads exit. This is not really easy to achieve, even after main() returns child threads can still run.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to call an ASHX file and pass some query string variables from
I am writing some XML data to the stringwriter. I want to pass the
My aim is to retrieve some data from a global array which is defined
My aim is to retrieve some data from a global array which is defined
I want to pass some parameters to my MVC UserControl like ShowTitle(bool) and the
I want to pass an enum value as command parameter in WPF, using something
I have a PHP server script that SELECTs some data from a MySQL database.
I want to pass an integer value to a form in .Net so that
I want to pass an int list (List) as a declarative property to a
I want to pass in the tType of a class to a function, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.