Linux多线程实例练习 - pthread_create()

Linux多线程实例练习 pthread_create():创建一个线程

int pthread_create(pthread_t *tidp,

        const pthread_attr_t *attr,

        (void*)(*start_rtn)(void*),

        void *arg);

1、代码如下 xx_pthread_create.c

 1 #include <pthread.h>

 2 #include <stdio.h>

 3 #include <unistd.h>

 4 #include <sys/time.h>   // for gettimeofday

 5 

 6 #define debugMsg(fmt, arg...)\

 7     do{\

 8         unsigned long lSec = 0; unsigned long lUSec = 0;\

 9         getTimeuSec(&lSec, &lUSec);\

10         printf("[%ld.%06ld]:", lSec, lUSec);\                                                        

11         printf(fmt, ##arg);\

12     }while(0)

13 

14 int getTimeuSec(unsigned long *lSec, unsigned long *lUSec)

15 {

16     struct timeval start;

17     gettimeofday( &start, NULL );

18     *lSec = start.tv_sec;

19     *lUSec = start.tv_usec;

20 

21     return 0;

22 }

23 void * doPrint(void *arg)

24 {

25     int i = 0;

26     while(i < 30)

27     {

28         debugMsg("pthread %2d; main %d\n", i++, *(int*)arg);

29         usleep(200000);

30     }

31 

32     return NULL;

33 }

34 

35 int main()

36 {

37     pthread_t pid;

38     int iX = 123;

39     pthread_create(&pid, NULL, doPrint, &iX);

40     while(iX <= 20 + 123)

41     {

42         debugMsg("main : %d\n", iX++);

43         usleep(300000);

44     }

45 

46     return 0;

47 }

 

2、CentOS 下编译通过

g++ -g -c -o xx_pthread_create.o xx_pthread_create.c 

g++ -g -o xx_pthread_create xx_pthread_create.o -lpthread

3、运行结果

[1422496189.763862]:main : 123

[1422496189.764341]:pthread  0; main 124

[1422496189.965627]:pthread  1; main 124

[1422496190.065601]:main : 124

[1422496190.166510]:pthread  2; main 125

[1422496190.366393]:main : 125

[1422496190.367391]:pthread  3; main 126

[1422496190.568275]:pthread  4; main 126

[1422496190.667215]:main : 126

[1422496190.769157]:pthread  5; main 127

[1422496190.968039]:main : 127

[1422496190.970323]:pthread  6; main 128

[1422496191.171922]:pthread  7; main 128

[1422496191.269869]:main : 128

[1422496191.373803]:pthread  8; main 129

[1422496191.571696]:main : 129

[1422496191.574958]:pthread  9; main 130

[1422496191.776566]:pthread 10; main 130

[1422496191.873512]:main : 130

[1422496191.977457]:pthread 11; main 131

[1422496192.174348]:main : 131

[1422496192.178362]:pthread 12; main 132

[1422496192.379214]:pthread 13; main 132

[1422496192.475159]:main : 132

[1422496192.580095]:pthread 14; main 133

[1422496192.776006]:main : 133

[1422496192.781267]:pthread 15; main 134

[1422496192.981968]:pthread 16; main 134

[1422496193.076864]:main : 134

[1422496193.182797]:pthread 17; main 135

[1422496193.377656]:main : 135

[1422496193.384089]:pthread 18; main 136

[1422496193.584595]:pthread 19; main 136

[1422496193.678472]:main : 136

[1422496193.785406]:pthread 20; main 137

[1422496193.980296]:main : 137

[1422496193.987689]:pthread 21; main 138

[1422496194.189201]:pthread 22; main 138

[1422496194.281149]:main : 138

[1422496194.390049]:pthread 23; main 139

[1422496194.582987]:main : 139

[1422496194.590944]:pthread 24; main 140

[1422496194.792793]:pthread 25; main 140

[1422496194.883821]:main : 140

[1422496194.993852]:pthread 26; main 141

[1422496195.184826]:main : 141

[1422496195.195665]:pthread 27; main 142

[1422496195.397447]:pthread 28; main 142

[1422496195.486468]:main : 142

[1422496195.598408]:pthread 29; main 143

[1422496195.787329]:main : 143

 

你可能感兴趣的:(pthread)