Functions called by threads must be thread safe races

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: cer-consumer program: Producer and consumer threads. 586 CHAPTER 11. CONCURRENT PROGRAMMING WITH THREADS Consumer V(e) P(f) V(e) P(f) V(e) Initially e=1 f=0 P(f) Producer P(e) V(f) P(e) V(f) P(e) V(f) ... ... Figure 11.21: Progress graph for prodcons.c. #include <pthread.h> int pthread mutex init(pthread mutex t *mutex, pthread mutexattr t *attr); int pthread mutex lock(pthread mutex t *mutex); int pthread mutex unlock(pthread mutex t *mutex); return: 0 if OK, nonzero on error A mutex must be initialized before it can be used, either at run-time by calling the pthread init function, or at compile-time: 1 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; For our purposes, the attr argument in pthread init will always be NULL and can be safely ignored. The pthread mutex lock function performs a È operation and the pthread mutex unlock function performs a Î operation. Completing the call to pthread mutex lock is referred to as acquiring the mutex, and completing the call to pthread mutex unlock is referr...
View Full Document

This note was uploaded on 09/02/2010 for the course ELECTRICAL 360 taught by Professor Schultz during the Spring '10 term at BYU.

Ask a homework question - tutors are online