设为首页  
联系我们  
加入收藏  
网页制作 冲浪宝典 图形图像 操作系统 软件教学 编程开发 认证考试 安全技术 站长专区 文学驿站 娱乐天地 游戏天地 办公软件
文章搜索
您的位置: 首页 >> 文章首页 >> 编程开发 >> 游戏开发 >> 《Windows游戏编程大师技巧》(第二版)第11章
精品推荐
游戏开发点击TOP10
·DirectX5.0最新游戏编程指南 DirectDraw教程篇 二、第一个DirectDraw实例
·用Excel编写小游戏
·五子棋的原代码
·游戏开发方面相关的电子书下载
·游戏文件系统的解决方案
·基于LOD的大规模真实感室外场景实时渲染技术的初步研究 part II&III
·用MFC构造DirectX应用框架
·游戏开发开门
·《Windows游戏编程大师技巧》(第二版)第11章
·DirectX8.1的DirectDraw7研究手记(三)
编程开发点击TOP10
·数字小键盘指法练习
·ASP.NET 程序中常用的三十三种代码
·用C语言编通讯录程序(初学者级别的)
·我写的Java学生成绩管理系统源代码
·CHK文件恢复工具
·Modem 常用AT指令集
·java笔试题
·异常java.sql.SQLException: Io exception:The Network Adapter could not establish connection
·单片机模拟I2C总线及24C02(I2C EEPROM)读写实例(源代码)
·C++经典电子书下载
精选专题

《Windows游戏编程大师技巧》(第二版)第11章

作者: 来源:网络文章 时间:2005-12-17 22:39:07

《Windows游戏编程大师技巧》(第二版)第11章(30) // takes longer than threads,
// so threads finish first, note that primary thread prints 4

for (index=0; index<25; index++)
    {
    printf("4 ");
    Sleep(100);
    }  // end for index

// at this point all the threads are still running,
// now if the keyboard is hit
// then a message will be sent to terminate all the
// threads and this thread
// will wait for all of the threads to message in

while(!kbhit());

// get that char
getch();

// set global termination flag
terminate_threads = 1;

// wait for all threads to terminate,
// when all are terminated active_threads==0
while(active_threads);

// at this point the threads should all be dead, so close handles
for (index=0; index < MAX_THREADS; index++)
    CloseHandle(thread_handle[index]);
// end with a blank line
printf("\nAll threads terminated.\n");

}  // end main
输出示例:
Starting Threads...
4 1 2 3 4 2 1 3 4 3 1 2 4 2 1 3 4 3 1 2 4 2 1 3 4 2
 3 1 4 2 1 3 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1
 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2
 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 4 2 3 1 2 3 1 3 2
 1 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 3 2 1 1 2
 3 3 2 1 2 3 1 3 2 1 2 3 1 3 2 1 2 3 1 3 2 1 2 3 1 3 2
 1 3 1 2 3 2 1 3 1 2 3 2 1
All threads terminated.
如输出所示,当用户敲击一个键时,所有的线程被结束,随后主线程也被结束。这种方法有两个问题。第一个问题比较不明显。下面是它的具体案例,多看几遍你便会发现问题:
1. 假定只剩一个从线程没有关闭。
2. 假定最后一个线程对处理器拥有控制,并递减跟踪激活线程数的全局变量值。
3. 就在这一刹那,进程切换到主线程。主线程监测全局变量并认为所有的线程都已结束,然而最后一个线程却还没有返回!
在大多数情况下,这不是一个问题,但是如果递减代码和返回代码之间还有代码,便会出现这个问题。所以我们需要一个函数来询问线程是否已结束。很多情况下,这是非常有帮助的。参考一下Wait*()系列函数,对编程会大有益处。
第二个问题是当你创建了一个忙循环(Busy Loop)或轮询的循环。在Win16/DOS系统下,该循环会良好地执行,但在Win32下,就很不好。在一个封闭的循环中,等待一个变量,会给多任务内核带来繁重的负担并严重占用CPU资源。
可以使用Windows附带的SYSMON.EXE(Windows 95/98/ME/XP的附件中)、PERFMON.EXE(Windows NT/2000)或类似的第三方CPU占用率测试工具来进行测定。这些工具软件会有助于你明白线程的运行状态和处理器的占用率。接下来,我们看看Wait*()类函数将如何帮助我们确定一个线程是否结束。

等待合适时机

1.《Windows游戏编程大师技巧》(第二版)第11章(1)
2.《Windows游戏编程大师技巧》(第二版)第11章(2)
3.《Windows游戏编程大师技巧》(第二版)第11章(3)
4.《Windows游戏编程大师技巧》(第二版)第11章(4)
5.《Windows游戏编程大师技巧》(第二版)第11章(5)
6.《Windows游戏编程大师技巧》(第二版)第11章(6)
7.《Windows游戏编程大师技巧》(第二版)第11章(7)
8.《Windows游戏编程大师技巧》(第二版)第11章(8)
9.《Windows游戏编程大师技巧》(第二版)第11章(9)
10.《Windows游戏编程大师技巧》(第二版)第11章(10)
11.《Windows游戏编程大师技巧》(第二版)第11章(11)
12.《Windows游戏编程大师技巧》(第二版)第11章(12)
13.《Windows游戏编程大师技巧》(第二版)第11章(13)
14.《Windows游戏编程大师技巧》(第二版)第11章(14)
15.《Windows游戏编程大师技巧》(第二版)第11章(15)
16.《Windows游戏编程大师技巧》(第二版)第11章(16)
17.《Windows游戏编程大师技巧》(第二版)第11章(17)
18.《Windows游戏编程大师技巧》(第二版)第11章(18)
19.《Windows游戏编程大师技巧》(第二版)第11章(19)
20.《Windows游戏编程大师技巧》(第二版)第11章(20)
21.《Windows游戏编程大师技巧》(第二版)第11章(21)
22.《Windows游戏编程大师技巧》(第二版)第11章(22)
23.《Windows游戏编程大师技巧》(第二版)第11章(23)
24.《Windows游戏编程大师技巧》(第二版)第11章(24)
25.《Windows游戏编程大师技巧》(第二版)第11章(25)
26.《Windows游戏编程大师技巧》(第二版)第11章(26)
27.《Windows游戏编程大师技巧》(第二版)第11章(27)
28.《Windows游戏编程大师技巧》(第二版)第11章(28)
29.《Windows游戏编程大师技巧》(第二版)第11章(29)
30.《Windows游戏编程大师技巧》(第二版)第11章(30)
31.《Windows游戏编程大师技巧》(第二版)第11章(31)
32.《Windows游戏编程大师技巧》(第二版)第11章(32)
33.《Windows游戏编程大师技巧》(第二版)第11章(33)
34.《Windows游戏编程大师技巧》(第二版)第11章(34)
35.《Windows游戏编程大师技巧》(第二版)第11章(35)
36.《Windows游戏编程大师技巧》(第二版)第11章(36)
37.《Windows游戏编程大师技巧》(第二版)第11章(37)
38.《Windows游戏编程大师技巧》(第二版)第11章(38)
共38页 9 7 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25] [26] [27] [28] [29] [30] [31] [32] [33] [34] [35] [36] [37] [388 :>

《Windows游戏编程大师技巧》(第二版)第11章 相关文章:
《Windows游戏编程大师技巧》(第二版)第11章 相关软件:
特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
转载请注明来源:http://www.xgdown.com