原文: Linux C 多線程
linux下C語言多線程編程
#include <pthread.h>
#include
<stdio.h>
#include
<sys/time.h>
#include
<
string
.h>
#define
MAX 10
pthread_t thread[
2
];
pthread_mutex_t mut;
int
number=
0
, i;
void
*
thread1()
{
printf (
"
thread1 : I'm thread 1\n
"
);
for
(i =
0
; i < MAX; i++
)
{
printf(
"
thread1 : number = %d\n
"
,number);
pthread_mutex_lock(
&
mut);
number
++
;
pthread_mutex_unlock(
&
mut);
Sleep(
2
);
}
printf(
"
thread1 :主函數在等我完成任務嗎?\n
"
);
pthread_exit(NULL);
}
void
*
thread2()
{
printf(
"
thread2 : I'm thread 2\n
"
);
for
(i =
0
; i < MAX; i++
)
{
printf(
"
thread2 : number = %d\n
"
,number);
pthread_mutex_lock(
&
mut);
number
++
;
pthread_mutex_unlock(
&
mut);
Sleep(
3
);
}
printf(
"
thread2 :主函數在等我完成任務嗎?\n
"
);
pthread_exit(NULL);
}
void
thread_create(
void
)
{
int
temp;
memset(
&thread,
0
,
sizeof
(thread));
//
comment1
/*
創建線程
*/
if
((temp = pthread_create(&thread[
0
], NULL, thread1, NULL)) !=
0
)
//
comment2
printf(
"
線程1創建失敗!\n
"
);
else
printf(
"
線程1被創建\n
"
);
if
((temp = pthread_create(&thread[
1
], NULL, thread2, NULL)) !=
0
)
//
comment3
printf(
"
線程2創建失敗
"
);
else
printf(
"
線程2被創建\n
"
);
}
void
thread_wait(
void
)
{
/*
等待線程結束
*/
if
(thread[
0
] !=
0
) {
//
comment4
pthread_join(thread[
0
],NULL);
printf(
"
線程1已經結束\n
"
);
}
if
(thread[
1
] !=
0
) {
//
comment5
pthread_join(thread[
1
],NULL);
printf(
"
線程2已經結束\n
"
);
}
}
int
main()
{
/*
用默認屬性初始化互斥鎖
*/
pthread_mutex_init(
&
mut,NULL);
printf(
"
我是主函數哦,我正在創建線程,呵呵\n
"
);
thread_create();
printf(
"
我是主函數哦,我正在等待線程完成任務阿,呵呵\n
"
);
thread_wait();
return
0
;
}
執行結果
我是主函數哦,我正在創建線程,呵呵
線程1被創建
線程2被創建
我是主函數哦,我正在等待線程完成任務阿,呵呵
thread1 : I
'
m thread 1
thread1 : number =
0
thread2 : I
'
m thread 2
thread2 : number =
1
thread1 : number
=
2
thread2 : number
=
3
thread1 : number
=
4
thread2 : number
=
5
thread1 : number
=
6
thread1 : number
=
7
thread2 : number
=
8
thread1 : number
=
9
thread2 : number
=
10
thread1 :主函數在等我完成任務嗎?
線程1已經結束
thread2 :主函數在等我完成任務嗎?
線程2已經結束
下面一個稍微復雜的多線程
extern int pthread_join __P ((pthread_t __th, void **__thread_return));
第一個參數為被等待的線程標識符,第二個參數為一個用戶定義的指針,它可以用來存儲被等待線程的返回值。這個函數是一個線程阻塞的函數,調用它的線程將一直等待到被等待的線程結束為止,當函數返回時,被等待線程的資源被收回。一個線程的結束有兩種途徑,一種是象我們上面的例子一樣,函數結束了,調用它的線程也就結束了;另一種方式是通過函數pthread_exit來實現。它的函數原型為:
extern void pthread_exit __P ((void *__retval)) __attribute__ ((__noreturn__));
唯一的參數是函數的返回代碼,只要pthread_exit中的參數retval不是NULL,這個值將被傳遞給 thread_return。最后要說明的是,一個線程不能被多個線程等待,否則第一個接收到信號的線程成功返回,其余調用pthread_join的線程則返回錯誤代碼ESRCH。
實例:
#include <stdio.h>
#include
<pthread.h>
#include
<stdlib.h>
pthread_t tid1, tid2;
void
*
tret;
void
*
thr_fn1(
void
*
arg)
{
sleep(
1
);
//
睡眠一秒,等待TID2結束。
pthread_join(tid2, &tret);
//
tid1一直阻賽,等到tid2的退出,獲得TID2的退出碼
printf(
"
thread 2 exit code %d\n
"
, (
int
)tret);
printf(
"
thread 1 returning\n
"
);
return
((
void
*)
2
);
}
void
*
thr_fn2(
void
*
arg)
{
printf(
"
thread 2 exiting\n
"
);
pthread_exit((
void
*)
3
);
}
int
main(
void
)
{
int
err;
err
= pthread_create(&
tid1, NULL, thr_fn1, NULL);
if
(err !=
0
)
printf(
"
can't create thread 1\n
"
);
err
= pthread_create(&
tid2, NULL, thr_fn2, NULL);
if
(err !=
0
)
printf(
"
can't create thread 2\n
"
);
err
= pthread_join(tid1, &tret);
//
祝線程一直阻賽,等待TID1的返回。
if
(err !=
0
)
printf(
"
can't join with thread 1\n
"
);
printf(
"
thread 1 exit code %d\n
"
, (
int
)tret);
//
err = pthread_join(tid2, &tret);
//
if (err != 0)
//
printf("can't join with thread 2\n");
//
printf("thread 2 exit code %d\n", (int)tret);
exit(
0
);
}
命令:#gcc
-lthread myfile11-
3
.c
:#.
/a.
out
運行結果:
thread
2
exiting
thread
2
exit code
3
thread
1
returning
thread
1
exit code
2
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

