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的定义 ...
随机推荐
- docker的网络(基础)
Docker的网络子系统是可插拔的,使用驱动程序.默认情况下存在多个驱动程序,并提供核心网络功能: bridge:docker默认的网络驱动.如果未指定驱动程序,则这是需要创建的网络类型.当应用程序在 ...
- Linux离线安装redis集群
一.应用场景介绍 本文主要是介绍Redis集群在Linux环境下的安装讲解,联网环境安装较为简单,这里只说脱机的Linux环境下是如何安装的.因为大多数时候,公司的生产环境是在内网环境下,无外网,服务 ...
- POJ 1383 Labyrinth (树的直径求两点间最大距离)
Description The northern part of the Pyramid contains a very large and complicated labyrinth. The la ...
- MyBatis 3 分页
利用MyBatis 3进行分页,选定的数据库表c_province,有3列,id列,provinceid列,province列,用Oracle数据库.首先建立一个对应的实体类,Province有3个属 ...
- win10 命令行无法直接ping的问题解决方法
ping是在system32下的一个应用包里,设置完环境变量并保存之后若还是无法直接ping,可以考虑如下方法: 打开命令行窗口,键入以下命令: cd\ cd windows\system32 pin ...
- Codeforces Round #386 (Div. 2) A+B+C+D!
A. Compote 水题(数据范围小都是水题),按照比例找最小的就行了,3min水过. int main() { int a,b,c; while(~scanf("%d%d%d" ...
- CodeForces 554B--Ohana Cleans Up
B. Ohana Cleans Up time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 【dfs】codeforces Journey
http://codeforces.com/contest/839/problem/C [AC] #include<iostream> #include<cstdio> #in ...
- fedora安装后,yum命令不能使用,Cannot retrieve metalink for repository: fedora. Please verify its path and try again 解决方法
fedora安装后,yum命令不能使用,Cannot retrieve metalink for repository: fedora. Please verify its path and try ...
- ES6__数据结构 Map
/* 数据结构 Map */ /* * 字典:是用来存储不重复的key的hash结构.不同于集合(Set)的是,字典使用的是[键,值]的形式来储存数据的. *javaScript 的对象(Object ...