Linux多线程实践(2) --线程基本API
POSIX線程庫
? 與線程有關(guān)的函數(shù)構(gòu)成了一個完整的系列,絕大多數(shù)函數(shù)的名字都是以“pthread_”開頭,要使用這些函數(shù)庫,要通過引入頭文<pthread.h>,而且鏈接這些線程函數(shù)庫時要使用編譯器命令的“-lpthread”選項[Ubuntu系列系統(tǒng)需要添加的是”-pthread”選項而不是”-lpthread”,如Ubuntu?14.04版本,深度Ubuntu等]
?
1.pthread_create
int pthread_create(pthread_t *restrict thread,const pthread_attr_t *restrict attr,void *(*start_routine)(void*), void *restrict arg);創(chuàng)建一個新的線程
參數(shù)
? thread:線程ID
??attr:設(shè)置線程的屬性,一般設(shè)置為NULL表示使用默認(rèn)屬性
??start_routine:是個函數(shù)地址,線程啟動后要執(zhí)行的函數(shù)
??arg:傳給線程啟動函數(shù)的參數(shù)
返回值:成功返回0;失敗返回錯誤碼;
?
附-Posix錯誤檢查
??UNIX傳統(tǒng)的函數(shù):成功返回0,失敗返回-1,并且對設(shè)置全局變量errno以指定錯誤類型。然而pthreads函數(shù)出錯時不會設(shè)置全局變量errno(而其他的大部分POSIX函數(shù)會設(shè)置errno)。而是將錯誤代碼通過返回值返回;
??pthreads同樣也提供了線程內(nèi)的errno變量,對于每一個線程,?都有一個errno的值,?以支持其它使用errno的代碼。對于pthreads函數(shù)的錯誤,建議通過返回值進(jìn)行判定,因為讀取返回值要比讀取線程內(nèi)的errno變量的開銷更小!
/** 實踐: 新的錯誤檢查與錯誤退出函數(shù) **/ inline void err_check(const std::string &msg, int retno) {if (retno != 0)err_exit(msg, retno); } inline void err_exit(const std::string &msg, int retno) {std::cerr << msg << ": " << strerror(retno) << endl;exit(EXIT_FAILURE); }2.pthread_exit
void pthread_exit(void *value_ptr);線程終止
? value_ptr:指向該線程的返回值;注意:value_ptr不能指向一個局部變量。
?
3.pthread_join
int pthread_join(pthread_t thread, void **value_ptr);等待線程結(jié)束
??value_ptr:它指向一個指針,后者指向線程的返回值(用戶獲取線程的返回值)
/** 示例: 等待線程退出 **/ void *thread_rotine(void *args) {for (int i = 0; i < 10; ++i){printf("B");fflush(stdout);usleep(20);}pthread_exit(NULL); }int main() {pthread_t thread;int ret = pthread_create(&thread, NULL, thread_rotine, NULL);err_check("pthread_create", ret);for (int i = 0; i < 10; ++i){printf("A");fflush(stdout);usleep(20);}ret = pthread_join(thread, NULL);err_check("pthread_join", ret);putchar('\n');return 0; }4.pthread_self
pthread_t pthread_self(void);返回線程ID
/** 示例:主控線程與子線程傳遞數(shù)據(jù) **/ typedef struct _Student {char name[20];unsigned int age; } Student;void *threadFunction(void *args) {cout << "In Thread: " << pthread_self() << endl;Student tmp = *(Student *)(args);cout << "Name: " << tmp.name << endl;cout << "Age: " << tmp.age << endl;pthread_exit(NULL); }int main() {Student student = {"xiaofang",22};pthread_t thread;//啟動創(chuàng)建并啟動線程pthread_create(&thread,NULL,threadFunction,&student);//等待線程結(jié)束pthread_join(thread,NULL);return 0; }5.pthread_cancel
int pthread_cancel(pthread_t thread);取消一個執(zhí)行中的線程
6.pthread_detach
int pthread_detach(pthread_t thread);??將一個線程分離-如果在新創(chuàng)建的線程結(jié)束時主線程沒有結(jié)束同時也沒有調(diào)用pthread_join,則會產(chǎn)生僵線程,次問題可以通過設(shè)置線程為分離的(detach)來解決;
?
總結(jié):進(jìn)程 VS.?線程
進(jìn)程(pid_t) | 線程(pthread_t) |
Fork | Pthread_create |
Waitpit | Pthread_join/Pthread_detach |
Kill | Pthread_cancel |
Pid | Pthead_self |
Exit/return | Pthread_exit/return |
僵尸進(jìn)程(沒有調(diào)用wait/waitpid等函數(shù)) | 僵尸線程(沒有調(diào)用pthread_join/pthread_detach) |
其完整源代碼:download.csdn.net/detail/hanqing280441589/8440763
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎!定制產(chǎn)品紅包拿不停!
總結(jié)
以上是生活随笔為你收集整理的Linux多线程实践(2) --线程基本API的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 利用cx_Freeze将py文件打包成e
- 下一篇: Pycharm导入anaconda环境