Add 469782 to NEWS
[valgrind.git] / helgrind / tests / tc14_laog_dinphils.c
blobad45f693e82bfa692f1882250f577b0c6c8c20f5
2 #include <pthread.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <assert.h>
6 /* Naive dining philosophers with inconsistent lock acquisition
7 ordering. */
9 static pthread_t phil[5];
10 static struct {
11 pthread_mutex_t m;
12 char pad[120 - sizeof(pthread_mutex_t)];
13 } chop[5];
15 void* dine ( void* arg )
17 int i;
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);
23 /* eating */
24 pthread_mutex_unlock(&chop[left].m);
25 pthread_mutex_unlock(&chop[right].m);
27 return NULL;
30 int main ( void )
32 long i;
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 );
41 sleep(1);
43 for (i = 0; i < 5; i++)
44 pthread_join(phil[i], NULL);
46 return 0;