这个程序输出一个Start!,一秒后会输出一个数,把你电脑上输出的数回复在评论区下方。
有可能会输出Error,遇到这种情况请告诉我。
#include <windows.h>
#include <iostream>
#include <future>
using namespace std;
typedef HANDLE Thread,Event;
SECURITY_ATTRIBUTES sa={sizeof sa};
Event start=CreateEvent(&sa,true,false,"Local\\CPU-speed");
LONG isend,plustime;
DWORD WINAPI check(PVOID)
{
if(WaitForSingleObject(start,10000)==WAIT_TIMEOUT)
return (cout<<"Error",0xE0000000);
while(isend)
InterlockedIncrement(&plustime);
return plustime;
}
int main(int argc,char *argv[],char *envp[])
{
Thread thrd=CreateThread(&sa,NULL,check,nullptr,NULL,NULL);
cout<<"Start!"<<endl;
SetEvent(start);
InterlockedIncrement(&isend);
this_thread::sleep_for(chrono::seconds(1));
InterlockedDecrement(&isend);
if(WaitForSingleObject(thrd,10000)==WAIT_TIMEOUT)
cout<<"Error";
cout<<plustime<<endl;
system("pause");
return 0;
}
我次运行程序的输出:
Update:在学校机房的电脑上跑出了
Update again:这个东西只能在Windows下运行!还有,这个程序需要C++11。