如何在类中使用pthread_mutex及其功能?

问题描述

| 我已经搜索了许多小时以寻求解决方案,但是找不到简单的答案。 我有一个使用pthreads的类。实际的函数指针在该类中是静态的,并且我需要锁定一个互斥锁,因为到目前为止,我得到的结果是“怪异”(参数未正确传递)。 但是pthread_mutex_lock和unlock在给线程的函数中不起作用,因为它在静态成员函数中,但是我不能使函数非静态,因为它在类内部不起作用,并且我不能将其移到外部该课程,因为它将无法访问所需的信息。 以下代码应说明:
class Fight{

     pthread_mutex_t thread_mutex;
     static void *thread_run_fighter(void *temp);

  public:

    Fight();
    bool thread_round(Individual &a,int a_u,Individual &b,int b_u);
    std::vector<Individual> tournament();
  };
和cpp文件:
    Fight::Fight(){
       thread_mutex = PTHREAD_MUTEX_INITIALIZER;
    }

    bool Fight::thread_round(Individual &a,int b_u){

    if (a.saved and b.saved){
       a.uniform = a_u;
       b.uniform = b_u;
       Individual *one = &a;
       Individual *two = &b;      
       pthread_t a_thread,b_thread;
       int a_thread_id,b_thread_id;
       a_thread_id = pthread_create(&a_thread,NULL,Fight::thread_run_fighter,(void*) one);
       b_thread_id = pthread_create(&b_thread,(void*) two);

       pthread_join( a_thread,NULL);
       pthread_join( b_thread,NULL); 
       return true;
    }
    else{
       return false;
    }
   }

   void *Fight::thread_run_fighter(void *temp){

     Individual *indiv;
     indiv = (class Individual*)temp;
     pthread_mutex_lock( &thread_mutex );
     indiv->execute(indiv->uniform);
     pthread_mutex_unlock( &thread_mutex );

   }
如果有人能对此有所启发,我将非常感激。我已经被困了几个小时,而我却找不到任何信息。 谢谢!     

解决方法

通过“不起作用”,我假设您是说它不会编译,因为您正试图在
static
成员函数中使用实例成员。 但是更大的问题是,为什么要为此使用线程? 无论如何,您拥有的线程功能完全受到互斥锁的保护-通过简单地调用,您将获得相同(或更好)的性能
a.execute(a.uniform);
b.execute(b.uniform);
而不是旋转线程,然后等待它们完成。 但是,如果您真的想使用线程(也许您正在学习线程),并且希望静态成员函数能够处理实例成员,那么这里有一些指针。为了使它起作用,您需要以某种方式将Fight对象的实例传递给
static
线程函数:
// somewhere in `class Fight` definition:
//
// a structure that will let you pass a Fight* instance pointer
//  along with an Individual* to work on in the the
//  thread function

struct context {
    Fight* me;
    Individual* indiv;
};



// ...

// in Fight::thread_round():
//  package up data to pass to the thread function
context one = {this,&a };  // instead of Individual *one = &a;
context two = {this,&b };  // instead of Individual *two = &b;
最后,
Fight::thread_run_fighter()
void *Fight::thread_run_fighter(void *temp)
{
    // pull out the Fight object instance and the Individual
    //  object to work on
    context* ctx = (context*) temp;
    Individual *indiv = ctx->indiv;
    Fight* me = ctx->me;

    // do the work (in a pretty serialized fashion,unfortunately)
    pthread_mutex_lock( &me->thread_mutex );
    indiv->execute(indiv->uniform);
    pthread_mutex_unlock( &me->thread_mutex );

    return 0;
}
    ,我想问的第一个问题:您需要可移植的代码吗?如果是,则永远不要将C ++函数传递给pthread_create。原因:pthread_create的接口需要声明为extern \“ C \”的函数,并且您很幸运(由于使用x86 :)),静态成员方法适用于此,但不能保证在其他平台上也可以使用相同的方法。编译器。 第二,在互斥量创建之后,您已调用
thread_mutex = PTHREAD_MUTEX_INITIALIZER;
,据我记得标准所说,仅在初始化时才允许这样做。 最后,不允许使用带有静态方法的“ 9”,因为静态方法无法访问对象的非静态成员,因此您需要将指针传递给对象。您可以将
pthread_mutex_t thread_mutex;
void *thread_run_fighter(void *temp);
声明为全局变量,因为在这种情况下,这是最简单的方法。 还有一些注意事项:那么boost :: threads呢?我认为使用它而不是创建自己的解决方案会更好...     ,我认为您错过的只是使用
&indiv->thread_mutex
而不是省略
indiv->
对象。 编辑:请注意,使用静态类型转换可能比使用C风格的“ shot弹枪”类型转换更好:
Individual *indiv = static_cast<Individual*>(temp);
    ,在静态ѭ15的内部,您使用的是非静态
thread_mutex
。使用前必须先使用pthread_mutex_init创建thread_mutex。     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...