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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【线程】线程基本函数

發布時間:2023/11/30 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【线程】线程基本函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、pthread_self函數

功能:獲取線程ID。

pthread_t pthread_self(void);
  • 線程ID:pthread_t類型,本質:在Linux為無符號整數(%lu),其他系統可能是結構體實現
  • 線程ID是進程內部識別標志。(兩個進程間,線程ID允許相同)

注意:不應使用全局變量pthread_t tid,在子線程通過pthread_create傳出參數來獲取線程ID,而應使用pthread_self.

?

二、pthread_create函數

功能:創建一個新線程

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg); typedef void *(*start_routine) (void *); int pthread_create(pthread_t *thread, const pthread_attr_t *attr, start_routine th_fn, void *arg);

參數:

  • pthread_t:當前Linux可理解為:typedef unsigned long int pthread_t;
  • 參數1:傳出參數,保存系統為我們分配好的線程ID
  • 參數2:通常傳NULL,表示使用線程默認屬性。若想使用具體屬性也可以修改該參數。
  • 參數3:函數指針,指向線程主函數(線程體),該函數運行結束,則線程結束。
  • 參數4:線程執行期間所使用的參數。

?

1. 測試代碼:

#include<stdio.h> #include<pthread.h> #include<unistd.h> #include<stdlib.h>void *thrd_func(void *arg) {printf("In thread:thread id = %lu, pid = %u\n", pthread_self(), getpid());return NULL; }int main(void) {pthread_t tid;int ret; printf("In main 1: thread id = %lu, pid = %u\n", pthread_self(), getpid());ret = pthread_create(&tid, NULL, thrd_func, NULL);if(ret != 0) {fprintf(stderr, "pthread_create error: %s\n", sterror(ret));exit(1);}sleep(1); printf("In main 2: thread id = %lu, pid =%u\n", pthread_self(), getpid()); return 0; }

輸出結果:

?

?

2. 測試代碼:

#include<stdio.h> #include<pthread.h> #include<unistd.h> #include<stdlib.h> #include<string.h>void *thrd_func(void *arg) {int i = (int) arg;sleep(i);printf("%dth thread: id = %lu, pid = %u\n", i+1, pthread_self(), getpid());return NULL; }int main(void) {pthread_t tid;int ret, i; for(i = 0; i < 5; i++) {ret = pthread_create(&tid, NULL, thrd_func, (void *)i);if(ret != 0) {fprintf(stderr, "pthread_create%s\n", strerror(ret));exit(1);}}sleep(i);return 0; //將當前進程退出 }

輸出結果:

?

?

?

三、線程與共享

注意:線程間共享全局變量!

【牢記】線程默認共享數據段、代碼段等地址空間,常用的是全局變量,而進程間不共享變量,只能借助mmap。

線程共享資源:

  • 文件描述符
  • 每種信號的處理方式
  • 當前工作目錄
  • 用戶ID和組ID
  • 內存地址空間(.text/ .data/ .bss/heap/共享庫)

線程非共享資源

  • 線程ID
  • 處理器現場和棧指針(內核棧)
  • errno變量
  • 信號屏蔽字

測試代碼:

#include<stdio.h> #include<pthread.h> #include<stdlib.h> #include<unistd.h>int var = 100;void *tfn(void *arg) {var = 200;printf("thred\n");return NULL; }int main(void) {printf("At first var = %d\n", var);pthread_t tid;pthread_create(&tid, NULL, tfn, NULL);sleep(1);printf("after pthread_create, var = %d\n", var);return 0;}

輸出結果:

?

?

四、pthread_exit函數

功能:將單個線程退出

void pthread_exit(void *retval) 參數:retval表示線程退出狀態,通常NULL

?

1. 測試代碼:

#include <pthread.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h>int func(int a) {pthread_exit(NULL); }void *tfn(void *arg) {int i;i = (int)arg;sleep(i);if(i == 2)func(888);printf("I'am %dth thread, Thread_ID = %lu\n", i+1, pthread_self());pthread_exit(NULL); }int main() {int n = 5, i;pthread_t tid;for(i = 0; i < n; ++i) {pthread_create(&tid, NULL, tfn, (void*)i);}sleep(i);printf("I am main, and I am not a process, I'am a thread!\n""main_thread_ID = %lu\n", pthread_self());return 0; }

輸出結果:

?

2. 測試代碼:

#include <pthread.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h>int func(int a) {return 0; }void *tfn(void *arg) {int i;i = (int)arg;sleep(i);if(i == 2)func(888);printf("I'am %dth thread, Thread_ID = %lu\n", i+1, pthread_self());pthread_exit(NULL); }int main() {int n = 5, i;pthread_t tid;for(i = 0; i < n; ++i) {pthread_create(&tid, NULL, tfn, (void*)i);}sleep(i);printf("I am main, and I am not a process, I'am a thread!\n""main_thread_ID = %lu\n", pthread_self());return 0; }

?輸出結果:

?

五、pthread_join函數

int pthread_join(pthread_t thread, void **retval);返回值:若成功,返回0,否則,返回錯誤編號

參數:

  • thread:線程ID(【注意】:不是指針);retval:存儲線程結束狀態

注意:

  • 調用該函數的線程將掛起等待,直到id為thread的線程終止狀態。thread線程以不同的方法終止,通過pthread_join得到的終止狀態是不同的,總結如下:
  • 如果thread線程通過return返回,retval所指向的單元存放的是thread線程函數的返回值。
  • 若果thread線程被別的線程調用pthread_cancel異常終止掉,retval指向單元里存放的是常數PTHREAD_CANCELED。
  • ?

    1. 測試代碼:

    #include<pthread.h> #include<stdlib.h>typedef struct {int a;int b; } exit_t; void *tfn(void *arg) {exit_t *ret;ret = (exit*)malloc(sizeof(exit_t));ret->a = 100;ret->b = 300;pthred_exit((void *) ret); } int main() {pthread_t tid;exit_t *retval;pthread_create(&tid, NULL, tfn, NULL);pthread_join(tid, (void**)&retval); /*調用pthread_join可以獲取線程的退出轉態*/ printf("a = %d, b = %d\n", retval->a, retval->b);return 0;}

    輸出結果:

    ?

    2. 測試代碼:

    #include<stdio.h> #include<string.h> #include<pthread.h> #include<stdlib.h> #include<unistd.h> #include<string.h>typedef struct {int ch;int var;char str[64]; }exit_t;void *thrd_func(void *arg) {exit_t *retval = (exit_t *)arg;retval->ch = 'm';retval->var = 200;strcpy(retval->str, "my thread\n");pthread_exit((void*) retval); } int main() {pthread_t tid;int ret;exit_t *retval = (exit_t *)malloc(sizeof(exit_t));printf("In main 1: thread id = %lu, pid = %u\n", pthread_self(), getpid());ret = pthread_create(&tid, NULL, thrd_func, (void*)retval);if(ret != 0) {fprintf(stderr, "pthread_create error: %s\n", strerror(ret));exit(1);}pthread_join(tid, (void**)&retval);printf("ch = %c, var = %d, str = %s\n", retval->ch, retval->var, retval->str);free(retval);pthread_exit((void *)1); }

    輸出結果:

    ?

    測試代碼:

    #include<stdio.h> #include<string.h> #include<pthread.h> #include<stdlib.h> #include<string.h> #include<unistd.h>typedef struct {int ch;int var;char str[64]; }exit_t;void *thrd_func(void *arg) {exit_t *retvar = (exit_t *)malloc(sizeof(exit_t));retvar->ch = 'm';retvar->var = 200;strcpy(retvar->str, "my thread\n");pthread_exit((void*) retvar); } int main() {pthread_t tid;int ret;exit_t *retval;printf("In main 1: thread id = %lu, pid = %u\n", pthread_self(), getpid());ret = pthread_create(&tid, NULL, thrd_func, NULL);if(ret != 0) {fprintf(stderr, "pthread_create error: %s\n", strerror(ret));exit(1);}pthread_join(tid, (void**)&retval);printf("ch = %c, var = %d, str = %s\n", retval->ch, retval->var, retval->str);free(retval);pthread_exit((void *)1); }

    輸出結果:

    ?

    3. 測試代碼:

    #include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<pthread.h>int var = 100;void *tfn(void *arg) {int i;i = (int) arg;sleep(i);if(i == 1) {var = 333;printf("var = %d\n", var);return (void*) var;} else if(i == 3) {var = 777;printf("I'm %dth pthread, pthread_id = %lu\n var = %d\n", i+1, pthread_self(), var);pthread_exit((void*)var); } else {printf("I'm %dth pthread, pthread_id = %lu\n var = %d\n", i+1, pthread_self(), var);pthread_exit((void*)var);}return NULL; }int main() {int i;pthread_t tid[5];int *ret[5];for(i = 0; i < 5; ++i) {pthread_create(&tid[i], NULL, tfn, (void*)i);}for(i = 0; i < 5; ++i) {pthread_join(tid[i], (void**)&ret[i]);printf("----------%d's ret = %d\n", (int)ret[i]); }printf("I'm main pthread tid = %lu\t var = %d\n", pthread_self(), var);sleep(i);return 0; }

    輸出結果:

    總結

    以上是生活随笔為你收集整理的【线程】线程基本函数的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。