日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

Linux线程的终止

發(fā)布時間:2023/12/31 linux 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linux线程的终止 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Linux線程的終止

1.線程終止的三種情況。
  • 線程從啟動例程中簡單返回(return)。
  • 線程被同一進程中的其它線程取消。
  • 線程調(diào)用pthread_exit()方法。
  • 1.線程終止,并返回數(shù)據(jù)。
    void pthread_exit(void *retval);
    2.取消線程。
    int pthread_cancel(pthread_t thread);
    3.線程清理處理程序。
    void pthread_cleanup_push(void (*routine)(void *),void *arg); void pthread_cleanup_pop(int execute);
    例1.線程清理處理程序執(zhí)行情況
  • 線程從啟動例程簡單返回,即只return,線程清理處理程序不會執(zhí)行。
  • 線程調(diào)用pthread_exit()方法,線程清理處理程序會執(zhí)行。
  • 線程簡單返回(return),pthread_cleanup_pop()中的參數(shù)為非零時,線程清理處理程序會執(zhí)行。
  • #include <stdio.h> #include <stdlib.h> #include <pthread.h> #include <string.h>static void cleanup_fun(void *arg){printf("%s: cleanup\n", (char *)arg); }//簡單返回,return static void *thr_fun1(void *arg){pthread_cleanup_push(cleanup_fun, "thread1 handler1");pthread_cleanup_push(cleanup_fun, "thread1 handler2");if(arg){return ((void *)1);}pthread_cleanup_pop(3);//非零時,線程清理處理程序被調(diào)用pthread_cleanup_pop(0);//零時,線程清理處理程序沒被調(diào)用return ((void *)1); }static void *thr_fun2(void *arg){pthread_cleanup_push(cleanup_fun, "thread2 handler1");pthread_cleanup_push(cleanup_fun, "thread2 handler2");if(arg){//調(diào)用了pthread_exit(),即使pthread_ckeanup_pop()中的參數(shù)為0//線程清理處理程序依然執(zhí)行。pthread_exit((void *)2);}pthread_cleanup_pop(0);pthread_cleanup_pop(0);pthread_exit((void *)2); }int main(int argc, char const *argv[]) {int err;pthread_t tid1, tid2;void *tret;err = pthread_create(&tid1, NULL, thr_fun1, (void *)0);if(err){fprintf(stderr, "th1 create %s\n", strerror(err));exit(err);}err = pthread_create(&tid2, NULL, thr_fun2, (void *)1);if(err){fprintf(stderr, "th2 create %s\n", strerror(err));exit(err);}err = pthread_join(tid1, &tret);//tret接收子線程返回的值,return返回的也能被接收if(err){fprintf(stderr, "th1 join %s\n", strerror(err));exit(err);}printf("th1 return code: %ld\n", (long)tret);err = pthread_join(tid2, &tret);if(err){fprintf(stderr, "th2 join %s\n", strerror(err));exit(err);}printf("th2 exit code: %ld\n", (long)tret);return 0; } 執(zhí)行結(jié)果: thread1 handler2: cleanup th1 return code: 1 thread2 handler2: cleanup thread2 handler1: cleanup th2 exit code: 2
    例2.取消線程

    執(zhí)行pthread_cancel()終止線程,線程清理處理程序會被執(zhí)行。pthread_join()將不會接收到線程啟動例程中返回的數(shù)據(jù)。

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h>static void clean_fun(void *arg) {printf("cleanup\n"); }static void *th_fun(void *arg) {int n = 0;pthread_cleanup_push(clean_fun, NULL);while (1){printf("n = %d\n", n++);sleep(1);}pthread_cleanup_pop(0);return (void *)3; }int main(int argc, char const *argv[]) {pthread_t tid;void *ret;int err;err = pthread_create(&tid, NULL, th_fun, NULL);if (err){fprintf(stderr, "thread create: %s\n", strerror(err));exit(err);}sleep(5);pthread_cancel(tid);err = pthread_join(tid, &ret);if (err){fprintf(stderr, "thread join: %s\n", strerror(err));exit(err);}printf("ret = %lu\n", (unsigned long)ret);return 0; } 執(zhí)行結(jié)果: n = 0 n = 1 n = 2 n = 3 n = 4 cleanup ret = 18446744073709551615//64位機的最大數(shù),強轉(zhuǎn)成int,是-1

    總結(jié)

    以上是生活随笔為你收集整理的Linux线程的终止的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。