windows下的两个等待技术

第一种: Win32  Sleep()函数

     这个函数要求操作系统中止线程动作。直到读过某个指定的时间之后才恢复。能在某个线程结束时(而不是某段时间结束时)被调用。

另外一种:busy  loop(busy waits)

     不断调用GetExitCodeThread(),直到其结果不再是STILL_ACTIVE.

缺点:浪费CPU时间。

绝对不要在Win32中使用busy loop

//busywait.c

/*Domonstrate the effect on performance of using a busy loop.

First call the worker routine with just a function call to get a baseline performance reading 

then create a second thread and a busy loop.

*/

#define WIN32_LEAN_AND_MEAN

#include <stdio.h>

#include<stdlib.h>

#include<windows.h>

#include<time.h>

#include "MtVerify.h"



DWORD WINAPI ThreadFunc(LPVOID);



int main()

{

HANDLE hThrd;

DWORD exitCode = 0;

DWORD threadId;

DWORD begin;

DWORD elapsed;

puts("TImiing normal function call.....");

begin = GetTickCount();//示以毫秒为单位的计算机启动后经历的时间间隔。

ThreadFunc(0);

elapsed = GetTickCount() - begin;

printf("Function call took:%d.%.03d seconds\n\n", elapsed / 1000, elapsed % 1000);

puts("Timing thread + busy loop....");

begin = GetTickCount();

MTVERIFY(hThrd = CreateThread(NULL, 0, ThreadFunc, (LPVOID)1, 0, &threadId));

//这个宏内部事实上是记录并解释了Win32 GetLastError()的结果。

/*This busy loop chews up lots of CPU time*/

for (;;)

{

GetExitCodeThread(hThrd, &exitCode);

if (exitCode != STILL_ACTIVE)

break;

}

elapsed = GetTickCount() - begin;

printf("Thread+busy loop took: %d.%.03d seconds\n", elapsed / 1000, elapsed % 1000);

MTVERIFY(CloseHandle(hThrd));

return EXIT_SUCCESS;

}

/*Cute little busy work routine that computes the value

of PI using probability.Highly dependent on having a good random number generator (rand is iffy)

*/

DWORD WINAPI ThreadFunc(LPVOID n)

{

int i;

int inside = 0;

double val;

UNREFERENCED_PARAMETER(n);//告诉编译器,已经使用了该变量,不必检測警告。

/*Seed the random-number generator.*/

srand((unsigned)time(NULL));

for (i = 0; i < 1000000; i++)

{

double x = (double)(rand()) / RAND_MAX;

double y = (double)(rand()) / RAND_MAX;

if ((x*x + y*y) <= 1.0)

inside++;

}

val = (double)inside / i;

printf("PI=%.4g\n", val * 4);

return 0;

}

windows下的两个等待函数的更多相关文章

  1. Qt Windows下链接子系统与入口函数(终结版)(可同时存在main和WinMain函数)

    Qt Windows下链接子系统与入口函数(终结版) 转载自:http://blog.csdn.net/dbzhang800/article/details/6358996 能力所限,本讨论仅局限于M ...

  2. windows下配置两个或多个Tomcat启动的方法

    确保window的环境变量中找不到CATALINA_HOME和CATALINA_BASE 修改server.xml,用解压版的tomcat,不要用安装版的. 1.修改http访问端口 conf下的se ...

  3. Windows下编程2----- C语言常用函数举例

    几个小函数 1.    //MessageBoxA(0,"网络故障,重新登录","qq error",3); //弹出对话框 2.    //ShellExec ...

  4. Windows下的两个缺陷

    记事本缺陷: 标题:新建记事本中仅输入“联通”,保存关闭后再打开,显示为乱码 详细描述: 环境说明:操作系统ALL 重现步骤: 1.新建一个记事本,在其中仅输入“联通”两个字 2.再将该记事本关闭保存 ...

  5. Windows 下关于转码的函数

    std::string& MsgFieldList::GBToUTF8(std::string& des,const char* str) { WCHAR *strSrc; TCHAR ...

  6. windows下的getopt/getoptlong函数

    windows下的getopt/getoptlong函数 getopt/getopt_long函数是GNU C中的函数,在linux编程中很常用到.这里就不介绍了. windows下没有找到类似的函数 ...

  7. WINDOWS下如何安装GCC(转载http://nirvana.cublog.cn;作者:北斗星君(黄庠魁))

    第一章 在视窗操作系统下的GCC 第一节 GCC家族概览 GCC 是一个原本用于 Unix-like 系统下编程的编译器.不过,现在 GCC 也有了许多 Win32 下的移植版本.所以,也许对于许多 ...

  8. 【转】在Windows下搭建React Native Android开发环境

    http://www.jianshu.com/p/2fdc4655ddf8 安装JDK 从Java官网下载JDK并安装.请注意选择x86还是x64版本. 推荐将JDK的bin目录加入系统PATH环境变 ...

  9. Windows下文件列举,搜索

    Windows下列举文件用的函数是 FindFirstFile 和 FindNextFile ,另外一个结构体是WIN32_FIND_DATA 以下是MSDN对于WIN32_FIND_DATA的定义 ...

随机推荐

  1. [IOS初学]ios 第一篇 storyboard 与viewcontroller的关系 - Zoe_J

    时间 2014-07-27 16:08:00  博客园-所有随笔区 原文  http://www.cnblogs.com/zoe-j/p/3871501.html 主题 StoryBoard 学习了一 ...

  2. linux 部署nginx作为反向代理入口的内核参数/etc/sysctl.conf

    # Kernel sysctl configuration file for Red Hat Linux## For binary values, 0 is disabled, 1 is enable ...

  3. 网易技术分享:Nginx缓存引发的跨域惨案

    推荐:更多技术团队分享文章 关注:MAYOU18技术专栏 1. 前言 贵金属wap版直播间上线后,偶尔有用户反馈,在进入wap直播间的时候,出现空白页面,但是重新刷新又可以正常显示了.我们曾一度认为是 ...

  4. 关于nagios系统下使用shell脚本自定义监控插件的编写以及没有实时监控图的问题

    关于nagios系统下shell自定义监控插件的编写.脚本规范以及没有实时监控图的问题的解决办法 在自已编写监控插件之前我们首先需要对nagios监控原理有一定的了解 Nagios的功能是监控服务和主 ...

  5. Python 函数的初识

    1.函数的初识 函数的作用:以功能为导向 减少代码重复 # 函数试编程 # 函数以功能(完成一件事)为导向,登录 注册, # 一个函数就是一个功能,一个函数只能写一个功能 # 何时需要 何时调用,随调 ...

  6. 我的Python分析成长之路1

    Python是什么?                                                                                           ...

  7. ssm+activiti+maven

    1spring整合activiti中添加activiti依赖 <!-- 添加Activiti支持 --> <dependency> <groupId>org.act ...

  8. URAL 2040 Palindromes and Super Abilities 2

    Palindromes and Super Abilities 2Time Limit: 500MS Memory Limit: 102400KB 64bit IO Format: %I64d &am ...

  9. 洛谷P1504 积木城堡

    题目描述 XC的儿子小XC最喜欢玩的游戏用积木垒漂亮的城堡.城堡是用一些立方体的积木垒成的,城堡的每一层是一块积木.小XC是一个比他爸爸XC还聪明的孩子,他发现垒城堡的时候,如果下面的积木比上面的积木 ...

  10. 【转载】在Javascript中 声明时用"var"与不用"var"的区别

    原文链接:http://www.2cto.com/kf/201204/128406.html[侵删]   Javascript声明变量的时候,虽然用var关键字声明和不用关键字声明,很多时候运行并没有 ...