thread didnt awake from sleep - c++

I have a multi-threaded application but sometimes one thread that is suppose to wake up after 10 seconds and do some work is not getting woken up from sleep or is starving.
It only happens sporadically.
//ACE task svc method
int svc (void)
{
while(true)
{
ACE_DEBUG((MY_INFO "sleep\n"));
sleep(10);
ACE_DEBUG((MY_INFO "awake for HB\n"));
_csender.sendHeartBeat();
}
return 0;
}
The last line in the log is:
2012-06-12 11:34:20.807272|INFO|sleep
Thread either didnt awake for 15 seconds or didn't do any work after its awaken until the 15th second, so the application closed.
There are total 6 threads in the application, all started with same priority.
One of the thread is very busy, it receives a lot of market data and processes it, but does not send anything out on socket. The thread above is the only thread sending data out and both the receiver and sender threads are sharing the same socket object.
This is on red hat linux 5.3.
any ideas what could be the issue?

'Busy thread is prints 2 ACE_DEBUGs every 2 microseconds' - so probably clogging up the output queue of the debugger and preventing this thread from getting in to queue up its 'sleep\n'.
That, and/or you have prioritized down this thread and it cannot get a core, as other posters have indicated.
The sleep(10) is almost an irellevance.

Sleep does not give any guarantees about maximum time spent sleeping, i.e. you sleep for at least that amount of time, or any time longer. I don't know if it's plausible to expect a 5 second delay though, sounds far too long.

I think its in ACE_DEBUG. Did you try a printf with a flush right after the sleep?

Related

What exactly does alarm() and sleep() do?

If my timeslice is 3 seconds, I am guessing the alarm stops the execution of a process every three seconds. What does sleep do? Does it put the process to sleep for 3 seconds? This does not make sense to me - what if there are a lot of processes? Wouldn't it have to sleep for longer?
I am doing this with the round robin stimulation:
while (head!=NULL)
{
alarm(TIMESLICE);
sleep(TIMESLICE);
}
cout<<"no processes left"<<endl;
The code works, but I just want to understand what exactly is going on as I am new to this concept.
I am guessing the alarm stops the execution of a process every three seconds.
Sort of. It arranges for a signal to be sent to the process in three seconds. The process can then continue normally and can even ignore the signal if it wants to.
What does sleep do? Does it put the process to sleep for 3 seconds?
Correct.
This does not make sense to me - what if there are a lot of processes? Wouldn't it have to sleep for longer?
No. Even a process that never sleeps isn't guaranteed to get the CPU all the time. A process that isn't sleeping may or may not be scheduled to run on a core at any particular time. Once it's no longer sleeping, it's ready-to-run, and the scheduler will make the decision of when and for how to long to let it use what core.

Worker and interrupter thread coordination in Boost

Kind of C++ and even more Boost noobie here.
I have successfully managed to create two threads, based on this example, one Worker and one Interrupter. The latter sleeps, via this_thread::sleep_for for 50 seconds and interrupts the Worker if he is not done within that time, with proper use of while !this_thread::interruption_requested() and thread::interrupt(). So it's a very nice and easy timeout mechanism. My problem is how to stop Interrupter sleep sooner? I mean if Worker finishes before that 50 seconds, I do a this_thread::yield or this_thread::interruption_requested (both seem to work) but Interrupter is still asleep and have to wait for 50 seconds for him to wake up:(
Is there any way to do this timeout mechanism, but if Worker is done with his work before sleep is over, to notify/wake up Interrupter?
PS: Do I need some sort of synchronization when all are done?
Your interrupter thread should not sleep unconditionally for 50 seconds but wait for conditional variable for 50 seconds, if worker thread finishes earlier it will signal conditional var and interrupter would wake up. You can use either std::conditional_variable or one from boost with the same name

How to stop select() immediately on closing the worker thread? [duplicate]

I have a loop which basically calls this every few seconds (after the timeout):
while(true){
if(finished)
return;
switch(select(FD_SETSIZE, &readfds, 0, 0, &tv)){
case SOCKET_ERROR : report bad stuff etc; return;
default : break;
}
// do stuff with the incoming connection
}
So basically for every few seconds (which is specified by tv), it reactivates the listening.
This is run on thread B (not a main thread). There are times when I want to end this acceptor loop immediately from thread A (main thread), but seems like I have to wait until the time interval finishes..
Is there a way to disrupt the select function from another thread so thread B can quit instantly?
The easiest way is probably to use pipe(2) to create a pipe and add the read end to readfds. When the other thread wants to interrupt the select() just write a byte to it, then consume it afterward.
Yes, you create a connected pair of sockets. Then thread B writes to one side of socket and thread A adds the other side socket to select. So once B writes to socket A exits select, do not forget to read this byte from socket.
This is the most standard and common way to interrupt selects.
Notes:
Under Unix, use socketpair to create a pair of sockets, under windows it is little bit tricky but googling for Windows socketpair would give you samples of code.
Can't you just make the timeout sufficiently short (like 10ms or so?).
These "just create a dummy connection"-type solution seem sort of hacked. I personally think that if an application is well designed, concurrent tasks never have to be interrupted forcefully, the just has worker check often enough (this is also a reason why boost.threads do not have a terminate function).
Edit Made this answer CV. It is bad, but it might help other to understand why it is bad, which is explained in the comments.
You can use shutdown(Sock, SHUT_RDWR) call from main thread to come out of waiting select call which will also exit your another thread before the timeout so you don't need to wait till timeout expires.
cheers. :)

epoll performance for smaller timeout values

I have a single thread server process that watches few (around 100) sockets via epoll in a loop, my question is that how to decide the optimum value of epoll_wait timeout value, since this is a single threaded process, everything is triggered off epoll_wait , if there is no activity on sockets, program remains idle, my guess is that if i give too small timeout, which causes too many epoll_wait calls there is no harm because even though my process is doing too many epoll_wait calls, it would be sitting idle otherwise, but there is another point, I run many other processes on this (8 core) box, something like 100 other process which are clients to this process, I am wondering how timeout value impacts cpu context switiching, i.e if i give too small timeout which results in many epoll_wait call will my server process be put in waiting many more times vs when I give a larger timeout value which results in fewer epoll_wait calls.
any thoughts/ideas.
Thanks
I believe there is no good reason to make your process wake up if it has nothing to do. Simply set the timeout to when you first need to do something. For example, if your server has a semantic of disconnecting a client after N seconds of inactivity, set the epoll timeout to the time after the first client would have to be disconnected assuming no activity. In other words, set it to:
min{expire_time(client); for each client} - current_time
Or, if that's negative, you can disconnect at least one client immediately. In general, this works not only for disconnecting clients; you can abstract the above into "software timers" within your application.
I'm failing to see this compromise you've mentioned. If you use a timeout any smaller than you have to, you'll wake up before you have to, then, presumably, go back to sleep because you have nothing to do. What good does that do? On the other hand, you must not use a timeout any larger than what you have to - because that would make your program not respect the disconnect timeout policy.
If your program is not waiting for any time-based event (like disconnecting clients), just give epoll_wait() timeout value -1, making it wait forever.
UPDATE If you're worried that this process being given less CPU when other processes are active, just give it lower nice value (scheduler priority). On the other hand, if you're worried that your server process will be swapped out to disk in favour of other processes when it's idle, it is possible to avoid swapping it out. (or you can just lower /proc/sys/vm/swappiness, affecting all processes)

C++: More precise timing intervals

I need to periodically do a particular task and am currently using nanosleep.
The task needs to be run every second or every 10 seconds.
Is there a better way to do this than:
while(true)
{
doTask();
sleep();
}
Walter
One of the options could be to create a thread that will do the task with specified timeout.
You can use a thread library to create a thread which handle run the doTask(). Your main thread just keeps sleeping and runs every 1 second or 10 seconds.
This can be done with a QTimer and a QRunnable.
http://doc.qt.nokia.com/latest/qtimer.html
According to the dock, the resolution is around 1 ms in most cases. For your need, this should be sufficient.