作为pthread_create参数

2013-02-08 15:12:09 · 作者: · 浏览: 591

  很久以前已经说过,C++(www.cppentry.com)指向类成员函数的指针非常变态, 如果要把类成员函数作为线程 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:

  TCP/IP 指数增长和线性增长的编程(www.cppentry.com)实现

  关于 C++(www.cppentry.com) 中的函数指针

  C#封装log4net

  使用ServletContextListener在服务器启动和关闭时创建和关闭缓存

  如何使用ServletContextListener