Multithreading a class - Shared data?

Hi forum,

Once again I have a problem with my program.
I've have managed to run two threads at once using the _begintreadex. I use it when the user opens a lot of files for processing. I create a new thread for the function that process the files so the user still can use the program while it's processing the files.
My function is inside a class and I would like the new thread to have access to the class data from the main thread since I need some of the variables data from the class.
How do I make the thread share data with the first thread? I've searched a lot and can't seem to find the solution.

Regards,

Simon H.A.
can you provide your classes/functions?
You must synchronize the access to the shared variables. See http://msdn.microsoft.com/en-us/library/ms686364%28VS.85%29.aspx.

If, however, your question is more like "how do I do this in C++?" then there are a variety of possible answers. You would have to post some more information about how your classes are laid out.
Sorry for not having answered you. It has been over a month since I posted, so I haven't noticed the response.
But I'd find a way around to avoid the need of shared data.
Thanks for your responses anyways.

Regards,

Simon H.A.
I recommend that you read up more on the basics of threading.

The short answer is, your thread should already be able to see that data, if that data has not changed! This is where const (or some programming discipline) is useful/important. If that data could change, you will have to use a mutex to protect that data.

In fact, this is one of those tricks for mutex-free threading - initialize all the data that you need before you start any threads and ensure that data is read-only.

What I just posted pertains to pthreads, but it is so basic to threading that it should also apply elsewhere - read the manuals on your windows APIs to verify that it's the same.

One other suggestion: make sure you use a race-condition checking program like Valgrind on your data. You want to do this to ensure that your program is robust and that you don't end up in debugging hell due to threading.
Last edited on
Topic archived. No new replies allowed.