6
/* Naive dining philosophers with inconsistent lock acquisition
9
static pthread_t phil[5];
10
static pthread_mutex_t chop[5];
12
void* dine ( void* arg )
15
long left = (long)arg;
16
long right = (left + 1) % 5;
17
for (i = 0; i < 1000/*arbitrary*/; i++) {
18
pthread_mutex_lock(&chop[left]);
19
pthread_mutex_lock(&chop[right]);
21
pthread_mutex_unlock(&chop[left]);
22
pthread_mutex_unlock(&chop[right]);
30
for (i = 0; i < 5; i++)
31
pthread_mutex_init( &chop[i], NULL);
33
for (i = 0; i < 5; i++)
34
pthread_create(&phil[i], NULL, dine, (void*)i );
38
for (i = 0; i < 5; i++)
39
pthread_join(phil[i], NULL);