It seems to work but doesn't shows the graphic. I have tried to put a MessageBox before WaitForSingleObject and for as long as I don't press the ok button I see that the links and nodes are coloring. pressing ok the graphic part stops.
What am I doing wrong?
Do you have any advices or suggestion?
This is a very expensive way of making a function call. You are practically just making a call to StaticTx(0, (void*) pParam) since your function won't return until the StaticTx function has returned. This is definately incorrect. You should never block the main GUI thread as it is needed to handle window messages to the application.
I'm not sure why you need a thread for each node, and I hope that you are drawing only in response to WM_PAINT or have a good reason for not doing so.
I have made a thread for each Cluster Head [CH] node because they have to make the same action at the same time. The Tx(int nNodeIndex) function is used by all CH node to activate, one at the time, the node that has to trasmit (coloring the link, redrawing all the nodes on the IDC_PLOT control). The same thing is done by all the CH at the same time managing all the nodes each CH has under its control.
Everythig seems to work fine for one round but if I increase it I have to "tell" the function to wait all the thread of the StartSteadyState(void) to end before restarting everything with the ElectCluster(void).
Any advice on how to solve it? Or some ideas on other ways to do the same thing?
It's really hard to say without really knowing the specifics of the problem. But the thing is that even with threads, you aren't really doing the same action at the same time. How much processing is each cluster head doing? For each step? How many cluster heads? Actually if processing is low for each step I would be just using a WM_TIMER to update my state at discrete points in time and Invalidate.
Here's an idea in sorta pseudocode. Frankly I find threads with message loops to be much more flexible but this idea may or may not be suitable. I trust you can take care of any synchronisation issues with shared variables.
WaitForMultipleObjects( Start Event or Stop Event )
if Start Event
int nCurrentTime = 0;
foreach(clusterHead in ClusterArray)
clusterHead->PerformNextStep(nCurrentTime); // This updates the state of your system
// Your OnPaint handler will have to look at the state of your system and paint accordingly// probably accessing the state of the system that is shared by the main thread and your// worker thread
WaitForSingleObject(Stop Event, millisecsToNextInterval)
// If the stop event is signalled exit the thread, if it timeouts it means that you've hit// the next interval, this way you can control the speed of the simulation, I'm assuming // you actually want to allow the user time to see what is going on...if Stop Event
if Stop Event