分享

作为pthread

 小数点2013 2013-02-11

很久以前已经说过,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:

  TCP/IP 指数增长和线性增长的编程实现

  关于 C++ 中的函数指针

  C#封装log4net

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

  如何使用ServletContextListener

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多