xcode 没有对“pthread_create”的匹配函数调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10389384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No matching function call to 'pthread_create'
提问by qwertz
I'm using Xcode and C++ to make a simple game. The problem is the following code:
我正在使用 Xcode 和 C++ 来制作一个简单的游戏。问题是以下代码:
#include <pthread.h>
void *draw(void *pt) {
// ...
}
void *input(void *pt) {
// ....
}
void Game::create_threads(void) {
pthread_t draw_t, input_t;
pthread_create(&draw_t, NULL, &Game::draw, NULL); // Error
pthread_create(&input_t, NULL, &Game::draw, NULL); // Error
// ...
}
But Xcode gives me the error: "No matching function call to 'pthread_create'
". I haven't an idea 'cause of I've included pthread.h
already.
但是 Xcode 给了我错误:“ No matching function call to 'pthread_create'
”。我没有想法,因为我已经包括在内pthread.h
。
What's wrong?
怎么了?
Thanks!
谢谢!
回答by Tom
As Ken states, the function passed as the thread callback must be a (void*)(*)(void*) type function.
正如 Ken 所说,作为线程回调传递的函数必须是 (void*)(*)(void*) 类型的函数。
You can still include this function as a class function, but it must be declared static. You'll need a different one for each thread type (e.g. draw), potentially.
您仍然可以将此函数作为类函数包含在内,但必须将其声明为静态。您可能需要为每种线程类型(例如绘制)使用不同的线程。
For example:
例如:
class Game {
protected:
void draw(void);
static void* game_draw_thread_callback(void*);
};
// and in your .cpp file...
void Game::create_threads(void) {
// pass the Game instance as the thread callback's user data
pthread_create(&draw_t, NULL, Game::game_draw_thread_callback, this);
}
static void* Game::game_draw_thread_callback(void *game_ptr) {
// I'm a C programmer, sorry for the C cast.
Game * game = (Game*)game_ptr;
// run the method that does the actual drawing,
// but now, you're in a thread!
game->draw();
}
回答by Invictus
compilation of threads using pthread is done by providing options -pthread
.
Such as compiling abc.cpp would require you to compile like g++ -pthread abc.cpp
else would
give you an error like undefined reference to
pthread_create collect2: ld returned 1 exit status` . There must be some similar way to provide pthread option.
使用 pthread 编译线程是通过提供 options 来完成的-pthread
。比如编译 abc.cpp 会要求你像g++ -pthread abc.cpp
else 一样编译会给你一个错误,比如undefined reference to
pthread_create collect2: ld returned 1 exit status` 。必须有一些类似的方式来提供 pthread 选项。
回答by Ken Thomases
You're passing a member function pointer (i.e. &Game::draw
) where a pure function pointer is required. You need to make the function a class static function.
您正在传递一个成员函数指针(即&Game::draw
),其中需要一个纯函数指针。您需要使该函数成为类静态函数。
Edited to add: if you need to invoke member functions (which is likely) you need to make a class static function which interprets its parameter as a Game*
and then invoke member functions on that. Then, pass this
as the last parameter of pthread_create()
.
编辑添加:如果您需要调用成员函数(这很可能),您需要创建一个类静态函数,将其参数解释为 aGame*
然后调用成员函数。然后,this
作为 的最后一个参数传递pthread_create()
。