Boost线程不调用线程函数
Boost thread not calling thread function
我正在尝试创建一个 boost 线程,我看到线程已创建但控件没有到达线程函数。谁能解释一下为什么会这样?
使用的代码见下方
header_file.h
class fa {
public:
fa();
~fa();
int init(void);
static void* clThreadMemFunc(void *arg) { return ((fa*)arg)->collectData((fa*)arg); }
void* collectData(fa *f );
private:
int m_data;
boost::thread *m_CollectDataThread;
};
`
test.cpp
int fa::init(void)
{
if (m_CollectDataThread== NULL)
{
printf("New Thread...\n");
try
{
m_CollectDataThread =
new boost::thread((boost::bind(&fanotify::clThreadMemFunc, this)));
}
catch (...){perror("Thread error ");}
printf("m_CollectDataThread: %p \n", m_CollectDataThread);
}
return 0;
}
void* fa::collectData(fa *f)
{
printf("In collectData\n");
int data = f->m_data;
printf("data %d",data);
}
test.cpp
是complied/built作为一个库(test.so
)和另一个主函数调用init
函数。我看到变量 m_collectDataThread
值从 null 变为某个值(创建线程)并且 catch 也没有任何异常。
但我没有看到 collectData
中的任何语句被打印出来。为什么线程无法访问它?
或许尝试添加一个连接。
例如
try
{
m_CollectDataThread =
new boost::thread(boost::bind(&fanotify::clThreadMemFunc, this));
m_CollectDataThread->join();
}
当您使用 boost::thread
或 std::thread
时,您不需要传递线程函数的旧方法(使用静态方法和转换 void *
指针),您可以调用 class 方法直接:
class fa {
public:
fa();
~fa();
int init(void);
void collectData();
private:
int m_data;
boost::thread *m_CollectDataThread;
};
m_CollectDataThread = new boost::thread( &fa::collectData, this );
// or explicitly using bind
m_CollectDataThread = new boost::thread( boost::bind( &fa::collectData, this ) );
boost 是 C++ 库,不是 C
我正在尝试创建一个 boost 线程,我看到线程已创建但控件没有到达线程函数。谁能解释一下为什么会这样?
使用的代码见下方
header_file.h
class fa {
public:
fa();
~fa();
int init(void);
static void* clThreadMemFunc(void *arg) { return ((fa*)arg)->collectData((fa*)arg); }
void* collectData(fa *f );
private:
int m_data;
boost::thread *m_CollectDataThread;
};
`
test.cpp
int fa::init(void)
{
if (m_CollectDataThread== NULL)
{
printf("New Thread...\n");
try
{
m_CollectDataThread =
new boost::thread((boost::bind(&fanotify::clThreadMemFunc, this)));
}
catch (...){perror("Thread error ");}
printf("m_CollectDataThread: %p \n", m_CollectDataThread);
}
return 0;
}
void* fa::collectData(fa *f)
{
printf("In collectData\n");
int data = f->m_data;
printf("data %d",data);
}
test.cpp
是complied/built作为一个库(test.so
)和另一个主函数调用init
函数。我看到变量 m_collectDataThread
值从 null 变为某个值(创建线程)并且 catch 也没有任何异常。
但我没有看到 collectData
中的任何语句被打印出来。为什么线程无法访问它?
或许尝试添加一个连接。
例如
try
{
m_CollectDataThread =
new boost::thread(boost::bind(&fanotify::clThreadMemFunc, this));
m_CollectDataThread->join();
}
当您使用 boost::thread
或 std::thread
时,您不需要传递线程函数的旧方法(使用静态方法和转换 void *
指针),您可以调用 class 方法直接:
class fa {
public:
fa();
~fa();
int init(void);
void collectData();
private:
int m_data;
boost::thread *m_CollectDataThread;
};
m_CollectDataThread = new boost::thread( &fa::collectData, this );
// or explicitly using bind
m_CollectDataThread = new boost::thread( boost::bind( &fa::collectData, this ) );
boost 是 C++ 库,不是 C