Chinaunix首页 | 论坛 | 博客
  • 博客访问: 68190
  • 博文数量: 42
  • 博客积分: 1730
  • 博客等级: 上尉
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-02 13:06
文章分类

全部博文(42)

文章存档

2011年(1)

2009年(41)

我的朋友

分类: C/C++

2009-09-20 11:23:50

 
首先看code(vs 2008下编译通过):
 

// ThreadPriority.cpp : Defines the entry point for the console application.

//

  
#include "stdafx.h"
#include <Windows.h>
  
DWORD WINAPI ThreadIdle(LPVOID lpParam)
{
    int i = 0;
    while(i < 10)
    {
        printf("Idle thread running, count = %d\n", i++);
    }
    return 0;
}
  
DWORD WINAPI ThreadNormal(LPVOID lpParam)
{
    int i = 0;
    while(i < 10)
    {
        printf("Normal thread running, count = %d\n", i++);
    }
    return 0;
}
  
int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE h[2];
    DWORD dwThreadID;
  
    h[0] = ::CreateThread(NULL, NULL, ThreadIdle, NULL, CREATE_SUSPENDED, &dwThreadID);
    ::SetThreadPriority(h[0], THREAD_PRIORITY_IDLE);
    ::ResumeThread(h[0]);
  
    h[1] = ::CreateThread(NULL, NULL, ThreadNormal, NULL, 0, &dwThreadID);
  
    //::WaitForMultipleObjects(2, h, TRUE, INFINITE);.

    DWORD dw = ::WaitForMultipleObjects(2, h, FALSE, 0);
    switch (dw)
    {
    case WAIT_FAILED:
        printf("wait failed!\n");
        break;
    case WAIT_TIMEOUT:
        printf("wait timeout!\n");
        break;
    case WAIT_OBJECT_0+0:
        printf("HANDLE h[0] is running!\n");
        break;
    case WAIT_OBJECT_0+1:
        printf("HANDLE h[0] is running!\n");
        break;
    }
  
    ::CloseHandle(h[0]);
    ::CloseHandle(h[1]);
    printf("END\n");
    getchar();
    return 0;
}
// ThreadPriority.cpp : Defines the entry point for the console application.

//


#include "stdafx.h"
#include <Windows.h>

DWORD WINAPI ThreadIdle(LPVOID lpParam)
{
    int i = 0;
    while(i < 10)
    {
        printf("Idle thread running, count = %d\n", i++);
    }
    return 0;
}

DWORD WINAPI ThreadNormal(LPVOID lpParam)
{
    int i = 0;
    while(i < 10)
    {
        printf("Normal thread running, count = %d\n", i++);
    }
    return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE h[2];
    DWORD dwThreadID;

    h[0] = ::CreateThread(NULL, NULL, ThreadIdle, NULL, CREATE_SUSPENDED, &dwThreadID);
    ::SetThreadPriority(h[0], THREAD_PRIORITY_IDLE);
    ::ResumeThread(h[0]);

    h[1] = ::CreateThread(NULL, NULL, ThreadNormal, NULL, 0, &dwThreadID);

    //::WaitForMultipleObjects(2, h, TRUE, INFINITE);.

    DWORD dw = ::WaitForMultipleObjects(2, h, FALSE, 0);
    switch (dw)
    {
    case WAIT_FAILED:
        printf("wait failed!\n");
        break;
    case WAIT_TIMEOUT:
        printf("wait timeout!\n");
        break;
    case WAIT_OBJECT_0+0:
        printf("HANDLE h[0] is running!\n");
        break;
    case WAIT_OBJECT_0+1:
        printf("HANDLE h[0] is running!\n");
        break;
    }

    ::CloseHandle(h[0]);
    ::CloseHandle(h[1]);
    printf("END\n");
    getchar();
    return 0;
}

 

疑惑以及解释:(有什么不妥的地方,还请大家多多指教)

由于我这里设置等待的时间是0,所以在新建立的两个线程还未执行前,主线程已经执行到了getchar();
也就是说已经执行了closehandle(),那后面的两个线程怎么又能执行并打印结果呢?开始一直对这个问题
不解,通过翻阅了王艳平的《Windows程序设计》,才明白原来closehandle()并没有真正的销毁句柄
具体解释:
 每次createthread()创建线程对象的时候,线程对象中Usage count的初始化值为2(注意不是1)
closehandle()能是Usage count的值减少1,这个时候Usage count的值为1,所以并没有销毁,只有
当线程执行的函数通过return结束的时候,Usage count继续减少1变为0,这个时候才真正的销毁对象

 

 

运行结果:

 

wait
END
Normal thread running, count = 0
Normal thread running, count = 1
Normal thread running, count = 2
Normal thread running, count = 3
Normal thread running, count = 4
Normal thread running, count = 5
Normal thread running, count = 6
Normal thread running, count = 7
Idle thread running, count = 0
Normal thread running, count = 8
Idle thread running, count = 1
Normal thread running, count = 9
Idle thread running, count = 2
Idle thread running, count = 3
Idle thread running, count = 4
Idle thread running, count = 5
Idle thread running, count = 6
Idle thread running, count = 7
Idle thread running, count = 8
Idle thread running, count = 9

阅读(7158) | 评论(0) | 转发(0) |
0

上一篇:traceroute学习

下一篇:TLS thread local storage

给主人留下些什么吧!~~