怎么用CREATTHREAD,创建线程??能在不用MFC而纯C/C++的环境中用吗?要包含什么头文件?具体怎么操作,稍微讲一下好吗?弄懂后马上给分!

解决方案 »

  1.   

    you only need
    #include <windows.h>see examples athttp://codeguru.earthweb.com/system/OOThreadWrapper.htmlhttp://codeguru.earthweb.com/dll/DLL_Callback.html
    also see
    http://www.microsoft.com/msj/defaultframe.asp?page=/msj/0799/win32/win320799.htm&nav=/msj/0799/newnav.htm
      

  2.   

    哦,还有我不会用MFC,我希望在CONSOLE下面完成,能做到吗?
      

  3.   

    #include <windows.h>
    #include <iostream>using namespace std;void  WINAPI f(LPVOID  pvThread)
    {
      cout << "inside thread" << endl;
    }int main()
    {
      cout << "inside main" << endl;
      HANDLE hThread = CreateThread(NULL, 
                              0, 
                              (LPTHREAD_START_ROUTINE)f, 
                              NULL, 
                              0, 
                              NULL); if(hThread != NULL)
     {
             WaitForSingleObject(hThread, INFINITE); 
    CloseHandle(hThread); 
     }
     
      return 0;
    }
      

  4.   

    谢谢saucer(思归)的帮助因为我是第一次接触线程,所以仍有不少不懂的地方,还望告之!1:我调试你写的函数,为什么不会出来:Inside thread2:void  WINAPI f(LPVOID  pvThread)  其中WINAPI是必须的吗?代表什么?3:if(hThread != NULL)
     {
             WaitForSingleObject(hThread, INFINITE); 
    CloseHandle(hThread); 
     }
      是做什么的呢
    4:(LPTHREAD_START_ROUTINE)f 是不是把相应的函数与相应的线程结合起来?                         谢谢
      

  5.   

    请参考《win32多线程程序设计》Jim Beveridge & Robert Wiener 著,侯捷译,华中科技大出版!
      

  6.   

    1. what compiler are you using? it works on my machine (VC++6, win2k)2. according to the documentation
    #define WINAPI      __stdcallthis is the calling convention, but it is not needed in this case3. the main thread waits until the child thread finishes4. it just casts the function pointer into the required signaturetypedef DWORD (WINAPI * PTHREAD_START_ROUTINE)(LPVOID lpThreadParameter);typedef PTHREAD_START_ROUTINE LPTHREAD_START_ROUTINE;to be strict, you should be doingDWORD WINAPI f(LPVOID  pvThread)
    {
      cout << "inside thread" << endl;
      return 0;
    }
      

  7.   

    我用的是VC 6。0
    怎么就是不出来inside thread?
    一上来就出现inside main
      

  8.   

    另外,如果我想让那个线程一直进行下去,直至MAIN函数结束
    是不是不需要
    if(hThread != NULL)
     {
             WaitForSingleObject(hThread, INFINITE); 
    CloseHandle(hThread); 
     }
      ?我怎么试(无论是在线程里加断点还是别的,)就是不出现inside thread
      

  9.   

    1. right, but if you remove those statements, the thread function may terminate prematurely when the main thread terminates, it may not be what you want2. do not add breakpoints, just run the exe directly
      

  10.   

    However hard i tried ,i still can not see "inside thread"how come?!really appreciate it very much you can answer my question in so 
    late a night(actually in so early a day)
      

  11.   

    sorry, I have no idea why it does not run on your machine, here are the commands:D:\TEMP>vcvars32
    Setting environment for using Microsoft Visual C++ tools.
    D:\TEMP>cl testmt.cpp
    Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
    Copyright (C) Microsoft Corp 1984-1998. All rights reserved.testmt.cpp
    .........
    Microsoft (R) Incremental Linker Version 6.00.8447
    Copyright (C) Microsoft Corp 1992-1998. All rights reserved./out:testmt.exe
    testmt.objD:\TEMP>testmt
    inside main
    inside threadD:\TEMP>
      

  12.   

    it works!it works on my another system win2000,but still doesn't
    work here--win98.
    really thank u!!