<< Chapter < Page | Chapter >> Page > |
recs % cc -o create1 -lpthread -lposix4 create1.c
recs % create1Main - globvar=0
Main - creating i=0 tid=4 retval=0Main - creating i=1 tid=5 retval=0
Main - creating i=2 tid=6 retval=0Main thread - threads started globvar=0
Main - waiting for join 4TestFunc me=0 - self=4 globvar=0
TestFunc me=0 - sleeping globvar=15TestFunc me=1 - self=5 globvar=15
TestFunc me=1 - sleeping globvar=16TestFunc me=2 - self=6 globvar=16
TestFunc me=2 - sleeping globvar=17TestFunc me=2 - done param=6 globvar=17
TestFunc me=1 - done param=5 globvar=17TestFunc me=0 - done param=4 globvar=17
Main - back from join 0 retval=0Main - waiting for join 5
Main - back from join 1 retval=0Main - waiting for join 6
Main - back from join 2 retval=0Main thread – threads completed globvar=17
recs %
You can see the threads getting created in the loop. The master thread completes the
pthread_create( )
loop, executes the second loop, and calls the
pthread_join( )
function. This function suspends the master thread until the specified thread completes. The master thread is waiting for Thread 4 to complete. Once the master thread suspends, one of the new threads is started. Thread 4 starts executing. Initially the variable
globvar
is set to 0 from the main program. The
self, me
, and
param
variables are thread-local variables, so each thread has its own copy. Thread 4 sets
globvar
to 15 and goes to sleep. Then Thread 5 begins to execute and sees
globvar
set to 15 from Thread 4; Thread 5 sets
globvar
to 16, and goes to sleep. This activates Thread 6, which sees the current value for
globvar
and sets it to 17. Then Threads 6, 5, and 4 wake up from their sleep, all notice the latest value of 17 in
globvar
, and return from the
TestFunc( )
routine, ending the threads.
All this time, the master thread is in the middle of
a pthread_join( )
waiting for Thread 4 to complete. As Thread 4 completes, the
pthread_join( )
returns. The master thread then calls
pthread_join( )
repeatedly to ensure that all three threads have been completed. Finally, the master thread prints out the value for
globvar
that contains the latest value of 17.
To summarize, when an application is executing with more than one thread, there are shared global areas and thread private areas. Different threads execute at different times, and they can easily work together in shared areas.
Multithreaded applications were around long before multiprocessors existed. It is quite practical to have multiple threads with a single CPU. As a matter of fact, the previous example would run on a system with any number of processors, including one. If you look closely at the code, it performs a sleep operation at each critical point in the code. One reason to add the sleep calls is to slow the program down enough that you can actually see what is going on. However, these sleep calls also have another effect. When one thread enters the sleep routine, it causes the thread library to search for other “runnable” threads. If a runnable thread is found, it begins executing immediately while the calling thread is “sleeping.” This is called a user-space thread context switch. The process actually has one operating system thread shared among several logical user threads. When library routines (such as sleep ) are called, the thread library The pthreads library supports both user-space threads and operating-system threads, as we shall soon see. Another popular early threads package was called cthreads . jumps in and reschedules threads.
Notification Switch
Would you like to follow the 'High performance computing' conversation and receive update notifications?