Archived

This topic is now archived and is closed to further replies.

Spider_In_A_Web

MultiThreaded Variables

Recommended Posts

Hello All, Iam experimenting with my first large multithreaded application and have hit my first problem I want to share a single instance of a class between two threads, but so far i keep getting multiple copies of the same class. So when i initialise my class in the main thread all is fine, but when i try and update it from my dedicated thread it seems to have an entirely new version of the class which is unitialised!! Any ideas, currently i have a static copy of the class held in a header file called globals ie static CKeys; Thanks!!

Share this post


Link to post
Share on other sites
MonkeyChuff    158
I think you already on the right track. What you describe is the Singleton pattern, which uses a static to ensure that a single object is created.

Check my profile ... I answered a previous post about singletons with example code.

Cheers

Share this post


Link to post
Share on other sites
Oluseyi    2109
quote:
Original post by Spider_In_A_Web
...currently i have a static copy of the class held in a header file called globals

And you include the globals header file in how many source files?

As Mayrel responded to your other post on the ''static'' keyword, static outside a class declaration declares an object with private file visibility, and you should use extern if this is your problem.

Now, as for updating a single object from separate threads, I hope you''re up to speed on mutexes. You need to ensure that both threads do not try to access (and especially change) the variable at once, as that will lead to unpredictable results. A mutex is an application defined handle that can be "owned" temporarily by different threads. Every operating system provides mutex functionality and wait() functions to send a thread to sleep until it obtains ownership of a specific mutex (meaning it can freely write to the shared variable).

For Win32, look up CreateMutex(), WaitForSingleObject() and WaitForMultipleObjects().



I wanna work for Microsoft!

Share this post


Link to post
Share on other sites
Dire.Wolf    122
Use a critical section as they are faster overall. Use a mutex if you need inter-process synchronization of a variable/class. There are a couple design patterns that you can use to synchronize access to a class in an OO-manner. If all you need to do is modify a variables value use InterlockedExchangeAdd. It is a user-mode operation and is exceptionally fast. It only allows the variable in question (memory address) to be accessed by one thread at a time.



Dire Wolf
www.digitalfiends.com

Share this post


Link to post
Share on other sites
Shannon Barber    1681
If you''re threading, try to eliminate global variables - global data can prevent your functions and method from being properly reentrant, and lead to problems later on. As mentioned, the singleton pattern can solve this problem.

Magmai Kai Holmlor
- Not For Rent

Share this post


Link to post
Share on other sites
CruxMihiAncora    122
You say you understand multithreading so I don''t think this would be the issue (also I am a beginner so I may be far off here) but did you pass the class in as a parameter to CreateThread()?

Share this post


Link to post
Share on other sites
Oluseyi    2109
quote:
Original post by CruxMihiAncora
You say you understand multithreading so I don''t think this would be the issue (also I am a beginner so I may be far off here) but did you pass the class in as a parameter to CreateThread()?

You meant object. You cast the pointer to the object to a void pointer in CreateThread(), then cast it back to a pointer to an instance of the class in your thread proc. That, however, does not allevaite or obviate the exclusion issues. You''ll still need mutexes to determine when each thread can access the object.

You''re on the right track, too, though.




I wanna work for Microsoft!

Share this post


Link to post
Share on other sites
I did not pass it as a parameter, but there are reasons for this

The thread i was having trouble with was the first in the program, and it updates the keyboard device from DirectInput so i wan it global, i also decided to pass the update frequency as a parameter instead!!

I am using a critical section to update the device, then sleeping for a set period of time then starting again, this way i can now effectively forget about updating the keyboard ever!!

I also plan to run my main game control program in a completely seperate thread to allow me to forget about the windows message pump!

Share this post


Link to post
Share on other sites
Just wondering, does anyone know any good resources for multithreaded game organisation or have any words of advice

ie when and why to create a new thread to do what??

Does anyone use a thread to control rendering, and how does that work, is it simply a thread set with a perminat loop that constantly renders in a loop??

This kind of thing!!

Thanks all

Share this post


Link to post
Share on other sites
Shannon Barber    1681
I use one thread for the message pump and one thread for the game loop. If you do a large amount of IO, reading a huge wad off disk or networking, a worker thread can help with performance there. DirectPlay is free-threaded and uses a thread pool to process messages, which really is overkill on the client but good for a server.

If you run a thread flat-out and never sleep it, reduce it''s priority or you''ll starve the other threads in the system.

Magmai Kai Holmlor
- Not For Rent

Share this post


Link to post
Share on other sites