473,508 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

making a non blocking timer in c++

3 New Member
Hello everybody,
I am trying to make a C++ timer on windows which has following functionality:

Once the function is called, timer should start and last for 10 seconds, but in this period of time, it shouldn’t block other functions working.

I mean if timer should run in background and if it reaches to 10, a special function should be called and timer value should be reset.

If timer doesn’t reach to 10 and gets call for the second time, it should be reset to zero and start again.

Expand|Select|Wrap|Line Numbers
  1. Counter=0;
  2. Void function()
  3. {
  4. if (counter == 0)
  5.     time (&start);
  6. ::counter++;
  7.  
  8. for ( ; ; )
  9. {
  10. if (elapsed_time = difftime(time( &finish ), start )>10)
  11.     {
  12.         cout << "Time out Has Occurd" << endl;
  13.         time( &start );
  14.     }             
  15. }
  16. }
  17.  
  18.  
But this function is blocking other functions.

Is there any other command that can help me?
May 1 '07 #1
6 16161
Savage
1,764 Recognized Expert Top Contributor
Hello everybody,
I am trying to make a C++ timer on windows which has following functionality:

Once the function is called, timer should start and last for 10 seconds, but in this period of time, it shouldn’t block other functions working.

I mean if timer should run in background and if it reaches to 10, a special function should be called and timer value should be reset.

If timer doesn’t reach to 10 and gets call for the second time, it should be reset to zero and start again.

Expand|Select|Wrap|Line Numbers
  1. Counter=0;
  2. Void function()
  3. {
  4. if (counter == 0)
  5.     time (&start);
  6. ::counter++;
  7.  
  8. for ( ; ; )
  9. {
  10. if (elapsed_time = difftime(time( &finish ), start )>10)
  11.     {
  12.         cout << "Time out Has Occurd" << endl;
  13.         time( &start );
  14.     }             
  15. }
  16. }
  17.  
  18.  
But this function is blocking other functions.

Is there any other command that can help me?
What is that special function?

Is user inputing something when function is called?

If it input u could do something like:

Expand|Select|Wrap|Line Numbers
  1. time(&start);
  2.  
  3. do{
  4.            spec_func();
  5.            time(&finish);
  6.            time=finish-start;
  7.            if(time<=10)print_out_time();
  8.            else time=0;
  9.  
  10. }while(user_don't_press_any_key or while function executes);
Savage
May 1 '07 #2
mohtasham1983
3 New Member
it is a function used to send a packet.

Actually it's a thread which should sends a packet and if time out happens, it should retransmit it. I cannot use global variable either, since it may conflict with other threads.

By the way, there is a method using waitable timer in
http://msdn2.microsoft.com/en-us/library/ms687008.aspx

But I am not sure if it suits my needs, because my program is a pure c++ one.

I even tried to use settimer function, but i couldn't figure out how to use it.
May 1 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Research Win32 CreateThread() and put your timer in a function whose address and arguments are passed to CreateThread(). The timer will happily run on its own thread without bothering the main thread.

You will find your thread must be a ThreadProc (more research) and you may have to call your real function from the ThreadProc that you used with CreateThread().

You can communicate with this thread using WaitForSingleObject() and an event (more research on CreateEvernt()).
May 1 '07 #4
mohtasham1983
3 New Member
I am trying to find a way to solve it without using win32 special APIs.

I tried so:

Expand|Select|Wrap|Line Numbers
  1. class Timer {
  2.     clock_t counter;
  3. public:
  4.     Timer(): counter(0) {};
  5.  
  6.     bool elasped(clock_t ms)
  7.     {
  8.         clock_t tick = std::clock();
  9.  
  10.         if(tick - counter >= ms)
  11.         {
  12.              counter = tick;
  13.              return true;
  14.         }
  15.  
  16.         return false;
  17.     }
  18. };
  19.  
and then i placed this inside my thread:

Expand|Select|Wrap|Line Numbers
  1. Timer timer;
  2.  
  3. while(true) {
  4.     if(timer.elasped(20000)) {
  5.         log(ServerPacketBuffer,1,1,0);
  6.         udp_server_send(send_buffer, packet_length, client[slavenum].clientAddr);    // Send packet
  7.         cout << "Time out Has Occurd" << endl;
  8.     }
  9. }        
  10.  
This one is non blocking, but i cannot reset it
May 1 '07 #5
Savage
1,764 Recognized Expert Top Contributor
I am trying to find a way to solve it without using win32 special APIs.

I tried so:

Expand|Select|Wrap|Line Numbers
  1. class Timer {
  2.     clock_t counter;
  3. public:
  4.     Timer(): counter(0) {};
  5.  
  6.     bool elasped(clock_t ms)
  7.     {
  8.         clock_t tick = std::clock();
  9.  
  10.         if(tick - counter >= ms)
  11.         {
  12.              counter = tick;
  13.              return true;
  14.         }
  15.  
  16.         return false;
  17.     }
  18. };
  19.  
and then i placed this inside my thread:

Expand|Select|Wrap|Line Numbers
  1. Timer timer;
  2.  
  3. while(true) {
  4.     if(timer.elasped(20000)) {
  5.         log(ServerPacketBuffer,1,1,0);
  6.         udp_server_send(send_buffer, packet_length, client[slavenum].clientAddr);    // Send packet
  7.         cout << "Time out Has Occurd" << endl;
  8.     }
  9. }        
  10.  
This one is non blocking, but i cannot reset it
How do u mean u cannot reset it?

Have u tryed initialize start and finish time again?

Savage
May 1 '07 #6
weaknessforcats
9,208 Recognized Expert Moderator Expert
I am trying to find a way to solve it without using win32 special APIs.
By default any C++ program is single-threaded. You simply can't do two things at once. You will need to create a worker thread (daemon) that can run independently of the main thread.

Threads are controlled by the OS. Wins32 is your interface to the OS.
May 2 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2742
by: Daniel Bickett | last post by:
Hello, I am writing an application using two event-driven libraries: wxPython, and twisted. The first problem I encountered in the program is the confliction between the two all-consuming...
4
5068
by: Paul Smith | last post by:
I have been given an unusual task to create a Windows Service to do something periodically to make the screensaver timer reset. We have a general policy of locking computers automatically after 10...
7
2936
by: callmebill | last post by:
I have a server that right now runs infinitely. I'd like to make it die after some amount of time. I was thinking of having a timebomb thread that starts when the server starts. The timebomb...
4
4009
by: Anthony Boudouvas | last post by:
Hi to all, i have a form with 2 System.Windows.Forms.Timer objects. One fire every 5 seconds and the other every 10 seconds, the both take actions in two hashtables declared in same form. ...
6
4091
by: roger beniot | last post by:
I have a program that launches multiple threads with a ThreadStart method like the following (using System.Net.Sockets.Socket for UDP packet transfers to a server): ThreadStart pseudo code: ...
20
1997
by: Charles Law | last post by:
Consider the following scenario: A data packet is sent out of a serial port and a return packet is expected a short time later. The application sending the packet needs to send another packet...
4
1647
by: monade | last post by:
Hi, i have a file stream std::ofstream file and some data std::string data to serialize in a performance sensitive code section: file << data << '\n' I know that the data will be flushed when...
2
1549
by: wongjoekmeu | last post by:
Dear All, I have some a program in which I link a static library. The static library has a initialize() and uninitialized() function. Now when I call the initialize function a thread is being...
0
1018
by: =?Utf-8?B?VG9tbXkgTG9uZw==?= | last post by:
I apologise in advance for this question has been answered at least in this thread: http://forums.msdn.microsoft.com/en-US/netfxnetcom/thread/9b7d3a8f-b964-45a7-ba7d-1a567f406757 Unfortunately...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.