C
Comp1597
I have read the above from a multithreading in c++ tutorial:
BEGINNING OF QUOTE
Now imagine that we start a number of threads, all executing
workerThread(). If we have just one thread, doSomeWork() is going to
be executed the correct number of times (whatever sharedCounter starts
out at).
However, with more than one thread doSomeWork() will most likely be
executed too many times. Exactly how many times depends on the number
of threads spawned, computer architecture, operating system scheduling
and...chance. The problem arises because we do not test and update
sharedCounter as an atomic operation, so there is a period where the
value of sharedCounter is incorrect. During this time other threads
can pass the test when they really shouldn't have.
The value of sharedCounter on exit tells us how many extra times
doSomeWork() is called. With a single thread, the final value of
sharedCounter is of course 0. With multiple threads running, it will
be between 0 and -N where N is the number of threads.
Moving the update adjacent to the test will not make these two
operations atomic. The window during which sharedCounter is out of
date will be smaller, but the race condition remains. An illustration
of this non-solution follows:
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*)
Am I correct?
Any other comments on the webpage are welcome.
Thanks.
BEGINNING OF QUOTE
Now imagine that we start a number of threads, all executing
workerThread(). If we have just one thread, doSomeWork() is going to
be executed the correct number of times (whatever sharedCounter starts
out at).
However, with more than one thread doSomeWork() will most likely be
executed too many times. Exactly how many times depends on the number
of threads spawned, computer architecture, operating system scheduling
and...chance. The problem arises because we do not test and update
sharedCounter as an atomic operation, so there is a period where the
value of sharedCounter is incorrect. During this time other threads
can pass the test when they really shouldn't have.
The value of sharedCounter on exit tells us how many extra times
doSomeWork() is called. With a single thread, the final value of
sharedCounter is of course 0. With multiple threads running, it will
be between 0 and -N where N is the number of threads.
Moving the update adjacent to the test will not make these two
operations atomic. The window during which sharedCounter is out of
date will be smaller, but the race condition remains. An illustration
of this non-solution follows:
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*)
Am I correct?
Any other comments on the webpage are welcome.
Thanks.