Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data structures to different threads

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.

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.

EDIT: So my lettered thread constructor should take the VALUE of the data structure, not the reference?

like image 445
Robb Avatar asked May 07 '10 15:05

Robb


People also ask

How do I share data between different threads?

All static and controlled data is shared between threads. All other data can also be shared through arguments/parameters and through based references, as long as the data is allocated and is not freed until all of the threads have finished using the data.

How do you pass data between threads in Java?

If you want synchronous communication between a main thread and a processing thread, you can use a SynchronousQueue. The idea is that the main thread passes data to the processing thread by calling put() , and the processing thread calls take() .

Can multiple threads access the same variable?

Only one thread can read and write a shared variable at a time. When one thread is accessing a shared variable, other threads should wait until the first thread is done. This guarantees that the access to a shared variable is Atomic, and multiple threads do not interfere.

Can multiple threads run the same function?

A thread can execute a function in parallel with other threads. Each thread shares the same code, data, and files while they have their own stack and registers.


1 Answers

I would have each thread create it's own copy of the datastructure, e.g. you pass the structure in the constructor and then explicitly create a local copy. Then you are guaranteed that the threads have distinct copies. (You say that it's passsed by reference, and that this invokes the copy constructor. I think you mean pass by value? I feel it's better to explicitly make a copy, to leave no doubt and to make your intent clear. Otherwise someone might later come along and change your pass by value to pass by reference as a "smart optimization".)

EDIT: Removed comment about strings. For some reason, I was assuming .NET.

To ensure strings are privately owned, follow the same procedure, create a copy of the string, which you can then freely modify.

like image 112
mdma Avatar answered Sep 20 '22 22:09

mdma