怎么让c++多线程计时

杨子隽 |浏览1066次
收藏|2019/02/20 03:59

满意回答

2019/02/20 04:12

这个可以给你个参考,利用定时器(它本身就是多线程的)#include <windows.h>#include <iostream>using namespace std;static BOOL bExitApp = FALSE;const UINT uiTimerID1 = 10;const UINT uiTimerID2 = 11;VOID CALLBACK FooTimerFun1( HWND, UINT, UINT, DWORD ) //定时器1执行程序{static int nCount = 0;cout << "Timer1 Function1 , nCount = " << nCount ++ << endl;if( nCount > 20 ) //20次后结束bExitApp = TRUE;}VOID CALLBACK FooTimerFun2( HWND, UINT, UINT, DWORD ){static int nCount = 0;cout << "Timer1 Function2 , nCount = " << nCount ++ << endl;if( nCount > 10 ) //10次结束bExitApp = TRUE;}int main(){MSG msgFoo;SetTimer( NULL , uiTimerID1 , 1000 , FooTimerFun1 ); //定时器1,1SSetTimer( NULL , uiTimerID1 , 2000 , FooTimerFun2 ); //定时器2,2Swhile( !bExitApp && GetMessage( &msgFoo , NULL , 0 , 0 ) ){TranslateMessage( &msgFoo );DispatchMessage( &msgFoo );}KillTimer( NULL , uiTimerID1);KillTimer( NULL , uiTimerID2);return 0;}程序用VS2017命令行编译执行成功不过建议你还是有MFC,更简单

whoami1978

其他回答(0)
0人关注该问题
+1

 加载中...