-
Notifications
You must be signed in to change notification settings - Fork 1
/
memcheck_rt_thread.c
73 lines (64 loc) · 1.2 KB
/
memcheck_rt_thread.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <pthread.h>
#include <stdio.h>
#include <malloc.h>
const int thread_num = 5 ;
const int test_count = 1 ;
void test_normal(void)
{
void *p = malloc(4) ;
usleep(10000) ;
free(p) ;
}
void test1(void)
{
void *p = malloc(1) ;
}
void test2(void)
{
void *p = malloc(3) ;
sleep(5) ;
// p = malloc(5) ;
}
void test3(void)
{
void *p = malloc(4) ;
sleep(10) ;
free(p) ;
sleep(20) ;
// p = malloc(4) ;
}
void *thread_test_func(void *p)
{
int i ;
int index = (int)(p) ;
printf("thread index %d\n", index) ;
for (i = 0 ; i < test_count ; i++)
{
printf("thread index %d: normal\n", index) ;
test_normal() ;
printf("thread index %d: test1\n", index) ;
test1() ;
printf("thread index %d: test2\n", index) ;
test2() ;
printf("thread index %d: test3\n", index) ;
test3() ;
printf("thread index %d: end\n", index) ;
}
}
int main(void)
{
printf("main thread is %lu\n", pthread_self() ) ;
int i ;
pthread_t tid[thread_num] ;
for (i = 0 ; i < thread_num ; i++)
{
pthread_create(&tid[i], NULL, thread_test_func, (void *)i) ;
printf("new thread %lu\n", tid[i]) ;
}
for (i = 0 ; i < thread_num ; i++)
{
printf("join thread %lu\n", tid[i]) ;
pthread_join(tid[i], NULL) ;
}
return 0 ;
}