2012-11-16

C++成员函数作为pthread_create参数

Views: 12059 | Add Comments

很久以前已经说过, C++指向类成员函数的指针非常变态, 如果要把类成员函数作为线程 pthread_create 的参数, 就更复杂!

class A{
public:
    void run(){
    }

    static void *run_helper(void *arg){
        ((A *)arg)->run();
        return (void *)NULL;
    }
};

A a;
pthread_t t;
pthread_create(&t, NULL, &A::run_helper, &a);

本来我们希望把 a.run 作为参数, 为此, 必须创建一个 static 的 run_helper() 函数, 然后在 run_helper() 中调用 run().

Related posts:

  1. TCP/IP 指数增长和线性增长的编程实现
  2. C++ const& 的坑
  3. 关于 C++ 中的函数指针
  4. C#封装log4net
  5. 使用ServletContextListener在服务器启动和关闭时创建和关闭缓存
Posted by ideawu at 2012-11-16 11:02:04

Leave a Comment