今天突然想研究一下pthread,以下是今天的成果
sleep.cpp |
#include #include #include using namespace std; class db { private: string a; public: db() { a="abc"; } ~db() { a=""; } void showme() { cout< } }; struct a { int b; db *pdb; }; void * search(void *arg) { struct a * mydata; mydata=(struct a *)arg; sleep(10); mydata->pdb->showme(); pthread_exit(0); } int main() { struct a b; b.pdb=new db(); b.b=10; pthread_t helper; char c[]="abc"; pthread_create(&helper,NULL,search,(void *) &b); sleep(20); cout<<"ok"< return 0; } |
编译:g++ -lpthread sleep.cpp -o sleep.exe
同样在cygwin下测试.效果还可以.
我在想thread和父进程之间如何通讯呢?
阅读(1501) | 评论(0) | 转发(0) |