File: inherit.c

package info (click to toggle)
valgrind 1%3A3.2.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 27,372 kB
  • ctags: 23,091
  • sloc: ansic: 192,648; xml: 10,723; sh: 4,750; perl: 4,023; makefile: 2,103; asm: 1,813; cpp: 140; haskell: 139
file content (55 lines) | stat: -rw-r--r-- 844 bytes parent folder | download | duplicates (2)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* test child thread inheriting data */

// ***
//
// Helgrind should detect an error on line 48 for this test, but it doesn't!
//
// ***

#include <pthread.h>
#include <unistd.h>

static volatile int shared[2];

static void *t1(void *v)
{
	volatile int *ip = (int *)v;
	*ip += 44;
	*ip *= 2;
	sleep(1);
	return 0;
}

static void *t2(void *v)
{
	volatile int *ip = (int *)v;
	*ip += 88;
	*ip *= 3;
	sleep(2);
	return 0;
}

int main()
{
	pthread_t a, b;
	volatile int ret = 0;

	sleep(0);

	shared[0] = 22;
	shared[1] = 77;

	pthread_create(&a, NULL, t1, (void *)&shared[0]);	
	pthread_create(&b, NULL, t2, (void *)&shared[1]);

	pthread_join(a, NULL);

	ret += shared[0];	/* no error - a is finished */
	ret += shared[1];	/* expect error - b has not finished,
				   so we can't touch shared[1] yet */

	pthread_join(b, NULL);


	return ret;
}