http://blog.csdn.net/lzx_bupt/article/details/6910597
在基础一上思考,如果线程调用的函数是在一个类中怎么办?答案是将该函数写成静态成员函数,如下模式就很符合C++的写作模式:
- #include <iostream>
- #include <pthread.h>
- using namespace std;
- #define NUM_THREADS 5
- class Hello
- {
- public:
- static void* say_hello(void* args)//除了多了static关键字,别无异样;
- {
- cout << "hello..." << endl;
- }
- };
- int main()
- {
- pthread_t tids[NUM_THREADS];
- for(int i = 0; i < NUM_THREADS; ++i)
- {
- int ret = pthread_create(&tids[i], NULL, Hello::say_hello, NULL);
- if (ret != 0)
- {
- cout << "pthread_create error: error_code=" << ret << endl;
- }
- }
- pthread_exit(NULL);
- }
原文链接: https://www.cnblogs.com/jackdong/archive/2012/09/04/2670193.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/61770
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!