Passing data structures to different threads

Posted by Robb on Stack Overflow See other posts from Stack Overflow or by Robb
Published on 2010-05-07T15:48:59Z Indexed on 2010/05/07 15:58 UTC
Read the original article Hit count: 306

Filed under:
|
|
|

I have an application that will be spawning multiple threads. However, I feel there might be an issue with threads accessing data that they shouldn't be. I'm relatively new to threading so bare with me.

Here is the structure of the threaded application (sorry for the crudeness):

                   MainThread
                  /          \
                 /            \
                /              \
        Thread A               Thread B
       /        \              /       \
      /          \            /         \
     /            \          /           \
Thread A_1     Thread A_2   Thread B_1    Thread B_2

Under each lettered thread (which could be many), there will only be two threads and they are fired of sequentially. The issue i'm having is I'm not entirely sure how to pass in a datastructure into these threads.

So, the datastructure is created in MainThread, will be modified in the lettered thread (Thread A, etc) specific to that thread and then a member variable from that datastructure is sent to Letter_Numbered threads.

Currently, the lettered thread class has a member variable and when the class is constructed, the datastructure from mainthread is passed in by reference, invoking the copy constructor so the lettered thread has it's own copy to play with.

The lettered_numbered thread simply takes in a string variable from the data structure within the lettered thread. My question is, is this accceptable? Is there a much better way to ensure each lettered thread gets its own data structure to play with?

Sorry for the somewhat poor explanation, please leave comments and i'll try to clarify.

© Stack Overflow or respective owner

Related posts about c++

Related posts about design-patterns