windows下的两个等待函数
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下的两个等待函数的更多相关文章
- Qt Windows下链接子系统与入口函数(终结版)(可同时存在main和WinMain函数)
Qt Windows下链接子系统与入口函数(终结版) 转载自:http://blog.csdn.net/dbzhang800/article/details/6358996 能力所限,本讨论仅局限于M ...
- windows下配置两个或多个Tomcat启动的方法
确保window的环境变量中找不到CATALINA_HOME和CATALINA_BASE 修改server.xml,用解压版的tomcat,不要用安装版的. 1.修改http访问端口 conf下的se ...
- Windows下编程2----- C语言常用函数举例
几个小函数 1. //MessageBoxA(0,"网络故障,重新登录","qq error",3); //弹出对话框 2. //ShellExec ...
- Windows下的两个缺陷
记事本缺陷: 标题:新建记事本中仅输入“联通”,保存关闭后再打开,显示为乱码 详细描述: 环境说明:操作系统ALL 重现步骤: 1.新建一个记事本,在其中仅输入“联通”两个字 2.再将该记事本关闭保存 ...
- Windows 下关于转码的函数
std::string& MsgFieldList::GBToUTF8(std::string& des,const char* str) { WCHAR *strSrc; TCHAR ...
- windows下的getopt/getoptlong函数
windows下的getopt/getoptlong函数 getopt/getopt_long函数是GNU C中的函数,在linux编程中很常用到.这里就不介绍了. windows下没有找到类似的函数 ...
- WINDOWS下如何安装GCC(转载http://nirvana.cublog.cn;作者:北斗星君(黄庠魁))
第一章 在视窗操作系统下的GCC 第一节 GCC家族概览 GCC 是一个原本用于 Unix-like 系统下编程的编译器.不过,现在 GCC 也有了许多 Win32 下的移植版本.所以,也许对于许多 ...
- 【转】在Windows下搭建React Native Android开发环境
http://www.jianshu.com/p/2fdc4655ddf8 安装JDK 从Java官网下载JDK并安装.请注意选择x86还是x64版本. 推荐将JDK的bin目录加入系统PATH环境变 ...
- Windows下文件列举,搜索
Windows下列举文件用的函数是 FindFirstFile 和 FindNextFile ,另外一个结构体是WIN32_FIND_DATA 以下是MSDN对于WIN32_FIND_DATA的定义 ...
随机推荐
- java 自动拆箱 自动装箱
自动装箱的定义就是 基本数据类型赋值给包装类型, 拆箱则相反. Integer integer = 122; // 自动装箱 int num = integer; //自动拆箱 想看一下源码是怎么 ...
- c++_方格填数(最新方法)
方格填数 如下的10个格子 +--+--+--+ | | | |+--+--+--+--+| | | | |+--+--+--+--+| | | |+--+--+--+ (如果显示有问题,也可以参 ...
- 使用 ES (elasticsearch) 搜索中文
1.创建索引 curl -XPUT http://172.16.125.139:9200/ques2.创建索引类型 curl -XPOST http://172.16.125.139:9200/que ...
- LeetCode(26) Remove Duplicates from Sorted Array
题目 Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- fiddler终极教程
http://www.cnblogs.com/yoyoketang/tag/fiddler/
- 大数据学习——hadoop安装
上传centOS6.7-hadoop-2.6.4.tar.gz 解压 tar -zxvf centOS6.7-hadoop-2.6.4.tar.gz hadoop相关修改配置 1 修改 /root/a ...
- NYOJ-481平衡字符串
平衡字符串 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 给你一定长度的字符串.字符串中只包含26个小写字母,首先我们把字母a-z分为2堆(a--m)和(n--z),判 ...
- COJ 1208 矩阵快速幂DP
题目大意: f(i) 是一个斐波那契数列 , 求sum(f(i)^k)的总和 由于n极大,所以考虑矩阵快速幂加速 我们要求解最后的sum[n] 首先我们需要思考 sum[n] = sum[n-1] + ...
- B题 Sort the Array
题目大意:判断能否通过一次倒置,使序列变为一个递增序列 如果可以,输出倒置那一段的起始点和终点的位置: 题目链接:http://codeforces.com/problemset/problem/45 ...
- BZOJ 3039: 玉蟾宫【dp】
Description 有一天,小猫rainbow和freda来到了湘西张家界的天门山玉蟾宫,玉蟾宫宫主蓝兔盛情地款待了它们,并赐予它们一片土地.这片土地被分成N*M个格子,每个格子里写着'R'或者' ...