Time synchronization for multiple threads

Source: Internet
Author: User

1. If you do game development, in order to write a single thread for time is not a good deal, Createwaitabletimera and the like is a kernel object, because multiple clocks have the same name, you can have multiple threads simultaneously read a clock.

2. If you want to use more than one timer, you should set the name of each timer, if it is anonymous, it can only be one.

#include <stdio.h>#include<stdlib.h>#include<Windows.h>intMain () {HANDLE time1= Createwaitabletimera (NULL, TRUE,"Haihua"); if(time1==NULL) {printf ("creation failed");  } Large_integer MyTime; //Large integer, which is essentially a structural body mytime. QuadPart= -50000000;//The unit is 0.1 microseconds, set to 5 seconds after the timer is started. 

if(! SetWaitableTimer (Time1,&mytime,0,0,0,0) {printf ("Setup failed"); } if(WaitForSingleObject (time1,infinite) = =wait_object_0) { //if equals wait_object_0, it means getting the message. printf ("wait OK"); } Else{printf ("Wait No"); } System ("Pause");}

The time timer also allows the callback function

#include <stdio.h>#include<stdlib.h>#include<Windows.h>
//function pointer as a parameter, the function he calls is the callback function.
VOID CALLBACK Timerun (void*Parg,dword Timearg,dword Timehigh) {DWORD dwindex= * (DWORD *) parg; printf ("\ n%d times .", dwindex); //messageboxa (0, "1", "2", 0);}intMain () {HANDLE time1= Createwaitabletimera (NULL, TRUE,"Haihua");//Create always if(Time1 = =NULL) {printf ("creation failed"); } Large_integer MyTime; MyTime. QuadPart= -50000000;//0.1 Subtle,DWORD dwparam=1;//Set Timer if(SetWaitableTimer (time1, &mytime, the, Timerun, &dwparam, FALSE)){ //3000, which represents 3000 milliseconds to perform a loop. printf ("wait 5 seconds to start working ."); for(inti =0; I < the; i++, dwparam++)//loops how many times{SleepEx (INFINITE, TRUE); }} cancelwaitabletimer (time1);//Cancel TimerCloseHandle (time1); System ("Pause");}

Time synchronization for multiple threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.