導航:首頁 > 編程系統 > linuxthreadjoin

linuxthreadjoin

發布時間:2023-06-29 13:41:05

❶ 關於linux下多線程編程

pthread_join 線程停止等待函數沒有調用

pthread_create 線程生成後,沒有等子線程停止,主線程就先停止了。

主線程停止後,整個程序停止,子線程在沒有printf的時候就被結束了。

結論:不是你沒有看到結果,而是在子線程printf("..................\n");之前整個程序就已經停止了。

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>

#define FALSE -1
#define TRUE 0

void *shuchu( void *my )
{
int j;
printf("..................\n");
}
int main()
{
int i = 0;
int rc = 0;
int ret1;
pthread_t p_thread1;
if(0!=(ret1 = pthread_create(&p_thread1, NULL, shuchu, NULL)))printf("sfdfsdfi\n");
printf("[%d]\n",p_thread1);
pthread_join(p_thread1, NULL);
return TRUE;

}

❷ linux多線程問題:pthread_join 和 pthread_exit 的區別 求指教

pthread_join一般是主線程來調用,用來等待子線程退出,因為是等待,所以是阻塞的,一般主線程會依次join所有它創建的子線程。
pthread_exit一般是子線程調用,用來結束當前線程。
子線程可以通過pthread_exit傳遞一個返回值,而主線程通過pthread_join獲得該返回值,從而判斷該子線程的退出是正常還是異常。

❸ 如何在linux上使用boost:thread-C/C++

下載到boost_1_49_0.tar.bz2 (當然,其他壓縮格式也可以)後,可以把它放在用戶目錄下,即:~/

解壓縮:tar -jxvf boost_1_49_0.tar.bz2

這樣,出現文件夾:~/boost_1_49_0

然後進入:$ cd boost_1_49_0

你會發現有一個sh命令:bootstrap.sh

運行它:$ ./bootstrap.sh (boost自己的get start文檔中說設置參數 --prefix=dir 其中dir為你想指定的安裝文件夾,我建議就不用加這個參數,它會默認安裝到/usr/local)

結束後出現一個可執行文件: ~/boost_1_49_0/b2

運行這個文件: $ sudo ./b2 install (Ubuntu用戶千萬別忘了加sudo,不然安裝後將無法完全使用)

編譯安裝時間比較長,根據不同機器的情況20~40分鍾。結束後即安裝完畢。


#include<boost/thread.hpp>
#include<iostream>

voidtask1(){
//dostuff
std::cout<<"Thisistask1!"<<std::endl;
}

voidtask2(){
//dostuff
std::cout<<"Thisistask2!"<<std::endl;
}

intmain(intargc,char**argv){
usingnamespaceboost;
threadthread_1=thread(task1);
threadthread_2=thread(task2);

//dootherstuff
thread_2.join();
thread_1.join();
return0;
}

編譯時的命令為:
$ g++ -I./inlcude -L./lib example.cpp -lboost_thread -o example
編譯之後會出現一個 example 的可執行文件,可以運行:./example , 結果顯示:
This is task2!
This is task1!

可能你在運行時會出現這樣的錯誤:error while loading shared libraries: libboost_thread.so.1.49.0: cannot open shared object file: No such file or directory

這是因為要用到的庫不在默認的環境變數里,可以使用下面的命令添加:
$ sudo ldconfig /usr/local/lib

添加後,再執行./example,這樣你就完成了你的第一個boost::thread程序。

❹ 關於Linux 線程pthread_join的用法

Linux系統pthread_join用於掛起當前線程(調用pthread_join的線程),直到thread指定的線程終止運行為止,當前線程才繼續執行。

案例代碼

/*******************************************
**Name:pthread_join.c
**用於Linux下多線程學習
**案例解釋線程的暫停和結束
**Author:admin
**Date:2015/8/11
**Copyright(c)2015,AllRightsReserved!
**********************************************
#include<pthread.h>
#include<unistd.h>
#include<stdio.h>
void*thread(void*str)
{
inti;
//不調用pthread_join線程函數
for(i=0;i<10;++i)
{
sleep(2);
printf("Thisinthethread:%d ",i);
}
returnNULL;
}

intmain()
{
pthread_tpth;
inti;
intret=pthread_create(&pth,NULL,thread,(void*)(i));
//調用pthread_join線程函數
pthread_join(pth,NULL);
for(i=0;i<10;++i)
{
sleep(1);
printf("Thisinthemain:%d ",i);
}

return0;
}

通過Linux下shell命令執行上面的案例代碼:

[root@localhostsrc]#gccpthread_join.c-lpthread
[root@localhostsrc]#./a.out
Thisinthemain:0
Thisinthethread:0
Thisinthemain:1
Thisinthemain:2
Thisinthethread:1
Thisinthemain:3
Thisinthemain:4
Thisinthethread:2
Thisinthemain:5
Thisinthemain:6
Thisinthethread:3
Thisinthemain:7
Thisinthemain:8
Thisinthethread:4
Thisinthemain:9

子線程還沒有執行完畢,main函數已經退出,那麼子線程也就退出了,「pthread_join(pth,NULL);」函數起作用。

[root@localhostsrc]#gccpthread_join.c-lpthread
[root@localhostsrc]#./a.out
Thisinthethread:0
Thisinthethread:1
Thisinthethread:2
Thisinthethread:3
Thisinthethread:4
Thisinthethread:5
Thisinthethread:6
Thisinthethread:7
Thisinthethread:8
Thisinthethread:9
Thisinthemain:0
Thisinthemain:1
Thisinthemain:2
Thisinthemain:3
Thisinthemain:4
Thisinthemain:5
Thisinthemain:6
Thisinthemain:7
Thisinthemain:8
Thisinthemain:9

這說明pthread_join函數的調用者在等待子線程退出後才繼續執行。

❺ Linux線程及同步

linux多線程
1.線程概述
線程是一個進程內的基本調度單位,也可以稱為輕量級進程。線程是在共享內存空間中並發的多道執行路徑,它們共享一個進程的資源,如文件描述和信號處理。因此,大大減少了上下文切換的開銷。一個進程可以有多個線程,也就
是有多個線程式控制製表及堆棧寄存器,但卻共享一個用戶地址空間。
2.線程實現
線程創建pthread_create()
所需頭文件#include
<pthread.h>
函數原型int
pthread_create
((pthread_t
*thread,
pthread_attr_t
*attr,
thread:線程標識符
attr:線程屬性設置
start_routine:線程函數的起始地址
arg:傳遞給start_routine的參數
函數返回值
成功:0
出錯:-1
線程退出pthread_exit();
所需頭文件#include
<pthread.h>
函數原型void
pthread_exit(void
*retval)
函數傳入值retval:pthread_exit()調用者線程的返回值,可由其他函數如pthread_join
來檢索獲取
等待線程退出並釋放資源pthread_join()
所需頭文件#include
<pthread.h>
函數原型int
pthread_join
((pthread_t
th,
void
**thread_return))
函數傳入值
th:等待線程的標識符
thread_return:用戶定義的指針,用來存儲被等待線程的返回值(不為NULL時)
函數返回值
成功:0
出錯:-1
代碼舉例
1.
#include<pthread.h>
2.
#include<stdio.h>
3.
#include<errno.h>
4.
5.
/*線程1*/
6.
void
thread1()
7.
{
8.
int
i=0;
9.
10.
while(1)
11.
{
12.
printf(thread1:%d/n,i);
13.
if(i>3)
14.
pthread_exit(0);
15.
i++;
16.
sleep(1);
17.
}
18.
}
19.
20.
/*線程2*/
21.
void
thread2()
22.
{
23.
int
i=0;
24.
25.
while(1)
26.
{
27.
printf(thread2:%d/n,i);
28.
if(i>5)
29.
pthread_exit(0);
30.
i++;
31.
sleep(1);
32.
}
33.
}
34.
35.
int
main()
36.
{
37.
pthread_t
t1,t2;
38.
39.
/*創建線程*/
40.
pthread_create(&t1,NULL,(void
*)thread1,NULL);
41.
pthread_create(&t2,NULL,(void
*)thread2,NULL);
42.
/*等待線程退出*/
43.
pthread_join(t1,NULL);
44.
pthread_join(t2,NULL);
45.
return
0;
46.
}
3同步與互斥
<1>互斥鎖
互斥鎖的操作主要包括以下幾個步驟。

互斥鎖初始化:pthread_mutex_init

互斥鎖上鎖:pthread_mutex_lock

互斥鎖判斷上鎖:pthread_mutex_trylock

互斥鎖接鎖:pthread_mutex_unlock

消除互斥鎖:pthread_mutex_destroy
1.
#include<pthread.h>
2.
#include<stdio.h>
3.
#include<errno.h>
4.
5.
int
i=0;/*共享變數*/
6.
pthread_mutex_t
mutex=PTHREAD_MUTEX_INITIALIZER;/*互斥鎖*/
7.
8.
void
thread1()
9.
{
10.
int
ret;
11.
while(1)
12.
{
13.
14.
15.
ret=pthread_mutex_trylock(&mutex);/*判斷上鎖*/
16.
17.
if(ret!=EBUSY)
18.
{
19.
pthread_mutex_lock(&mutex);/*上鎖*/
20.
printf(This
is
thread1:%d/n,i);
21.
i++;
22.
pthread_mutex_unlock(&mutex);/*解鎖*/
23.
}
24.
sleep(1);
25.
}
26.
}
27.
28.
void
thread2()
29.
{int
ret;
30.
while(1)
31.
{
32.
33.
ret=pthread_mutex_trylock(&mutex);
34.
if(ret!=EBUSY)
35.
{
36.
pthread_mutex_lock(&mutex);
37.
printf(This
is
thread2:%d/n,i);
38.
i++;
39.
pthread_mutex_unlock(&mutex);
40.
}
41.
sleep(1);
42.
}
43.
}
44.
int
main()
45.
{
46.
pthread_t
t1,t2;
47.
pthread_mutex_init(&mutex,NULL);
48.
pthread_create(&t1,NULL,(void
*)thread1,NULL);
49.
pthread_create(&t2,NULL,(void
*)thread2,NULL);
50.
51.
pthread_join(t1,NULL);
52.
pthread_join(t2,NULL);
53.
54.
pthread_mutex_destroy(&mutex);
55.
return
0;
56.
}
<2>信號量
未進行同步處理的兩個線程
1.
#include<pthread.h>
2.
#include<stdio.h>
3.
#include<errno.h>
4.
5.
int
i=0;
6.
void
thread1()
7.
{
8.
9.
while(1)
10.
{
11.
printf(This
is
thread1:%d/n,i);
12.
i++;
13.
sleep(1);
14.
}
15.
}
16.
17.
18.
void
thread2()
19.
{
20.
21.
while(1)
22.
{
23.
printf(This
is
thread2:%d/n,i);
24.
i++;
25.
sleep(1);
26.
}
27.
}
28.
29.
int
main()
30.
{
31.
pthread_t
t1,t2;
32.
33.
pthread_create(&t1,NULL,(void
*)thread1,NULL);
34.
pthread_create(&t2,NULL,(void
*)thread2,NULL);

閱讀全文

與linuxthreadjoin相關的資料

熱點內容
js裡面的圖片對齊 瀏覽:965
三星2016視頻文件夾 瀏覽:317
舊手機創新手機數據怎麼傳 瀏覽:954
怎麼刪除領克app里的記錄 瀏覽:254
捷波朗弦月3最新版本 瀏覽:123
win10保存不了文件 瀏覽:735
jsonobject解析list 瀏覽:558
網站未知回應怎麼回事 瀏覽:103
refdoc資料庫 瀏覽:602
傳奇世界文件在哪裡下載 瀏覽:306
國際象棋編程哪個好 瀏覽:255
一加5截屏在哪個文件夾 瀏覽:764
發送失敗錯誤代碼10009 瀏覽:508
word轉換點陣圖 瀏覽:237
百度文件包含違規內容提取不了 瀏覽:317
大名網站推廣多少錢 瀏覽:791
喜歡網路詞都有什麼 瀏覽:811
怎麼設置iphone網路 瀏覽:281
cad坐標轉換圖文教程 瀏覽:397
蘋果12原裝數據線怎麼感覺有點硬 瀏覽:764

友情鏈接