-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram.c
More file actions
41 lines (29 loc) · 680 Bytes
/
program.c
File metadata and controls
41 lines (29 loc) · 680 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <assert.h>
int counter = 0;
pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
void *thread (void *arg)
{
(void) arg;
pthread_mutex_lock (&m);
printf ("thread: incrementing\n");
counter++;
pthread_mutex_unlock (&m);
return 0;
}
int main (int argc, char ** argv)
{
pthread_t th;
(void) argc;
(void) argv;
pthread_create (&th, 0, thread, 0);
pthread_mutex_lock (&m);
printf ("main: checking\n");
if (counter == 0) counter = 1;
pthread_mutex_unlock (&m);
pthread_join (th, 0);
assert (counter == 2); // can be violated, if thread 1 runs first
return 0;
}