C++ C++;从';pthread_t';至';pthread_t*{aka long unsigned int}'';至';pthread\u t*
我是pthreads新手,试图让这段代码正常工作,但我在创建线程方面遇到了问题 我仍然无法理解如何使用pthread_create()函数创建一个pthread,该函数有一个struct对象(thread),并且我在退出线程时遇到问题。我正在尝试在我的main中创建3个线程,并将函数传递给正在使用向量进行一些工作的线程 我已经检查了这个问题并试图解决,但无法解决C++ C++;从';pthread_t';至';pthread_t*{aka long unsigned int}'';至';pthread\u t*,c++,pthreads,C++,Pthreads,我是pthreads新手,试图让这段代码正常工作,但我在创建线程方面遇到了问题 我仍然无法理解如何使用pthread_create()函数创建一个pthread,该函数有一个struct对象(thread),并且我在退出线程时遇到问题。我正在尝试在我的main中创建3个线程,并将函数传递给正在使用向量进行一些工作的线程 我已经检查了这个问题并试图解决,但无法解决 与其他pthread函数不同,它们通过值获取pthread\u t,pthread\u create获取pthread\u t* 你
与其他
pthread
函数不同,它们通过值获取pthread\u t
,pthread\u create
获取pthread\u t*
你需要像这样的东西:
int errc;
pthread_t mainThread; /*...*/
if ( 0!=(errc=pthread_create(&mainThread, ...)))
throw errno_exception(errc); // { errno=errc; perror(0); /*...*/}
您需要更改
agency1
和agency2
的签名,以匹配函数类型pthread\u create
accepts
void* agency1(void*) { ... }
void* agency2(void*) { ... }
另外,转换错误是因为pthread\u create
接收到指向pthread\u t
的指针,并且您只传递了该类型的值,需要将其更改为:
at1 = pthread_create(&aT->agencyTread1, NULL, agency1, NULL);
at2 = pthread_create(&aT->agencyTread2, NULL, agency2, NULL);
mt = pthread_create(&mainThread, NULL, mainT, (void *)aT);
请以实际文本而不是文本图像的形式发布错误。另外,为什么要使用
malloc
而不是new
?有什么特殊的原因吗?既然你把答案编辑到你的问题中,那么给出的答案就没有意义了。你把这个OP标记为C++。您不使用std::thread有什么原因吗?std::thread比Posix线程方便得多。@I0veisreal peror在stdio.h
中(或在cstdio
中作为std::peror
)man perror
会告诉你的。对于errno
,您还需要errno.h
,或者沿着异常路径(对C++来说更为惯用)。添加它们后仍然有相同的错误,即使我这样做了,如果(mt){可以,我设法让您的工作,但仍然有分段错误