个人而言,我使用互斥锁来序列化访问列表,并通过在套接字上发送一个字节(由socketpair()生成)来唤醒用户。这可能不如信号量或条件变量有效,但它的优点是允许用户在select()/ poll()中阻塞。这样,除了数据队列之外,消费者还可以等待其他事情,如果需要的话。它还允许您在几乎所有操作系统上使用完全相同的排队代码,因为几乎每个操作系统都支持BSD套接字API。
伪代码如下:
// Called by the producer. Adds a data item to the queue, and sends a byte
// on the socket to notify the consumer, if necessary
void PushToQueue(const DataItem & di)
{
mutex.Lock();
bool sendSignal = (queue.size() == 0);
queue.push_back(di);
mutex.Unlock();
if (sendSignal) producerSocket.SendAByteNonBlocking();
}
// Called by consumer after consumerSocket selects as ready-for-read
// Returns true if (di) was written to, or false if there wasn't anything to read after all
// Consumer should call this in a loop until it returns false, and then
// go back to sleep inside select() to wait for further data from the producer
bool PopFromQueue(DataItem & di)
{
consumerSocket.ReadAsManyBytesAsPossibleWithoutBlockingAndThrowThemAway();
mutex.Lock();
bool ret = (queue.size() > 0);
if (ret) queue.pop_front(di);
mutex.Unlock();
return ret;
}
嗯,你是对的,我没有正确地问,我使用互斥量来访问数据,除了信号量。 – 2009-09-23 17:41:34
在queue.c文件中查看http://code.google.com/p/litm/的来源。 – jldupont 2009-09-25 10:17:49