c – 没有匹配函数调用’pthread_create’
发布时间:2020-12-14 18:08:25 所属栏目:百科 来源:网络整理
导读:我正在使用 Xcode和C来制作一个简单的游戏. 问题是以下代码: #include pthread.hvoid *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,NUL
我正在使用
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); // Error // ... } 但是Xcode给了我错误:“没有匹配函数调用’pthread_create’”.我不知道’因为我已经包含了pthread.h. 怎么了? 谢谢! 解决方法
正如Ken所说,作为线程回调传递的函数必须是(void *)(*)(void *)类型函数.
您仍然可以将此函数作为类函数包含在内,但必须将其声明为static.您可能需要为每种线程类型(例如绘图)使用不同的一种. 例如: 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,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(); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |