win32多线程程序设计 问题1

代码

#include
<stdio.h>
#include
<stdlib.h>
#include
<conio.h>
#include
<windows.h>

DWORD WINAPI GetMax(LPVOID p)
{
    Sleep((DWORD)p
*1000+1000);
    
return (DWORD)p+100;
}

int main()
{
    HANDLE    hThread1;
    HANDLE  hThread2;
    DWORD    uExitCode1;
    DWORD    uExitCode2;
    DWORD    dwThId;

    hThread1 
= CreateThread(NULL, 0, GetMax, (LPVOID)10&dwThId);
    
if(hThread1)
        puts(
"start thread1");
    
    hThread2 
= CreateThread(NULL, 0, GetMax, (LPVOID)20&dwThId);
    
if(hThread2)
        puts(
"start thread2");

    
for(;;)
    {
        puts(
"press any key to check thread...");
        _getch();

        GetExitCodeThread(hThread1, 
&uExitCode1);
        GetExitCodeThread(hThread2, 
&uExitCode2);
        
if(uExitCode1==STILL_ACTIVE)
            puts(
"Thread1 still active");
        
else
            printf(
"Thread1 over with: %d\n", uExitCode1);

        
if(uExitCode2==STILL_ACTIVE)
            puts(
"Thread2 still active");
        
else
            printf(
"Thread2 over with: %d\n", uExitCode2);


        
if(uExitCode1!=STILL_ACTIVE && uExitCode2!=STILL_ACTIVE)
            
break;
    }

    CloseHandle(hThread1);  
//关闭线程句柄并不影响线程的执行
    CloseHandle(hThread2);

    
return EXIT_SUCCESS;
}
 1 Code:
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 #include<conio.h>
 5 #include<windows.h>
 6 
 7 DWORD WINAPI GetMax(LPVOID p)
 8 {
 9     Sleep((DWORD)p*1000+1000);
10     return (DWORD)p+100;
11 }
12 
13 int main()
14 {
15     HANDLE    hThread1;
16     HANDLE  hThread2;
17     DWORD    uExitCode1;
18     DWORD    uExitCode2;
19     DWORD    dwThId;
20 
21     hThread1 = CreateThread(NULL, 0, GetMax, (LPVOID)10&dwThId);
22     if(hThread1)
23         puts("start thread1");
24     
25     hThread2 = CreateThread(NULL, 0, GetMax, (LPVOID)20&dwThId);
26     if(hThread2)
27         puts("start thread2");
28 
29     for(;;)
30     {
31         puts("press any key to check thread...");
32         _getch();
33 
34         GetExitCodeThread(hThread1, &uExitCode1);
35         GetExitCodeThread(hThread2, &uExitCode2);
36         if(uExitCode1==STILL_ACTIVE)
37             puts("Thread1 still active");
38         else
39             printf("Thread1 over with: %d\n", uExitCode1);
40 
41         if(uExitCode2==STILL_ACTIVE)
42             puts("Thread2 still active");
43         else
44             printf("Thread2 over with: %d\n", uExitCode2);
45 
46 
47         if(uExitCode1!=STILL_ACTIVE && uExitCode2!=STILL_ACTIVE)
48             break;
49     }
50 
51     CloseHandle(hThread1); 
52     CloseHandle(hThread2);
53 
54     return EXIT_SUCCESS;
55 }

《win32多线程程序设计》侯捷译:

CreateThread:获取一个结束线程的返回值。返回值是Bool值,表示线程是否执行成功而不是线程结束返回的值。

STILL_ACTIVE函数用来检测线程是否结束。

MSDN:

Warning  If a thread happens to return STILL_ACTIVE (259) as an error code, applications that test for this value could end up in an infinite loop.

如果一个线程返回STILL_ACTIVE作为失败的返回,则检测该线程的应用程序将陷入死循环。

 

不知道是不是我对台湾人用中文表达的错误理解...


 


posted @ 2010-05-06 15:55  wy111  阅读(186)  评论(0)    收藏  举报