Skip to main content
gamedev.net
🔒 Locked

C++: Inter Thread Communication

Started by kinglink Jan 19, 2005 at 1:01 PM 2 replies 2.5k views
Original Post
kinglink
kinglink
My question is thus. I've done some thread programming (I find it fun) And need to once again pull on this resource. However this time (for a game I'm programming currently technically) I'm looking for a way for the threads to communicate. There appears to be a lot of ways but I don't see one that is easily explained in my search and haven't done too many deep probes on any one topic because none seem perfect for my problem. My problem is this. I'm working on a program where there's the graphics thread, Network thread, and Logic thread (as well as an engine overseer that creates and controls the threads if it matters, though it's the main program) I then Have these situations. Logic needs input from user on which move to make, Graphics (the input and graphics thread) needs to know to show screen "B" allow the user to select option X and then return to logic which option was selected. (in this case select a card from a hand) I would also like network to be able to tell graphics what moves Player B is making. (Assuming B is not local) Is there anyway? I would prefer a compact way to do this as Graphics and logic tend to need to communicate almost every second (as any time a person makes a move the graphics has to recieve the move, tell logic what move it is and then what logic needs to tell graphics "I need this data") Is there a good way to communicate between the threads in this situation? I've pretty much no experience in most of the ways suggested from what I've seen.
Zipster
Zipster
One way is to give each thread a message queue, and whenever a thread adds a message or the parent thread retrieves its messages, you lock the queue to prevent synchronization issues.
kinglink
kinglink
I think I get your idea, let me just make sure.

First you have a Message Queue for each thread and then when Graphics needs to pass a message to networking (let's say it wants to send a chat message) The Graphics thread will push a message onto the Network message queue with a special number, the network thread during it's run will check the Network Message Queue and see something like (2, "Hello Team") which will be interpreted as "send 'Hello Team' to everyone"? Or is there already these classes made and such?

Either way it's a great idea, thanks.
Fruny
Fruny
That's pretty much it, though you can also pass function pointers, objects, etc...

A thread-safe queue class is a common component of any threading framework. The C++ standard library doesn't have one, as C++ itself does not support multithreading. It does, however, have std::queue and std::priority_queue classes you can build on.

Remember the volatile keyword.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.