关于在c++的类中使用线程的问题

来源:互联网 发布:mac版flash播放器下载 编辑:程序博客网 时间:2024/04/29 23:52

一个简单的例子
class test
{
      int x;
public:
      test(int y):x(y){};
      void Do()
      {
              cout<<x<<endl;
      }
      static unsigned int __stdcall run(void* pv)
      {
               test* p = (test*)pv;
               p->Do();
               return 0;
      }
};
int main(int argc, char* argv[])
{
         test t(100);
         ::_beginthreadex(0,0,test::run ,  &t , 0,0);
         Sleep(1000);

         return 0;
}
run必须是静态成员函数
因为线程函数,在本质上需一个全局的概念,而在面向对象的编程中
静态成员能够的执行需求