|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
#include <stdio.h>* |9 u( q5 f" V
#include <stdlib.h>
& Y9 Y+ d8 _ W#include <unistd.h>
" I- o4 r. l0 ?) z- ]: _#include <pthread.h>
+ n' a0 H; \8 g7 X3 O& i/ Y% J l3 x6 H! ~) t
; \8 l: ?2 G0 h" R' V$ J9 L$ M7 y$ @6 Q1 n& g8 Y" R
8 }- n6 K" T: _$ Zint * thread(void *arg); I6 `* {( z4 P
{, }2 r% J* j7 }7 i# F! Z! I2 {: p
while(1)4 S( c% P0 ~# y: l; m3 u4 [, @& `
{7 n9 I q2 X, F* J! y$ c
printf("It is the thread!\n");4 y( F1 b( f9 \
sleep(1);, N$ u) `" `, j9 }; p2 `$ u6 T
}
$ ]/ @$ M1 r( W$ h8 B 3 M9 P; h6 I, `4 W* ?8 G5 R
return NULL;2 q5 z( A6 k/ t, x+ H, o1 b
}
7 _/ r) @: P/ o/ [
" a( B/ R! l6 X2 I& M" ^* T* @' ?6 g/ l$ D" I* [0 h+ |- S
6 z) p- M, Y! [6 N/ J/ W' d# o# @8 u2 ?; A5 s
int main()7 M6 ?! p+ R( _9 ~( O- C, d
{
: U/ z! u+ A0 P8 Vpthread_t id;
* t# @0 \) A4 G! D5 Vprintf("Main thread id is %d \n",pthread_self());" J. \+ E: z3 @
if(!pthread_create(&id,NULL,(void *)thread,NULL))
3 K$ I( \+ Z- R7 y' F6 T{. z0 R B; t$ S# i9 ^3 r U) e
printf("succeed!\n");
7 x" q. j1 Y P% w7 K5 C2 X! M}
4 [+ K0 O- h- Celse, c/ i6 ^# \' D) F h& k- S, _; i
{; C& }. y$ B! }$ v
printf("Fail to Create Thread");5 v, s1 Y0 v1 A
return -1;4 o( P/ o5 l; D" A
}
6 ?! ~( X& A/ _while(1)1 u. y- g+ E9 g
{8 n( ^ t# l; E* y, {
printf("It is the main process!\n");
: K1 |3 Q, M% ~sleep(1);+ s' Y& n5 T* ~4 I3 ]+ x/ a8 c
}: P3 z, B. F8 A5 X: R6 f
}
0 y) K* M9 J2 x* y& }6 {! D: h) S" r; j) H7 M
% e) Q6 R; v$ E( x( V
sunxk@146:~> gcc -o pthread pthread.c -lpthread
. @3 m: b2 [* x4 P) E X# t3 T- i
: I1 N3 b. B9 K3 E% n, b
sunxk@146:~> ./pthread
4 V! C, Z! F, I; Q) H d" M2 Z+ ^Main thread id is 950486784
9 s! |4 e2 a2 g( Tsucceed!! r0 A7 i; J1 ^) F
It is the thread!
; Y% ]; D2 \) vIt is the main process!
- V0 a) S: U& w% G/ mIt is the thread!
3 n6 w& _, @% ? M' wIt is the main process!' o$ |! `( V! Q" C& l" c
It is the thread!5 ]! W) J. o& E
It is the main process!
; f" f% S+ C0 A7 R) Y6 i$ w) bIt is the thread!: A3 K; x/ n+ p0 M7 ]$ u( u
It is the main process!
8 m0 q* [3 \9 G+ Y7 f
# }2 G& ?7 ]- @4 s0 K {( n* p* q9 q* R) J: C/ E1 f4 G$ T: w
\6 m* {0 T6 S; O
9 S2 s3 _2 V# N6 j- {8 b4 `% X
|
|