6 /* Naive dining philosophers with inconsistent lock acquisition
9 static pthread_t phil
[5];
12 char pad
[120 - sizeof(pthread_mutex_t
)];
15 void* dine ( void* arg
)
18 long left
= (long)arg
;
19 long right
= (left
+ 1) % 5;
20 for (i
= 0; i
< 1000/*arbitrary*/; i
++) {
21 pthread_mutex_lock(&chop
[left
].m
);
22 pthread_mutex_lock(&chop
[right
].m
);
24 pthread_mutex_unlock(&chop
[left
].m
);
25 pthread_mutex_unlock(&chop
[right
].m
);
33 assert(sizeof(pthread_mutex_t
) <= 120);
35 for (i
= 0; i
< 5; i
++)
36 pthread_mutex_init( &chop
[i
].m
, NULL
);
38 for (i
= 0; i
< 5; i
++)
39 pthread_create(&phil
[i
], NULL
, dine
, (void*)i
);
43 for (i
= 0; i
< 5; i
++)
44 pthread_join(phil
[i
], NULL
);