我正在使用 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
// ...
}
但 Xcode 给了我错误:“No matching function call to 'pthread_create'
“。我不知道,因为我已经包括了pthread.h
已经。
怎么了?
Thanks!
正如 Ken 所说,作为线程回调传递的函数必须是 (void*)(*)(void*) 类型函数。
您仍然可以将此函数包含为类函数,但必须将其声明为静态。您可能需要为每种线程类型(例如绘制)使用不同的线程。
例如:
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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)