2007年3月31日星期六

Time to Wakeup a Thread in Linux

Abstract:
In multithreaded programming, it is sometimes desirable to have some process blocked for a specific event to happen. For better response time, there maybe two method to implement this, one approach is that the process spinned on a flag indicating the arrival of the event, and the other is the process blocked on a conditional variable and wakenup by another thread when the event happens. Here we compare the effectiveness of these two methods.

Test Platform:
OS: Linux 2.6.18-1.2798.fc6 #1 SMP x86_64 GNU/Linux
CC: gcc version 4.1.1 20061011 (Red Hat 4.1.1-30)
CFLAGS: -Wall -O2
LDFLAGS: -lrt -lpthread
CPU: Dual Core AMD Opteron(tm) Processor 270
System: 2 processors

Response Time(averaged among 500 run):
usleep(1): 39928260.000000 ns
clock_nanosleep(1): 39835940.000000 ns
pthread_cond_wait: 346940.000000 ns

Source Code for Test Program:
I can't find anywhere in this blog to upload files, so until I find it, the source code may not be publicly available. Maybe you can teach me howto upload files on blogger.com. Just drop me a email.

Conclusions:
Neither usleep(1) nor clock_nanosleep(1) is good for response time, use pthread_cond_wait() is a good choice with the additional advantage of releasing CPU time for other processes/threads.

没有评论: