>
|
p := proc( m )
global count;
print( count );
count := count+1;
end proc;
|
| (1) |
Create ten threads running the p function.
>
|
|
Without mutexes the same value may be printed multiple times. (You may have to execute this command multiple times to see this occur.)
>
|
p := proc( m )
global count;
Threads[Mutex][Lock]( m );
print( count );
count := count+1;
Threads[Mutex][Unlock]( m );
end proc;
|
| (4) |
Create ten threads running the new p function.
>
|
|
Using a mutex allows you to control access to the shared variable. Thus each number will be printed only once.