Managing Multiple Threads

I am using SFMLs "System.hpp" file and "windows.h"+"WinBase.h" and no namespaces everything else is from the standard C++ library

Hi Everyone,

Let me start by saying that my program works how I intended it to. However I have a For Loop with an imbedded If test on the main thread to launch my child threads and keep the number of them under control, if left unchecked this section of code will maxout any CPU time that it is given. Right now I am using the SFML Sleep() command to power it down if the maximum number of threads I defined is being executed and that technically works but I was wondering if there was a "better" C++ way to do this. Here are some of my other ideas and I wanted to run them by you guys to see if anyone was better then another or to get a better idea from anyone of you:

- I thought about using SFML to synthesize a Keystroke in the threads destructor (I have them made up as objects) to launch the next thread. I would build in a check to see if the program is to be finished.

- I thought about using the WndProc() CALLBACK but this to me would require the same checks that I'm already using right?

These were my two main ideas but I can't recall anyothers right now. I may edit this with more later.

Also this is technically a network App, and I was wondering if any of you knew off the top of your heads how to reduce the timeout if a host cannot be reached?

EDIT: By the way I'm only using WinBase.h in order to interface on the host computer across the network, this is NOT a Windows App.
Last edited on
Does it always need to be doing something, or does it react to some event (like network or user input)?

If so, you can take input asynchronously and wait for the input to arrive, rather than poll.
That sounds like a good way to go about it. Is there a link you could pass me with more info?
You could have a thread that listens for the network, blocked on select(). When something comes it, does the read and notifies you or notifies you and you do the read.

You could also have a similar arrangement for the keyboard input.

The notification would be signalling an event. Then the main thread just does a WaitForMultipleObjects with some timeout value, so it knows about an event occuring and acts as sleep(). The events must be auto-reset, not manual or you'll have race conditions.
Last edited on
Hmm I was using the SFML multi threading class but if Microsofts method has more flexability I'll have to give that another go. Thanks kbw!
I've not used SFML, so it's a Windows-centric solution. I find the syncronisation in Windows very powerful and flexible.
Actually Windows centric is fine, since you made your suggestion I took another look at the MultiThreading that Windows provides and it isn't too much more complicated. I had to adjust my code so that each object had its own thread but it's working so far. I think I'll eliminate SFML from the program and stick with the Windows XP-7 SDK.

I'm working on the syncronisation now but WaitForMultipleObjects(...) is kind of whacky. I may start a new post asking about that one. Thanks for your help!
Topic archived. No new replies allowed.