#include <windows.h>
#include <stdio.h>
#include <process.h>unsigned Counter; 
unsigned __stdcall SecondThreadFunc( void* pArguments )
{
    printf( "In second thread...\n" );    while ( Counter < 1000000 )
        Counter++;    _endthreadex( 0 );
    return 0;
} int main()

    HANDLE hThread;
    unsigned threadID;    printf( "Creating second thread...\n" );    // Create the second thread.
    hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );    // Wait until second thread terminates. If you comment out the line
    // below, Counter will not be correct because the thread has not
    // terminated, and Counter most likely has not been incremented to
    // 1000000 yet.
    WaitForSingleObject( hThread, INFINITE );
    printf( "Counter should be 1000000; it is-> %d\n", Counter );
    // Destroy the thread object.
    CloseHandle( hThread );
}

解决方案 »

  1.   

    #include <windows.h>
    #include <stdio.h>DWORD WINAPI ThreadFunc( LPVOID lpParam ) 

        printf( "Parameter = %d.", *(DWORD*)lpParam );     return 0; 

     
    VOID main( VOID ) 

        DWORD dwThreadId, dwThrdParam = 1; 
        HANDLE hThread;     hThread = CreateThread( 
            NULL,                        // default security attributes 
            0,                           // use default stack size  
            ThreadFunc,                  // thread function 
            &dwThrdParam,                // argument to thread function 
            0,                           // use default creation flags 
            &dwThreadId);                // returns the thread identifier 
     
       // Check the return value for success. 
     
       if (hThread == NULL) 
       {
          printf( "CreateThread failed (%d)\n", GetLastError() ); 
       }
       else 
       {
          _getch();
          CloseHandle( hThread );
       }
    }
      

  2.   

    谢谢,不过这不是我想要的啊?
    我想在关机函数{}中调用一个线程函数,在这个线程函数中还要添加一个判断语句if()else{}
    我想问的是怎么建立并启动一个线程函数,线程函数怎么调用?麻烦给讲下,先谢谢了.
      

  3.   

    MFC的更简单,看看吧,5分钟之内搞定
      

  4.   

    谢谢,不过这不是我想要的啊?
    我想在关机函数{}中调用一个线程函数,在这个线程函数中还要添加一个判断语句if()else{}
    我想问的是怎么建立并启动一个线程函数,线程函数怎么调用?麻烦给讲下,先谢谢了.------------------------