c語言線程終止練習(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語言實現(xiàn)簡單學(xué)生學(xué)籍管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)簡單學(xué)生學(xué)籍管理系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01C語言數(shù)據(jù)結(jié)構(gòu)之雙鏈表&循環(huán)鏈表&靜態(tài)鏈表詳解
這篇文章主要為大家詳細(xì)介紹了C語言數(shù)據(jù)結(jié)構(gòu)中雙鏈表&循環(huán)鏈表&靜態(tài)鏈表的原理與使用,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-09-09簡要對比C語言中的truncate()函數(shù)與ftruncate()函數(shù)
這篇文章主要介紹了C語言中的truncate()函數(shù)與ftruncate()函數(shù)的簡要對比,注意其之間的區(qū)別,需要的朋友可以參考下2015-09-09