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多线程的同步方式和锁机制
Object.wait(miliSec)/notify()/notifyAll() 线程调用wait()之后可以由notify()唤醒,如果指定了miliSec的话也可超时后自动唤醒.wait方法的调 ...
- PHP+Apache2.x+phpMyAdmin安装和配置
1>各个部件的下载 目前在windows下开发 PHP官网下载地址:https://windows.php.net/download PHP有TS(ThreadSafe)和NTS两个版本,所以按 ...
- 15. PARTITIONS
15. PARTITIONS PARTITIONS表提供有关表分区的信息. 此表中的每一行对应于分区表的单个分区或子分区. 有关分区表的更多信息,请参见分区. PARTITIONS表有以下列: TAB ...
- 项目中遇到的超卖问题及解决办法(使用go做测试工具)
超卖问题:在一个很短的时间内,Mysql的数据状态在 取出,比较,提交,或修改中,另外一个进程访问数据导致的超卖问题. 案例: 1.前端没有做限制,如果用户连续点击签到,那么会有多条数据发送到后端,如 ...
- django下的framework
可以创建个虚拟环境先,不过我没使用这个方式 virtualenv env source env/bin/activate ------ 退出: To exit the virtualenv envir ...
- python基础知识05-控制流程
控制流程 1.条件判断 python中的代码从上到下执行. if 条件: 缩进 语句1 elif 条件2: 缩进 语句2 (…或者写pass关键字.不写任何代码的时候,防止报错.) ... else: ...
- style对象的cssText方法
cssText 本质是什么? cssText 的本质就是设置 HTML 元素的 style 属性值. cssText 怎么用? domElement.style.cssText = "col ...
- 一个关于vue+mysql+express的全栈项目(五)------ 实时聊天部分socket.io
一.基于web端的实时通讯,我们都知道有websocket,为了快速开发,本项目我们采用socket.io(客户端使用socket.io-client) Socket.io是一个WebSocket库, ...
- Spring Boot 返回Html界面
@Controller public class HelloController { @RequestMapping("/") public String index(){ ret ...
- mybatis自动映射和手动映射
一对一查询 第一种方法: <!-- 查询所有订单信息 --> <select id="findOrdersList" resultType="cn.it ...