2

I need to achieve following scenario , please help me with this:

  1. A queue will be maintained, which will contain some objects.
  2. When an object is inserted in the queue, a timer will be started for that object.
  3. A signal will be sent whenever an event occurs on that object (the event can be like a timeout, some error etc.).
  4. The event which occurred for this object will be captured and an object Id will be received. The object Id is required to distinguish the object present in the queue and to take appropriate actions.

My approach which did not work for large number of objects is the following:

  1. create a thread for each object which will pass an object Id to the thread_function1(void *object_id); and use sleep() as a timer for that object.
  2. Now for other object I have to do same thing by creating new thread_function2(void *object_id); The above approach will not work as I wanted this to be dynamic.

Operating system: Linux.

I want to implement this in c++ , I have never implemented this so I need your help and guidance to move forward. Any online tutorial, reference will be of great help. Thanks in advance.

3
  • Neither the C++ language nor its standard library implements timers. You'll need to be specific about what operating system you target. And show what you've tried to make work and explain why it didn't work. Aug 11, 2013 at 16:53
  • added information required Aug 11, 2013 at 17:04
  • 3
    Rather than creating a zillion sleeping threads, create a list of future events. You will need just one waiting "thing" (a thread, a system timer, or whatever) programmed to wake up at the next event. If another event gets inserted and it's closer than the current nearest one, cancel the wait and start another one.
    – n. m.
    Aug 11, 2013 at 18:04

3 Answers 3

2

Back in 2004 I started writing a series of blog posts called "Practical Testing" which demonstrated how to test a C++ timer implementation. The series consists of many versions of a fully functional light-weight timer queue which uses a single thread to service the timers (you may, of course, pass the timer event off to a thread pool once it fires, but the queue itself needs just one thread to work).

The code is for Windows, so you'd have to adjust it somewhat I expect.

The code was designed to work with many thousands of network connections where each connection potentially had one or more timer associated with it. It's been used by systems with upwards of 10,000 connections and works well and is efficient.

In episode 23 I add an alternative implementation which uses a timer wheel rather than a timer queue and discuss the trade off required to obtain the performance increase that the new data structure gives.

The series starts here.

If you just want the latest working code then that can be downloaded from episode 31, here.

The code has good unit test coverage (I hope, after all that was the focus of the blog posts) and the tests can be downloaded with the code.

0

You need to use an event loop, for example boost::asio timer tutorials or libevent events.

0

The short answer is: Don't use sleep(). It's such an awkward old interface, that you just should not use it anymore in any code.

In particular, sleep() is not the least thread-aware. Rather, use functions from the pthread library for your timing needs. For example, you could use pthread_cond_timedwait() to limit the amount of time, that a thread is waiting for a certain event.

Unfortunately, the people, that created the pthread library, forgot to also create pthread-compatible versions of standard system calls like read() or write(). So programming a thread, that waits for any of the following:
A) Another thread signals a certain state
B) Data is received on a network connection
C) Timeout
is still a mess. One solution is to use select() and/or poll() to wait for the data (case B) or timeout (case C), and use a pipe/fifo back to yourself to send signals from another thead (case A). For that, add the reading end of the pipe into the select() set, and make the other thread write a byte to it, if it needs to signal a condition.

The alternative solution to this A/B/C scenario is to have a "reader helper" thread, that indefinitely waits for network data (case B), while the main thread uses pthread_cond_timedwait() for A and C. The helper thread will signal the arrival of data via pthread_cond_signal, thus basically converting case B to case A. BTW: If the main thread at any time chooses to abort the connection, it may just forcefully close() the network connection, which will also terminate the read() call of the waiting helper thread.

2
  • -1 'The short answer is: Don't use sleep(). It's such an awkward old interface, that you just should not use it anymore in any code.' Aug 11, 2013 at 21:10
  • @Martin James: Really, sleep(3) on unix has so many side effects (like playing with the alarm() timer) and problems (like using signals, which don't interact well at all with threads, like having only second-resolution, like having no clear restart syntax, if the sleep is interrupted by another signal etc.) that one should just avoid that call completely. There is nanosleep() now, which is much more well-defined and with much less side effects.
    – Kai Petzke
    Aug 12, 2013 at 7:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.