Re: possible typo in multithreading website
On Apr 12, 1:28 pm, Comp1...@yahoo.co.uk wrote:
I have read the above from a multithreading in c++ tutorial:
BEGINNING OF QUOTE
[...]
Listing 2. Still a race condition
void* workerThread(void*)
{
while(sharedCounter > 0)
{
--sharedCounter;
doSomeWork();
}
The solution is to use a mutex to synchronise the threads with
respect to the test and update. Another way of saying this is
that we need to define a critical section in which we both
test and update the sharedCounter. The next section introduces
mutexes and solves the example race condition.
END OF QUOTE.
I believe that the correct signature of workerThread is void
workerThread(void) rather than void* workerThread(void*)
It depends. If the function is the one passed to
pthread_create, then the correct signature in C++ is:
extern "C" void* workerThread( void* ) ;
Nothing else should pass the compiler. (G++ is buggy here, and
accepts the code without the `extern "C"'.)
For Windows, it should be:
DWORD WINAPI workerThread( __in LPVOID ) ;
(I'm not sure what all those macros mean: DWORD sounds like a
long long, WINAPI is probably some implementation extension
controling the linkage---the equivalent of `extern "C"' in
standard C++, and LPVOID a long pointer to void, except that I
didn't think that Windows supported long (48 bit) pointers.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34