Linux C/C++多线程pthread实例
Linux首先需要安裝GCC/G++編譯環(huán)境,方法本文從略,然后建個(gè)test.c或test.cpp文件。
本文測(cè)試系統(tǒng)Ubuntu 11.10。GCC編譯器截至發(fā)文日期止最新的版本,現(xiàn)在不在辦公室下次補(bǔ)上。
1、pthread_create函數(shù)定義
int pthread_create(pthread_t *restrict tidp,const pthread_attr_t *restrict attr,void *(*start_rtn)(void), void *restrict arg);
參數(shù)1:指向線程標(biāo)識(shí)符指針。
參數(shù)2:線程屬性。
參數(shù)3:線程運(yùn)行函數(shù)起始地址。
參數(shù)4:運(yùn)行函數(shù)的參數(shù)。
創(chuàng)建線程成功時(shí),函數(shù)返回0,若不為0則說明創(chuàng)建線程失敗,常見的錯(cuò)誤返回代碼為EAGAIN和EINVAL。前者表示系統(tǒng)限制創(chuàng)建新的線程,例如線程數(shù)目過多了;后者表示第二個(gè)參數(shù)代表的線程屬性值非法。
2、源碼包含pthread頭文件
include <pthread.h>
3、創(chuàng)建多線程示例程序C格式
#include <pthread.h> #include <stdio.h> #include <stdlib.h>pthread_t ntid;void *fnThreadFun(void *para){//... return ((void *)0); }int main(){int err;err = pthread_create(&ntid,NULL,fnThreadFun,NULL); if(err != 0){ printf("can't create thread: %s\n",strerror(err)); return 1; } sleep(1); return 0; }4、創(chuàng)建多線程示例程序C++格式
ctest.h
#include <pthread.h>class ctest{public:ctest();~ctest();private:void createthread();};ctest.cpp
ctest::ctest(){} ctest::~ctest(){} void* fnThreadFun(void *para){ //... return ((void *)0); }void ctest::createthread() { pthread_t ntid; err = pthread_create(&ntid,NULL,fnThreadFun,NULL); if(err != 0){ printf("can't create thread: %s\n",strerror(err)); return 1; } }test.cpp
#include ctest.hint main(){ctest tst;tst.createthread();while(1){//...sleep(1);} return 0;}另外,也可以把線程函數(shù)設(shè)計(jì)到類中,但是必須聲明為static類型,天緣認(rèn)為完全沒這個(gè)必要,因?yàn)閟tatic類型函數(shù)在編譯時(shí)仍然是先分配全局地址,反倒直接用全局似乎看起來更規(guī)整,就是注意點(diǎn),把函數(shù)名取好就可以了。
4、編譯執(zhí)行多線程程序
編譯上述多線程程序,必須使用 -lpthread編譯選項(xiàng),因?yàn)閜thread庫不是Linux默認(rèn)鏈接庫,鏈接時(shí)必須指定使用libpthread.a庫(天緣機(jī)子ubuntu11.10這些庫在/usr/lib/i386-linux-gnu路徑下),在編譯選項(xiàng)中需添加-lpthread參數(shù),示例如:
C編譯選項(xiàng):
>gcc test.c -o test -lpthread
C++編譯選項(xiàng):
>g++ ctest.cpp test.cpp -o test -lpthread
如果是寫到MAKEFILE中,可以找到類似TARG_OPTIONS=這樣的位置添加-lpthread。
但是往往還是會(huì)報(bào)告pthread_create未聲明問題,說明編譯器仍未找到libpthead.a的位置,這時(shí)可手動(dòng)在編譯命令行中添加:-L./usr/lib/i386-linux-gnu 選項(xiàng)(這里的路徑是libthread.a路徑,不同系統(tǒng)、機(jī)子可能有所不同!!)。
執(zhí)行:
>./test
5、pthread注意事項(xiàng)
注意,pthread_create第三個(gè)參數(shù),也就是線程回調(diào)函數(shù)格式為:
void* fnThreadFun(void* param)
{
? return NULL;//或return ((void *)0);
}
其返回值為void*型指針,如果寫成void fnThreadFun(void* param)形式,那么編譯會(huì)報(bào)告:
error: invalid conversion from ‘void (*)(void*)’ to ‘void* (*)(void*)’ [-fpermissive]
錯(cuò)誤。
寫成:
err = pthread_create(&ntid,NULL,(void*)&fnThreadFun,NULL);
樣式似乎也不行,gcc編譯時(shí)不會(huì)出錯(cuò),但是用g++就會(huì)有問題(也會(huì)報(bào)告上面錯(cuò)誤。),究其原因就是C語言編譯器允許隱含性的將一個(gè)通用指針轉(zhuǎn)換為任意類型的指針,而C++不允許(http://www.4ucode.com/Study/Topic/1353180)。
參考地址:
http://zhuwenlong.blog.51cto.com/209020/40339
線程互斥和解鎖請(qǐng)參考:
http://www.ibm.com/developerworks/cn/linux/l-cn-mthreadps/index.html
http://hi.baidu.com/see_yee/blog/item/139fe0243198ad34c8955917.html
http://en.wikipedia.org/wiki/POSIX_Threads
總結(jié)
以上是生活随笔為你收集整理的Linux C/C++多线程pthread实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: GCC + pthread
- 下一篇: gcc/g++ 链接库的编译与链接