C pthread不起作用
c++ pthread not working
我有一个创建线程的classa,我希望线程运行直到将变量设置为false。
。我创建类似的线程:
ClassA::ClassA():
m_bContinue(true),
{
pthread_mutex_init(&m_mutex, NULL);
pthread_create(&m_thWorkThread, NULL, &ClassA::ThreadProc, this);
}
我想要线程运行长,只要pclassa->继续()返回true。
void* ClassA::ThreadProc(void *p) //ThreadProc defined as static member function
{
ClassA *pClassA = reinterpret_cast<ClassA*>(p);
if(pClassA != NULL)
{
while(pClassA->Continue())
{
printf("in the while n ");
}
}
else
printf("pClassA null n");
}
继续返回M_BContinue,该M_BContinue设置为构造器中的true。
bool ClassA::Continue()
{
return bContinue;
}
当我运行它时,它只会在循环一次时进入并打印" while"并停止程序。当我进行街头时,我看到了Sigsegv 杀死的消息 。当我更改时循环时:
while(1){}
它正常工作。我想念什么?
您不能使用pthread_create
启动成员功能。而是使用正常功能,将this
传递给它,然后调用所需的功能:
void *ThreadProc (void *p)
{
reinterpret_cast<ClassA*>(p)->ThreadProc (p);
return 0;
}
...
pthread_create(&m_thWorkThread, NULL, &ThreadProc, this);
或,您可以使用允许启动类成员功能的C 11及其std::thread
。
类型A的对象的寿命比线程短?似乎对象死亡太早了。使用while(1)
,您不再引用A。
快速问题。pthread_join在哪里。希望您还没有错过它。只是好奇。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- C pthread不起作用
- pthread和信号量在osx maverick 10.9中对我不起作用
- C++ pthread连接有时不起作用
- 带有结构参数的pthread不起作用