c語(yǔ)言線程終止練習(xí)示例
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *t1(void *args) {
return (void *) 0;
}
void *t2(void *args) {
printf("thread 2 param[args] = %d\n", args);
pthread_exit((void *) 3);
}
void *t3(void *args) {
while(1) {
printf("thread 3 is working\n");
sleep(1);
}
}
int main(int argc, char *argv[]) {
pthread_t thread;
int err;
void *status;
printf("creating thread 1\n");
err = pthread_create(&thread, NULL, t1, NULL);
if(err) {
printf("Can not created thread 1\n");
exit(-1);
}
pthread_join(thread, &status);
printf("thread 1 exit return code %d\n\n", status);
printf("creating thread 2\n");
err = pthread_create(&thread, NULL, t2, (void *) 9);
if(err) {
printf("Can not created thread 2\n");
exit(-2);
}
pthread_join(thread, &status);
printf("thread 2 exit return code %d\n\n", status);
printf("creating thread 3\n");
err = pthread_create(&thread, NULL, t3, NULL);
if(err) {
printf("Can not created thread 3\n");
exit(-3);
}
sleep(10);
pthread_cancel(thread);
pthread_join(thread, &status);
printf("thread 3 exit return code %d\n", status);
return 1;
}
相關(guān)文章
C++應(yīng)用實(shí)現(xiàn)簡(jiǎn)易五子棋游戲
這篇文章主要為大家詳細(xì)介紹了C++應(yīng)用實(shí)現(xiàn)簡(jiǎn)易五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單學(xué)生學(xué)籍管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單學(xué)生學(xué)籍管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之雙鏈表&循環(huán)鏈表&靜態(tài)鏈表詳解
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)中雙鏈表&循環(huán)鏈表&靜態(tài)鏈表的原理與使用,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-09-09C++實(shí)現(xiàn)打印兩個(gè)有序鏈表公共部分的方法
這篇文章主要介紹了C++實(shí)現(xiàn)打印兩個(gè)有序鏈表公共部分的方法,涉及C++針對(duì)有序鏈表的簡(jiǎn)單遍歷、比較相關(guān)操作技巧,需要的朋友可以參考下2017-05-05C++使用GDAL庫(kù)實(shí)現(xiàn)Tiff文件的讀取
這篇文章主要為大家詳細(xì)介紹了C++使用GDAL庫(kù)實(shí)現(xiàn)Tiff文件的讀取的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03簡(jiǎn)要對(duì)比C語(yǔ)言中的truncate()函數(shù)與ftruncate()函數(shù)
這篇文章主要介紹了C語(yǔ)言中的truncate()函數(shù)與ftruncate()函數(shù)的簡(jiǎn)要對(duì)比,注意其之間的區(qū)別,需要的朋友可以參考下2015-09-09