I have a problem I don't know how to fix.
I need to create a few threads in a loop sending different arguments in there. Here is a simple version of my code:
I have 3 entries, so BounceProc is supposed to be called 3 times each time with different arguments. But in reality it receives the last entry all 3 times.I understand it happens because the loop executes too fast, it worked when I put Sleep() after CreateThread(). Is there any other way to solve this problem? I tryed everything I could think of, nothing else worked.
Don't use the same arglist for all calls.
You may use an array of arglists, like this :
CreateThread(NULL, 0, BounceProc, (void*)&arglist [ i ], 0, NULL);
This will ensure your parameters won't be overwritten before they're read.
Il n'est pas de langage de programmation dans lequel on ne puisse écrire de mauvais programmes.
(there are no programming languages in which one cannot make bad programs)
I'm afraid this requires allocating not a single arglist but an array of arglists on a heap - if arglist array is automatic it may go out of scope before the threads process their startup parameters.
Bookmarks