Parallel Programming Using Threads 355
int val = 0;49
ThreadD a ta arg ;50
arg . intptr = & val ;51
arg . mlock = & mlock ;52
pthread_t tid [ N UMBER_T HREAD ];53
int rtv ; // return value of pth r ead_cr eate54
int ind ;55
for ( ind = 0; ind < NUMB E R_THRE AD ; ind ++)56
{57
rtv = p thread _creat e (& tid [ ind ] , NULL ,58
threadfunc , ( void *) & arg ) ;59
i f ( rtv != 0)60
{61
printf (" pth read_c reate () fail %d n" , rtv ) ;62
return EXIT _FAILUR E ;63
}64
}65
for ( ind = 0; ind < NUMB E R_THRE AD ; ind ++)66
{67
rtv = p thread_ join ( tid [ ind ] , NULL ) ;68
i f ( rtv != 0)69
{70
printf (" pth r ead_joi n () fail %d n" , rtv ) ;71
return EXIT _FAILUR E ;72
}73
}74
pth read_ mute x _des troy (& mlock );75
return EXIT _SUCCES S ;76
}77
The program has a structure ThreadData that includes two pointers: one for the integer’s
address (i.e., the shared memory) and the other for the mutex’s address. For a critical section
to work as intended, all the threads must attempt to lock and unlock the same mutex.
Hence a pointer to the mutex is passed in ThreadData. If each thread has its own mutex,
then this would be like the library having a key available for every student, even though
there is only one study room. All of them can enter the room and this will create problems.
The critical section includes the code that reads and writes the shared variable. Each
thread obtains the lock by calling pthread mutex lock right after entering the while block.
If the thread cannot lock the mutex (because some other thread is in the critical section),
then that thread will be waiting at pthread mutex lock until the thread can obtain a lock.
The mutex is unlocked by calling pthread mutex unlock at the end of the while block.
The main function creates a single ThreadData object shared by all threads. Before call-
ing pthread mutex lock or pthread mutex unlock, the lock must be initialized by calling
pthread mutex init. This is done in the main function. What is the output of this pro-
gram? Nothing. The if condition in threadfunc is never true and nothing is printed. This
means that all threads keep running indefinitely.
There is much more to say about critical sections of code, and thread synchronization.
This chapter is only an introduction, and covers the most important concepts. Writing
correct multi-threaded programs can be challenging, and developing better tools and pro-
gramming languages for this purpose is an ongoing topic of research. When writing multi-
threaded programs, it is important to identify critical sections and make them atomic.