请问 我用CreateThread 创建个线程,这个线程返回一个数值,我怎么样才能在主线程中得到这个数值呢?
我通过传递一个指针参数给CreateThread 在线程中能正确给这个参数赋值,但是在主函数就得不到这个改变的值,是什么问题呢?

解决方案 »

  1.   

    线程函数的返回值一般用:
    BOOL GetExitCodeThread(
      HANDLE hThread,
      LPDWORD lpExitCode
    );
    得到。
      

  2.   

    大概是这个样子的
     int main()
    {
       void* p=NULL;
       thread1=createThread(NULL,0 ,threadTest,p,0,&ThradID)
    waitfor... 等待线程结束
    int m=*((int*)p); //不能访问}
    threadtest(void*p)
    {
      int a=1;
      p=&a;
    }
      

  3.   

    threadtest(void*p)
    {
      int a=1;
      p=&a;
    }
    =====
    你这里的a是局部变量,在主线程中调用int m=*((int*)p);时线程函数结束后a就收回了,当然访问不了。
      

  4.   

    int main()
    {
      int* p = new int;
      thread1=createThread(NULL,0 ,threadTest,(void*)p,0,&ThradID)
    waitfor... 等待线程结束
    int m=*((int*)p); //不能访问}
    threadtest(void*p)
    {
      int a=1;
      *(int*)p = a;
    }