没有匹配的函数调用'pthread

编程入门 行业动态 更新时间:2024-10-28 15:34:27
本文介绍了没有匹配的函数调用'pthread_create'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用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

本文发布于:2023-10-09 02:05:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474401.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   pthread

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!