强奸久久久久久久|草草浮力在线影院|手机成人无码av|亚洲精品狼友视频|国产国模精品一区|久久成人中文字幕|超碰在线视屏免费|玖玖欧洲一区二区|欧美精品无码一区|日韩无遮一区二区

首頁 > 廠商 > 經(jīng)驗 > Pthread,pthread如何在一個線程中終止另一個線程

Pthread,pthread如何在一個線程中終止另一個線程

來源:整理 時間:2023-08-27 07:09:46 編輯:智能門戶 手機(jī)版

本文目錄一覽

1,pthread如何在一個線程中終止另一個線程

如果需要只終止某個線程而不終止整個進(jìn)程,可以有三種方法: 1.從線程函數(shù)return。這種方法對主線程不適用,從main函數(shù)return相當(dāng)于調(diào)用exit。 2.一個線程可以調(diào)用pthread_cancel終止同一進(jìn)程中的另一個線程。 3.線程可以調(diào)用pthread_exit終止自己。

pthread如何在一個線程中終止另一個線程

2,如何判斷一個線程已經(jīng)被pthread

還記得signal嗎,大部分signal的默認(rèn)動作是終止進(jìn)程的運(yùn)行,所以,我們才要用signal()去抓信號并加上處理函數(shù)。int pthread_kill(pthread_t thread, int sig);向指定ID的線程發(fā)送sig信號,如果線程代碼內(nèi)不做處理,則按照信號默認(rèn)的行為影響整個進(jìn)程,也就是說,如果你給一個線程發(fā)送了SIGQUIT,但線程卻沒有實現(xiàn)signal處理函數(shù),則整個進(jìn)程退出。pthread_kill(threadid, SIGKILL)也一樣,殺死整個進(jìn)程。如果要獲得正確的行為,就需要在線程內(nèi)實現(xiàn)signal(SIGKILL,sig_handler)了。OK,如果int sig是0呢,這是一個保留信號,一個作用是用來判斷線程是不是還活著。我們來看一下pthread_kill的返回值:成功:0線程不存在:ESRCH信號不合法:EINVAL所以,pthread_kill(threadid,0)就很有用啦。int kill_rc = pthread_kill(thread_id,0);if(kill_rc == ESRCH)printf("the specified thread did not exists or already quit\n");else if(kill_rc == EINVAL)上述的代碼就可以判斷線程是不是還活著了。

如何判斷一個線程已經(jīng)被pthread

3,C語言多線程編程為什么要用pthread

3個線程使用的都是同一個info 代碼 Info_t *info = (Info_t *)malloc(sizeof(Info_t));只創(chuàng)建了一個info pthread_create(&threads[i],NULL,calMatrix,(void *)info); 三個線程使用的是同一個 我把你的代碼改了下
3個線程使用的都是同一個info 代碼 info_t *info = (info_t *)malloc(sizeof(info_t));只創(chuàng)建了一個infopthread_create(&threads[i],null,calmatrix,(void *)info); 三個線程使用的是同一個我把你的代碼改了下:#include #include #include int mtc[3] = { 0 }; // result matrix typedef struct { int prank; int *mta; int *mtb; }info_t; void* calmatrix(void* arg) { int i; info_t *info = (info_t *)arg; int prank = info->prank; fprintf(stdout,"calmatrix : prank is %d\n",prank); for(i = 0; i < 3; i++) mtc[prank] += info->mta[i] * info->mtb[i]; return null; } int main(int argc,char **argv) { int i,j,k = 0; int mta[3][3]; int mtb[3] = { 1 }; info_t *info = (info_t *)malloc(sizeof(info_t)*3); for(i = 0; i < 3; i++) for(j = 0; j < 3; j++) mta[i][j] = k++; /* 3 threads */ pthread_t *threads = (pthread_t *)malloc(sizeof(pthread_t)*3); fprintf(stdout,"\n");fflush(stdout); for(i = 0; i < 3; i++) { info[i].prank = i; info[i].mta = mta[i]; info[i].mtb = mtb; pthread_create(&threads[i],null,calmatrix,(void *)(&info[i])); } for(i = 0; i < 3; i++) pthread_join(threads[i],null); fprintf(stdout,"\n==== the matrix result ====\n\n"); fflush(stdout); for(i = 0; i < 3; i++) { fprintf(stdout,"mtc[%d] = %d\n",i,mtc[i]); fflush(stdout); } return 0; }矩陣的計算我忘記了,你運(yùn)行看看結(jié)果對不對

C語言多線程編程為什么要用pthread

4,pthread函數(shù)怎么用求解

.關(guān)于編譯時出現(xiàn) 對pthread_create未定義的引用 之類的錯誤的解決:由于pthread庫不是Linux系統(tǒng)默認(rèn)的庫,連接時需要使用庫libpthread.a,所以在使用pthread_create創(chuàng)建線程時,在編譯中要加-lpthread參數(shù): gcc -o pthread -lpthread pthread.c 特別的,如果這樣還沒解決的話: 按照上面編譯了一下,還是一樣的提示. 后面man gcc 才知道Usage: gcc [options] file... 因此需要將庫鏈接放在末尾。 xs@vm:~/Desktop$ gcc -o pthread pthread.c -lpthread 2.關(guān)于pthread里的一些函數(shù). pthread_join函數(shù): 函數(shù)pthread_join用來等待一個線程的結(jié)束。 函數(shù)定義: int pthread_join(pthread_t thread, void **retval); 描述 : pthread_join()函數(shù),以阻塞的方式等待thread指定的線程結(jié)束。當(dāng)函數(shù)返回時,被等待線程的資源被收回。如果進(jìn)程已經(jīng)結(jié)束,那么該函數(shù)會立即返回。并且thread指定的線程必須是joinable的。 參數(shù) : thread: 線程標(biāo)識符,即線程ID,標(biāo)識唯一線程。 retval: 用戶定義的指針,用來存儲被等待線程的返回值。 返回值 : 0代表成功。 失敗,返回的則是錯誤號。 看下面一段程序:[cpp] view plain copy print?#include <pthread.h> #include <unistd.h> #include <stdio.h> void *thread(void *str) int i; for (i = 0; i < 10; ++i) sleep(2); printf( "This in the thread : %d\n" , i ); } return NULL; } int main() pthread_t pth; int i; int ret = pthread_create(&pth, NULL, thread, (void *)(i)); pthread_join(pth, NULL); for (i = 0; i < 10; ++i) sleep(1); printf( "This in the main : %d\n" , i ); } return 0; } 也就是說:子線程還沒有執(zhí)行完畢,main函數(shù)已經(jīng)退出,那么子線程也就退出了! 如果我們不注釋掉那一行,那么運(yùn)行結(jié)果如下: 這說明:pthread_join函數(shù)的調(diào)用者在等待子線程退出后才繼續(xù)執(zhí)行! pthread_create函數(shù): 聲明: int pthread_create(pthread_t *thread, const pthread_attr_t *restrict_attr, void*(*start_rtn)(void*), void *restrict arg); 參數(shù): 第一個參數(shù)*thread為指向線程標(biāo)識符的指針。 第二個參數(shù)*restrict_attr用來設(shè)置線程屬性,上面也可以用NULL,表示使用默認(rèn)的屬性。 第三個參數(shù)是線程運(yùn)行函數(shù)的起始地址。 最后一個參數(shù)是運(yùn)行函數(shù)的參數(shù),NULL表示無參數(shù)。 另外,在編譯時注意加上-lpthread參數(shù),以調(diào)用鏈接庫。因為pthread并非Linux系統(tǒng)的默認(rèn)庫,而是posix線程庫,在Linux中將其作為一個庫來使用,因此加上 -lpthread(或-pthread)以顯示的鏈接該庫。函數(shù)在執(zhí)行錯誤時的錯誤信息將作為返回值返回,并不修改系統(tǒng)全局變量errno,當(dāng)然也無法使用perror()打印錯誤信息。 pthread_t:pthread_t用于聲明線程ID! 類型定義: typedef unsigned long int pthread_t; //come from /usr/include/bits/pthread.h sizeof (pthread_t) =4; pthread_attr_init函數(shù): 聲明:int pthread_attr_init(pthread_attr_t*attr); 返回值:返回0,表示函數(shù)初始化對象成功。失敗時返回一個錯誤代碼。 參數(shù):指向一個線程屬性的指針。 下面一個程序是書上的:[cpp] view plain copy print?/*小小的一個程序,折騰人個半死*/ #include <pthread.h> #include <unistd.h> #include <stdio.h> int sum; void *runner (void *param); int main(int argc, char *argv[]) pthread_t tid;/*線程標(biāo)示符*/ pthread_attr_t attr; if (argc != 2)/*如果參數(shù)不為2個*/ fprintf (stderr, "usage:a.out<integer value>\n");/*報錯*/ return -1; } if (atoi(argv[1] ) < 0) fprintf (stderr, "%d must be <= 0\n", atoi(argv[1])); return -1; } pthread_attr_init(&attr); /*初始化,得到默認(rèn)的屬性值*/ pthread_create(&tid, &attr, runner, argv[1]);/*創(chuàng)建一個線程*/ pthread_join(tid, NULL);/*等待子線程執(zhí)行完畢*/ printf ("sum = %d\n", sum); return 0; } void *runner(void *param)/*子線程將會執(zhí)行這個函數(shù)*/ int i, upper = atoi(param); sum = 0; for (i = 1; i <= upper; i++) sum += i; } pthread_exit(0); }
你的理解不對。 首先,pthread_setcancelstate()函數(shù)只是改變本線程(注意是本線程)的cancel state。所以t1進(jìn)入fun()函數(shù),執(zhí)行到pthread_setcancelstate()函數(shù)時,只是改變了t1本身的cancel state,并不能改變t2的cancel state。 第二,線程執(zhí)行到pthread_testcancel()函數(shù)時,并不一定會馬上取消(退出)。 先描述一下取消一個線程的過程:1) 其他線程通過調(diào)用pthread_cancel()函數(shù),向目標(biāo)線程發(fā)送取消請求(cancellation request)。2) 取消請求發(fā)出后,根據(jù)目標(biāo)線程的cancel state來決定取消請求是否會到達(dá)目標(biāo)線程:a. 如果目標(biāo)線程的cancel state是pthread_cancel_enable(默認(rèn)),取消請求會到達(dá)目標(biāo)線程。b. 如果目標(biāo)線程的cancel state是pthread_cancel_disable,取消請求會被放入隊列。直到目標(biāo)線程的cancel state變?yōu)閜thread_cancel_enable,取消請求才會從隊列里取出,發(fā)到目標(biāo)線程。3) 取消請求到達(dá)目標(biāo)線程后,根據(jù)目標(biāo)線程的cancel type來決定線程何時取消:a. 如果目標(biāo)線程的cancel type是pthread_cancel_deferred(默認(rèn)),目標(biāo)線程并不會馬上取消,而是在執(zhí)行下一條cancellation point的時候才會取消。有很多系統(tǒng)函數(shù)都是cancellation point,詳細(xì)的列表可以在linux上用man 7 pthreads查看。除了列出來的cancellation point,pthread_testcancel()也是一個cancellation point。就是說目標(biāo)線程執(zhí)行到pthread_testcancel()函數(shù)的時候,如果該線程收到過取消請求,而且它的cancel type是pthread_cancel_deferred,那么這個線程就會在這個函數(shù)里取消(退出),這個函數(shù)就不再返回了,目標(biāo)線程也沒有了。b. 如果目標(biāo)線程的cancel type是pthread_cancel_asynchronous,目標(biāo)線程會立即取消(這里的“立即”只是說目標(biāo)線程不用等執(zhí)行到屬于cancellation point的函數(shù)的時候才會取消,它會在獲得調(diào)度之后立即取消,因為內(nèi)核調(diào)度會有延時,所以并不能保證時間上的“立即”)。 舉個例子,說明一下這些與線程取消相關(guān)的函數(shù)的用法:void thread_function(void *arg)/*** 線程準(zhǔn)備執(zhí)行一些關(guān)鍵工作,在這個過程中不希望被取消。* 所以先通過pthread_setcancelstate()將本線程的cancel state* 設(shè)為disabled。*/pthread_setcancelstate(pthread_cancel_disable, null);/* 執(zhí)行關(guān)鍵工作 */.../*** 關(guān)鍵工作執(zhí)行完成,可以被取消。* 通過pthread_setcancelstate()將本線程的cancel state* 設(shè)為enabled。*/pthread_setcancelstate(pthread_cancel_enable, null);/*** 調(diào)用pthread_testcancel()函數(shù),檢查一下在cancel state* 為disabled狀態(tài)的時候,是否有取消請求發(fā)送給本線程。* 如果有的話就取消(退出)。*/pthread_testcancel();/*** pthread_testcancel()返回了,表明之前沒有取消請求發(fā)送給本線程,* 繼續(xù)其余的工作。* 這時候如果有取消請求發(fā)送給本線程,會在下一次執(zhí)行到* cancellation point的時候(例如sleep(), read(), write(), ...)時取消。*/.../*** 從這里開始,函數(shù)里不再包含cancellation point了。* 如果收到取消請求,將無法取消。所以先把本線程的cancel type* 設(shè)為asynchronous,收到取消請求將立即取消。*/pthread_setcanceltype(pthread_cancel_asynchronous, null);/* 不包含cancellation point的代碼 */...}
文章TAG:Pthreadpthread如何在一個線程中終止另一個線程

最近更新

  • 阿里文學(xué)作品數(shù)據(jù),全世界文學(xué)作品的數(shù)據(jù)量阿里文學(xué)作品數(shù)據(jù),全世界文學(xué)作品的數(shù)據(jù)量

    阿里文學(xué)of文學(xué)夢想能實現(xiàn)嗎?UC作為阿里娛樂集團(tuán)的重要組成部分,是阿里娛樂板塊的核心旗艦。阿里文學(xué)主編周云說,阿里文學(xué)它將為互聯(lián)網(wǎng)作家和內(nèi)容生產(chǎn)者提供一個全新的選擇和一個全新的付.....

    經(jīng)驗 日期:2023-08-27

  • 大電流,電流過大怎么解決大電流,電流過大怎么解決

    電流過大怎么解決2,電流大是什么意思呢3,什么是大電流接地系統(tǒng)4,電流很大可以電死人嗎5,大電流發(fā)生器的原理電壓調(diào)正器的原理6,大電流發(fā)生器的工作原理1,電流過大怎么解決我編號4臺電機(jī)為1,2,3.....

    經(jīng)驗 日期:2023-08-27

  • 電工配線計算公式,誰知道三項配線的計算方式電工配線計算公式,誰知道三項配線的計算方式

    誰知道三項配線的計算方式2,在平面圖中樓梯間的電氣配線工程量如何計算3,電工導(dǎo)線怎么計算當(dāng)我知道功率的時候怎么去計算用多大的導(dǎo)線呢4,電工電線計算公式5,我想請教一下電工比如說20個千.....

    經(jīng)驗 日期:2023-08-27

  • 扭矩單位,daNm是什么單位扭矩單位,daNm是什么單位

    daNm是什么單位2,力矩的國際單位是3,求一封蓋扭矩單位lbfinch4,馬力和扭矩代表什么1,daNm是什么單位是扭矩的單位一般扭矩的公制單位是Nm(牛.米)而daNm約等于10Nm2,力矩的國際單位是牛頓-米扭.....

    經(jīng)驗 日期:2023-08-27

  • 聯(lián)通pin碼查詢,聯(lián)通手機(jī)puk碼網(wǎng)上查詢聯(lián)通pin碼查詢,聯(lián)通手機(jī)puk碼網(wǎng)上查詢

    聯(lián)通手機(jī)puk碼網(wǎng)上查詢2,聯(lián)通怎么在網(wǎng)上營業(yè)廳查詢PIN密碼3,怎么在網(wǎng)上知道聯(lián)通手機(jī)的PUK碼呀4,聯(lián)通PIN2碼如何查詢5,中國聯(lián)通的PIN碼怎么知道啊6,你好我是聯(lián)通的卡我想鎖定SIM卡但是要PIN.....

    經(jīng)驗 日期:2023-08-27

  • 蘋果6plus數(shù)量診斷數(shù)據(jù)怎么刪除蘋果6plus數(shù)量診斷數(shù)據(jù)怎么刪除

    蘋果6splus的App購買記錄刪除?蘋果6spWhynot刪除Apply,/蘋果6plusapp-0/從app下載記錄怎么樣刪除?蘋果手機(jī)里的健康數(shù)據(jù)呢刪除?蘋果6plus如何徹底刪除所有信息iphone6plus手機(jī)徹底刪除照片.....

    經(jīng)驗 日期:2023-08-27

  • 南昌機(jī)器人比賽2017,南昌智能電話機(jī)器人南昌機(jī)器人比賽2017,南昌智能電話機(jī)器人

    中國機(jī)器人競賽與RoboCup公開賽比賽項目(一)RoboCup足球-0比賽1、模擬組2、小組3、中組4、標(biāo)準(zhǔn)臺組5、班。-0/組(3)RoboCup家庭組比賽(4)FIRA足球機(jī)器人比賽1、模擬組2、小組(5)air,-2/.....

    經(jīng)驗 日期:2023-08-27

  • bufg,例化PLL后ISE在綜合時會自動加bufg嗎bufg,例化PLL后ISE在綜合時會自動加bufg嗎

    例化PLL后ISE在綜合時會自動加bufg嗎2,如何約束兩個bufg之間的延時3,300英雄的bufg召喚符怎么用啊4,verilog程序中BUFG和CLKDLL編譯報錯求助BUFG和CLKDLL的使5,xilinx時鐘bufg怎么用6,DNF遠(yuǎn).....

    經(jīng)驗 日期:2023-08-27