I am using pthreads
and am trying to put a thread to sleep for X seconds.
I have tried sleep()
, nanosleep()
, pthread_cond_timedwait()
(waiting on a fake mutexlock for X seconds), pthread_yield()
, checking the time every time the thread wakes up, to see if it's its time to run, and finally just busy waiting for X seconds.
The problem is that, although all these methods do work fine considering the result, they do not give the performance results I would expect. Performance is measured in throughput (replies per second) and not in time.
In my configuration I am running 3 software threads over 2 simulated physical cores (VirtualBox running Ubuntu Server). I would expect to get better performance when putting 1 of the 3 threads to sleep, as the other 2 threads have one dedicated physical core to run on. However, I get the exact opposite behavior (with all my approaches): increasing the sleep time gives worse performance. (Note that the sleeping thread's workload is 130-300ms.)
Could it be because of the virtualization (the host machine has 4 cores)? Could it be because the methods I'm using operate in a similar way to my approach using pthread_yield
?
How are sleep()
and nanosleep()
implemented? Do they use signals? Do you think an implementation using a signal handler and alarm()
would be a better approach? What happens when I call pthread_cond_wait()
? How is it implemented?
I have also tried using chrt
to change the scheduling policies, with no luck.
time
command), that demonstrates your issue? – Jay Sullivan