-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpthreadTest.c
70 lines (64 loc) · 1.63 KB
/
pthreadTest.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
/*****************************************************************************
** Name:pthreadTest.c
** testing for multithread in linux
**
** Author:fivezh@gmail.com
** Date:2013-4-15
** Copyright (c) 2013,All Rights Reserved!
*****************************************************************************/
#include <pthread.h> //pthread_
#include <stdio.h> //printf
#include <unistd.h> //sleep
struct data
{
int dataID;
int realData;
};
int dataType;
pthread_t pThreadId[10];
void *funcPThread(void *var)
{
int *pDataType;
pDataType=(int *)var;
pthread_t pid;
pid = pthread_self();
int selfData=0;
if (pid == pThreadId[0])
{
printf("\n*****pthread0 data change begin, selfData = %d\n", selfData);
selfData=4;
printf("my pid = %u, data is %d\n", (unsigned int)pid, *pDataType);
printf("*****pthread0 data change end, selfData = %d\n", selfData);
}
else if (pid == pThreadId[1])
{
printf("\n*****pthread1 data change begin, selfData = %d\n", selfData);
selfData=5;
printf("my pid = %u, data is %d\n", (unsigned int)pid, *pDataType);
printf("*****pthread1 data change end, selfData = %d\n", selfData);
}
//线程对多线程共享的数据可能污读
printf("Common scope selfData = %d\n", selfData);
return (void *)0;
}
void *funcPThread2()
{
printf("this is another thread function\n");
}
int main(int argc, char **args)
{
int error;
int count=2;
int i;
for (i = 0; i < count; i++)
{
dataType=i;
error=pthread_create(&pThreadId[i], NULL, funcPThread, (void *)&dataType);
if (error)
{
printf("pthread create error\n");
}
pthread_join(pThreadId[i], NULL);
printf("hello end\n");
}
}