对不起,如果我不像我应该那样描述,我几乎睡着了。我试图在我的c ++代码中用m ++编译g ++来创建一个posix线程。这是我正在尝试编译的代码的除外
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,
答案 0 :(得分:1)
pthread_create
的第三个参数是void *(*start_routine) (void *)
- 一个函数指针。您正在将函数转换为(void*)
,这既不必要又不正确。只需删除演员:
pthread_create(&thread, NULL, processNodes, (void*) input );
此外,由于input
是一个指针,您不需要将其强制转换,所有指针(保存指向成员的指针)都可以隐式转换为void*
):
pthread_create(&thread, NULL, processNodes, input );
答案 1 :(得分:0)
假设您正在使用here中的pthread-w32
库,则以下标题信息是相关的:
#define PTW32_CDECL __cdecl
PTW32_DLLPORT int PTW32_CDECL pthread_create (pthread_t * tid,
const pthread_attr_t * attr,
void *(PTW32_CDECL *start) (void *),
void *arg);
这表明start
参数应该是使用__cdecl
calling convention的函数指针。
尝试将processNodes
函数原型更改为以下内容:
static void PTW32_CDECL processNodes(void* ptr)
然后编译器应该使用pthread_create
所期望的正确调用约定来创建线程函数。