线程函数
線程函數
man pthreads了解線程相關內容。
線程函數成功返回0,失敗返回錯誤碼,不設置errno。POSIX.1-2001指出線程函數絕不會(never)返回EINTR(不會因EINTR而失敗)。
- 線程創建
attr一般為NULL,采用系統默認屬性。
int pthread_attr_init(pthread_attr_t *attr);int pthread_attr_destroy(pthread_attr_t *attr);>>默認創建的線程是joinable的,可通過函數pthread_attr_setdetachstate()設置attr從而創建detached的線程。
>>可通過pthread_attr_setstacksize()設置attr從而創建指定棧大小的線程。pthread_attr_getstacksize()獲取當線程棧大小。
成功返回0,失敗返回錯誤號。
pthread_t無符號整型 typedef unsigned long int pthread_t
pthread庫的函數都是通過返回值返回錯誤號,雖然每個線程都有一個errno,但并不使用它。因此不能調用perror打印錯誤信息,可先用strerror把返回值(錯誤碼)轉化為錯誤信息再打印。
注:線程函數參數和返回值都是void*,且函數返回的指針所指向的內存單元必須是全局的或者malloc分配的。
void * (*start_routine)(void *)- 線程終止
終止線程有三種方法:
1)從線程函數return。
2)調用pthread_cancel()終止同一進程中的另一個線程。
3)線程可調用pthread_exit()終止自己。
void pthread_exit(void *value_ptr);無返回值,總是成功。
注:pthread_exit或者return返回的指針所指向的內存單元必須是全局的或者malloc分配的,不能在線程函數的棧上分配。
int pthread_cancel(pthread_t thread);成功返回0,失敗返回非零錯誤號。
被終止的線程的響應取決于可終止狀態和類型(cancelability state and type).
注:如果任意一個線程調用exit或_exit,則整個進程的所有線程都終止。
- 獲取終止狀態
等待線程終止,并獲取線程退出狀態。該線程必須是joinable。調用該函數的線程將掛起等待,直到id為thread的線程終止。阻塞函數
函數調用時注意參數:定義為void *res; 調用pthread_join(&res);最終調用參數(char *)res。
retval:
1)如果thread線程通過return返回,thread線程函數返回值。
2)pthread_cancel()異常終止,則retval所指向的單元存放常量PTHREAD_CANCELED(-1)。
3)自己調用pthread_exit()終止,retval存放pthread_exit參數。
- 分離線程
標記線程thread為分離狀態。當一個分離狀態的線程終止時,它的資源自動釋放給系統,不需要其他線程join。
成功返回0,失敗返回錯誤號。
注:讓線程自己pthread_detach(線程函數內調用)不好,庫函數不是原子的。
注:不能對一個已經處于detach狀態的線程調用pthread_join.==>EINVAL.
注:不能對同一線程調用兩次pthread_join或pthread_detach,或者一個線程已經調用pthread_detach就不能再調用pthread_join了。
注:線程創建時,都應該調用pthread_join()或pthread_detach(),以使系統資源釋放。
示例:pthread_detach(pthread_self());
- 獲取線程id
函數總是成功,返回id。
- 信號函數
線程信號用pthread_sigmask, pthread_kill。
線程讀寫用pread/pwrite.(文件偏移不改變)
?
manpage示例
#include <ctype.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <errno.h>#define handle_error_en(en, msg) \do{ errno = en; perror(msg); exit(EXIT_FAILURE); } while(0) #define handle_error(msg) \do { perror(msg); exit(EXIT_FAILURE); } while(0)struct thread_info{pthread_t thread_id;int thread_num;char *argv_string; };static void * t1(void *arg){ struct thread_info *tinfo = arg;char *uargv, *p; printf("Thread %d: top of stack near %p; argv_string=%s\n", tinfo->thread_num, &p, tinfo->argv_string);uargv = strdup(tinfo->argv_string);if(uargv == NULL)printf("strdup error.\n");for(p = uargv; *p != '\0'; p++)*p = toupper(*p);return uargv; }int main(int argc, char ** argv) {int s, tnum, opt, num_threads;struct thread_info *tinfo;pthread_attr_t attr;int stack_size;void *res;/* the "-s" option specifies a stack size for our threads */stack_size = -1;while((opt = getopt(argc, argv, "s:")) != -1){switch(opt){case 's':stack_size = strtoul(optarg, NULL, 0);break;default:fprintf(stderr, "Usage: %s [-s stack-size] arg ...\n",argv[0]);exit(EXIT_FAILURE);}}num_threads = argc - optind;s = pthread_attr_init(&attr);if(s != 0)handle_error_en(s, "pthread_attr_init");if(stack_size > 0){s = pthread_attr_setstacksize(&attr, stack_size);if(s != 0)handle_error_en(s, "pthread_attr_setstacksize");}tinfo = calloc(num_threads, sizeof(struct thread_info));if(tinfo == NULL)handle_error("calloc");for(tnum = 0; tnum < num_threads; tnum++){tinfo[tnum].thread_num = tnum +1;tinfo[tnum].argv_string = argv[optind + tnum];s = pthread_create(&tinfo[tnum].thread_id, &attr, &t1, &tinfo[tnum]);if(s != 0)handle_error_en(s, "pthread_create");}s = pthread_attr_destroy(&attr);if(s != 0)handle_error_en(s, "pthread_attr_destroy");for(tnum = 0; tnum < num_threads; tnum++){s = pthread_join(tinfo[tnum].thread_id, &res);if(s != 0)handle_error_en(s, "pthread_join");printf("Joined with thread %d; returned value was %s\n",tinfo[tnum].thread_num, (char*)res);free(res);}free(tinfo);exit(EXIT_SUCCESS); }執行:
yuxi@ubuntu:~/test/pthread$ ./a.out yuxi@ubuntu:~/test/pthread$ ./a.out -s 0x100000 abc def CHA Thread 3: top of stack near 0x7f7be5184f20; argv_string=CHA Thread 2: top of stack near 0x7f7be5285f20; argv_string=def Thread 1: top of stack near 0x7f7be5a74f20; argv_string=abc Joined with thread 1; returned value was ABC Joined with thread 2; returned value was DEF Joined with thread 3; returned value was CHA?
轉載于:https://www.cnblogs.com/embedded-linux/p/5071549.html
總結
- 上一篇: 2015.12.23 OC中的字符串(N
- 下一篇: Mac 如何恢复出厂设置