为什么在C++使用pthread_cre…
2017-06-29 10:13
302 查看
原文地址:为什么在C++使用pthread_create()的时候,类成员函数做线程的处理函数必须要定义成static类型的?作者:蓝月
今天在进行多线程编程的时候遇到了一个编译问题: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;
}
今天在进行多线程编程的时候遇到了一个编译问题: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;
}
相关文章推荐
- 为什么在C++使用pthread_create()的时候,类成员函数做线程的处理函数必须要定义成static类型的?
- 为什么在C++使用pthread_create()的时候,类成员函数做线程的处理函数必须要定义成static类型的?
- 为什么最好在c++中使用异常
- 经典的c++下面使用pthread_create问题的解决
- C++为什么不建议使用malloc,calloc,realloc来分配内存?
- C++拷贝构造函数的参数为什么必须使用引用类型
- C++的类中使用pthread_create()
- 为什么要同时使用和&符号(c++) 转载
- 为什么在C++中对赋值号“=”的重载只能使用成员函数而不可以使用友元函数?求高手、大神帮我解答!
- 为什么Android开发使用Java而不是C++?
- 为什么Android开发使用Java而不是C++?
- 为什么要学习(并使用)C++
- 为什么使用C++
- C++学习总结_为什么要使用指针
- 为什么最好在c++中使用异常
- 为什么使用googletest,而不是其他C++测试框架(译自google faq的该问题)
- 为什么使用C++
- C++中使用pthread的策略
- c与c++在使用pthread_create创建线程时的区别
- C++为什么不提倡使用scanf和printf函数