返回类型Pthread使用C++创建
Return Type Pthread Create Using C++
我正在尝试创建一个函数,该函数返回具有特定类型的对象。问题是创建线程不接受它。有人能帮我写下面的代码吗?
struct thread_args
{
Key *k;
QNode *q;
uint8_t USED_DIMENSION;
};
QLeafNode *st ;
struct thread_args Structthread2;
Structthread1.k=min;
Structthread1.q=start;
Structthread1.USED_DIMENSION=4 ;
pthread_create( &thread1, NULL,(void*)&FindLeafNode, ((void *) &Structthread1));
pthread_join( thread1, (void**)st);
QLeafNode* FindLeafNode (Key *k, QNode * r, uint8_t USED_DIMENSION ){
}
首先,您的线程函数定义不正确。只有表单的功能:
void* function_name(void* param)
可以传递给CCD_ 1。
现在,为了从这个函数返回指向某个东西的指针,您需要两个pthread函数:
pthread_exit(void *value_ptr);
在线程函数内部调用此函数以通过value_ptr
和返回值
pthread_join(pthread_t thread, void **value_ptr);
在父线程内部调用此函数以等待句柄为thread
的子线程的终止,并在value_ptr
中检索pthread_exit
返回的值。
所以你的代码应该看起来像:
struct thread_args
{
Key *k;
QNode *q;
uint8_t USED_DIMENSION;
};
QLeafNode *st ;
struct thread_args Structthread1;
Structthread1.k=min;
Structthread1.q=start;
Structthread1.USED_DIMENSION=4 ;
pthread_create(&thread1, NULL, FindLeafNode, ((void *) &Structthread1));
pthread_join(thread1, (void**)st);
...
void* FindLeafNode (void* param) {
struct thread_args* value = (struct thread_args*) param;
// use value for computations
QLeafNode* result = ... // allocate result with new / malloc
pthread_exit((void*)result);
}
返回一个堆分配的指针,指向[所以使用new
来分配它]您的新对象(在线程中分配,稍后可能在加入它的"父"线程中释放,即使用结果)。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 链接到自行创建的dll失败
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++类内存结构中创建"spacer"?
- 终端不会为C++文件创建.exe文件吗
- 在createdialog创建的窗口中捕获用于编辑控件的OnMouseMove消息