Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static variables in multithreading

Status
Not open for further replies.

wallaceoc80

Programmer
Jul 7, 2004
182
GB
Hi, I have a question regarding the use of static variables in a multithreading application. I have an application that will be parsing 2 different XML files on different threads. I then want to have a class (say Object1) that has a map containing other objects (Object2).

Items of Object2 can be created by both threads so my question is whether they can both be added to the same map or does the multithreading mean that the map is not accessible accross threads?

I hope I have explained this clearly enough and any help would be appreciated.

Regards,
Wallace
 
Multi-threading is irrelevant to variable scope. It's not a thread can (or cant) see the variable. So you may access a map from all threads (as global or via reference etc) - but be careful: a map (usually it's some kind of a tree structure) is not thread-safe data structure.

You must protect map insert operations from concurrent threads. Use mutexes or critical sections or semaphores (Windows synchronization primitives). Probably, packed into the special insert function critical section synchronization is the best choice in that case.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top