抱歉,如果我没有做到应有的描述性,我在打字时就快睡着了。我正在尝试在 mingw 上使用 g++ 编译的 c++ 代码中创建一个 posix 线程。这是我尝试编译的代码的异常(exception)
static void processNextNodeOnQueue(queue<TreeNode*> &toComputeQueue) {...}
void static processNodes(void* ptr) {
pair<queue<TreeNode*>*, bool*> *input = (pair<queue<TreeNode*>*, bool*>*) ptr;
while(*(input->second)) {
pthread_mutex_lock(&mutex1);
if(input->first->empty()) return;
pthread_mutex_unlock(&mutex1);
processNextNodeOnQueue(*(input->first));
}
}
void startThinking() {
thinking = true;
mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_t thread;
pair<queue<TreeNode*>*, bool*> *input = new pair<queue<TreeNode*>*, bool*>(&toComputeQueue, &thinking);
int thereadId = pthread_create(&thread, NULL, (void*)&processNodes, (void*) input );
delete input;
}
void stopThinking() {
//set thinking to false and wait for threads to wrap up
thinking = false;
}
void processForTime(double seconds) {
clock_t t;
int f;
t = clock();
startThinking();
//something to wait for an amount of time
stopThinking();
}
这是完整的编译器输出,以防我遗漏了某些内容
C:\Users\Maxwell\SkyDrive\RPI\Fall 2013\IHSS-1964\Connect 4\MaxAI>g++ -g -pthread -std=c++11 -o3 *.cpp -o Max.exe
In file included from unit_tests.h:5:0,
from main.cpp:11:
search_tree.h: In member function 'void BoardSearchTree::startThinking()':
search_tree.h:221:85: error: invalid conversion from 'void*' to 'void* (__attribute__((__cdecl__)) *)(void*)' [-fpermissive]
int thereadId = pthread_create(&thread, NULL, (void*)&processNodes, (void*) input );
^
In file included from search_tree.h:12:0,
from unit_tests.h:5,
from main.cpp:11:
c:\mingw\include\pthread.h:940:31: error: initializing argument 3 of 'int pthread_create(pthread_t*, pthread_attr_t_*const*, void* (__attribute__((__cdecl__)) *)(void*), void*)' [-fpermissive] PTW32_DLLPORT int PTW32_CDECL pthread_create (pthread_t * tid,
最佳答案
第三个参数为pthread_create
是 void *(*start_routine) (void *)
- 函数指针。您正在将该函数转换为 (void*)
这是不必要的也是不正确的。只需删除类型转换即可:
pthread_create(&thread, NULL, processNodes, (void*) input );
也是从input
开始是一个指针,您也不需要对其进行强制转换,所有指针(保存指向成员的指针)都可以隐式转换为 void*
):
pthread_create(&thread, NULL, processNodes, input );
关于c++ - 尝试创建 posix 线程并获得从 'void*' 到 'void* (__attribute__((__cdecl__)) *)(void*) 错误的无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20366806/