c++ - 尝试创建 posix 线程并获得从 'void*' 到 'void* (__attribute__((__cdecl__)) *)(void*) 错误的无效转换

标签 c++ multithreading posix

抱歉,如果我没有做到应有的描述性,我在打字时就快睡着了。我正在尝试在 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_createvoid *(*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/

相关文章:

multithreading - 如何使用安全异常库捕获异步异常?

c++ - 调用 posix_spawn 时关闭所有文件句柄

c++ - 编写 shell - 如何确定不同 exec 调用的可执行路径和文件名

c++ - 将 char 数组连接在一起

c++ - C++自动参数的推导规则

c++ - 不明确的 string::operator= 调用隐式转换为 int 和 string 的类型

java - Java API调用将根据队列大小进行合并,或者在最早的项目插入队列后的4秒内执行

c++ - 这行代码通过bfs在二分图算法中做了什么?

c++ - 如何在不同线程中调用同一类的多个对象的方法?

c - pthread_cond_signal 在调用线程退出之前未到达另一个线程