我使用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); //错误 pthread_create(& input_t,NULL,& Game :: draw,NULL); //错误 // ... }但Xcode给了我错误:没有匹配的函数调用'pthread_create'。我不知道因为我已经包括 pthread.h 。
出了什么问题? / p>
谢谢!
解决方案线程回调必须是一个(void *)(*)(void *)类型的函数。
你仍然可以包含这个函数作为类函数,静态的。
例如:
class Game { protected: void draw(void); static void * game_draw_thread_callback(void *); }; //并在您的.cpp文件中... void Game :: create_threads(void){ //将Game实例作为线程回调用户数据 pthread_create(&draw_t,NULL,Game :: game_draw_thread_callback,this); } static void * Game :: game_draw_thread_callback(void * game_ptr){ //我是一个C程序员,对不起C的演员。 Game * game =(Game *)game_ptr; //运行实际绘图的方法, //但是现在,你在一个线程! game-> draw(); }
I'm using Xcode and C++ to make a simple game. The problem is the following code:
#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.
What's wrong?
Thanks!
解决方案As Ken states, the function passed as the thread callback must be a (void*)(*)(void*) type function.
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(); }
更多推荐
没有匹配的函数调用'pthread
发布评论