gpt4 book ai didi

c++ - Boost线程不调用线程函数

转载 作者:太空宇宙 更新时间:2023-11-04 10:48:19 26 4
gpt4 key购买 nike

我正在尝试创建一个 boost 线程,我看到线程已创建,但控件没有到达线程函数。谁能解释一下为什么会这样?

使用的代码见下。

头文件.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;
};

`

测试.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 被编译/构建为库 (test.so),另一个主要函数调用 init 函数。我看到变量 m_collectDataThread 值从 null 变为某个值(线程被创建)并且 catch 也没有得到任何异常。

但我没有看到 collectData 中的任何语句被打印出来。为什么线程无法到达它?

最佳答案

也许尝试添加一个连接。

例如

 try 
{
m_CollectDataThread =
new boost::thread(boost::bind(&fanotify::clThreadMemFunc, this));
m_CollectDataThread->join();
}

关于c++ - Boost线程不调用线程函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32126725/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com