C++使用pthread_create()
来源:互联网 发布:ug倒角编程 编辑:程序博客网 时间:2024/05/20 23:04
本文章最需要注意的地方:C++中类成员函数的声明与实现写法不同,一定要注意。
今天在进行多线程编程的时候遇到了一个编译问题:error: argument of type ‘void (PIAMW::Communicator::)()’ does not
match ‘void* (*)(void*)’
后来发现将线程处理函数声明为static类型,问题得解。
其实这个原因很简单,当把线程函数封装在类中,this指针会作为默认的参数被传进函数中,从而和线程函数参数(void*)
不能匹配,不能通过编译。怎么解决呢?网上有一个解决办法,引用过来,自己记着。
摘自:http://hi.chinaunix.net/?uid-11770217-action-viewspace-itemid-48886
将线程函数作为静态函数,因为在C++中静态函数没有this指针(即在内存中静态函数和普通全局函数几乎没有什么区别),故可以
匹配编译通过, 但是当线程函数要访问私有变量呢?可以访问到吗?答案是不可以!
解决方案: 将this指针作为参数传递给静态函数,这样可以通过该this指针访问所有的私有变量, 但是我要是还需要向静态函数
中传递我自己需要的参数呢?
答案是:将this指针和需要的参数作为一个结构体一起传给静态函数,请看下面代码:
#include <iostream>
#include "pthread.h"
using namespace std;
class A;
struct ARG
{
A* pThis;
string var;
};
class A
{
public:
A();
~A();
static void* thread(void* args);
void excute();
private:
int iCount;
};
A::A()
{
iCount = 10;
}
A::~A()
{
}
void* A::thread(void* args) //看到了吗,与声明写的不一样,一定要注意哦
{
ARG *arg = (ARG*)args;
A* pThis = arg->pThis;
string var = arg->var;
cout<<"传入进来的参数var: "<<var<<endl;
cout<<"用static线程函数调用私有变量: "<<pThis->iCount<<endl;
}
void A::excute()
{
int error;
pthread_t thread_id;
ARG *arg = new ARG();
arg->pThis = this;
arg->var = "abc";
error = pthread_create(&thread_id, NULL, thread, (void*)arg);
if (error == 0)
{
cout<<"线程创建成功"<<endl;
pthread_join(thread_id, NULL);
}
}
int main()
{
A a;
a.excute();
return 0;
}
match ‘void* (*)(void*)’
后来发现将线程处理函数声明为static类型,问题得解。
其实这个原因很简单,当把线程函数封装在类中,this指针会作为默认的参数被传进函数中,从而和线程函数参数(void*)
不能匹配,不能通过编译。怎么解决呢?网上有一个解决办法,引用过来,自己记着。
摘自:http://hi.chinaunix.net/?uid-11770217-action-viewspace-itemid-48886
将线程函数作为静态函数,因为在C++中静态函数没有this指针(即在内存中静态函数和普通全局函数几乎没有什么区别),故可以
匹配编译通过, 但是当线程函数要访问私有变量呢?可以访问到吗?答案是不可以!
解决方案: 将this指针作为参数传递给静态函数,这样可以通过该this指针访问所有的私有变量, 但是我要是还需要向静态函数
中传递我自己需要的参数呢?
答案是:将this指针和需要的参数作为一个结构体一起传给静态函数,请看下面代码:
#include <iostream>
#include "pthread.h"
using namespace std;
class A;
struct ARG
{
A* pThis;
string var;
};
class A
{
public:
A();
~A();
static void* thread(void* args);
void excute();
private:
int iCount;
};
A::A()
{
iCount = 10;
}
A::~A()
{
}
void* A::thread(void* args) //看到了吗,与声明写的不一样,一定要注意哦
{
ARG *arg = (ARG*)args;
A* pThis = arg->pThis;
string var = arg->var;
cout<<"传入进来的参数var: "<<var<<endl;
cout<<"用static线程函数调用私有变量: "<<pThis->iCount<<endl;
}
void A::excute()
{
int error;
pthread_t thread_id;
ARG *arg = new ARG();
arg->pThis = this;
arg->var = "abc";
error = pthread_create(&thread_id, NULL, thread, (void*)arg);
if (error == 0)
{
cout<<"线程创建成功"<<endl;
pthread_join(thread_id, NULL);
}
}
int main()
{
A a;
a.excute();
return 0;
}
0 0
- C++中使用pthread_create
- pthread_create()在C和C++使用区别
- pthread_create用法 c线程
- pthread_create函数使用
- C++使用pthread_create()
- pthread_create和pthread_join使用
- pthread_create()的基本使用
- gcc pthread_create()方法使用
- Xcode c c++ 多线程 pthread_create
- linux c/c++ undefined reference to 'pthread_create' 未定义pthread_create问题
- c与c++在使用pthread_create创建线程时的区别
- c++test使用qt中的 mingw 创建pthread_create程序无法运行的问题
- linux c之使用pthread_create创建线程pthread_join等待线程和pthread_exit终止线程总结
- pthread_create
- pthread_create
- pthread_create
- pthread_create
- pthread_create()
- Git的安装的常见问题
- Java递归算法构造JSON树形结构
- JDBC编程
- C++中的动态绑定问题
- The WebGL Globe
- C++使用pthread_create()
- 常见的POS打印机分类
- 协议:kerboros,Yahalom
- Cody_Yu 通用的DAO查询,常见方法集合
- HDU 1087 Super Jumping! Jumping! Jumping!
- UVA - 11093 Just Finish it up 环路
- Java EE7和Maven工程入门(5)
- java 远程调试jar程序
- Linux7企业级运维高端培训视频教程