我正在构建一个阻塞的并发队列(即,如果生产者被唤醒,消费者在队列中没有任何内容时会睡觉)。我的用例受到限制,因为出列操作将始终阻塞空队列;没有tryDequeue
。
我目前的实施只使用std::mutex
和std::condition_variable
。我想知道用无锁算法改进数据结构是否有意义。在队列为空的情况下,这可能没有意义,因为消费者无论如何都必须阻止。但是在队列非空的情况下,我能做些什么吗?
答案 0 :(得分:0)
我认为这个问题正在询问您是否应该解除锁定。
在现代系统上,仅当每个项目的工作时间基本上都在1毫秒以下或某种热闹的大规模锁争用情况下(没有讨论,Web服务器是一个示例),无锁才有意义。
通过查看条件变量的响应时间,您可以感觉到通知条件变量的时间:
#include <thread>
#include <array>
#include <mutex>
#include <iostream>
#include <condition_variable>
std::chrono::microseconds timestamp()
{
const auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch());
return microseconds;
}
struct measurement
{
std::chrono::microseconds sent, acknowledged, received;
measurement() :sent{ 0 }, acknowledged{ 0 }, received{ 0 } {}
};
int main(int argc, char** argv)
{
std::array<measurement, 15> notifications;
std::mutex notifications_m;
std::condition_variable notification_cv;
auto notifying_thread = std::thread([&] {
for (auto i = 0; i < notifications.size(); ++i)
{
{
std::unique_lock<std::mutex> lk(notifications_m);
notifications[i].sent = timestamp();
}
notification_cv.notify_one();
//Now we wait for the notification to be acknowledged
std::unique_lock<std::mutex> lk(notifications_m);
const auto check_that_acknowledged = [&] {return notifications[i].acknowledged != std::chrono::microseconds(0); };
notification_cv.wait(lk, check_that_acknowledged);
notifications[i].received = timestamp();
}
});
for (auto i = 0; i < notifications.size(); ++i)
{
{
std::unique_lock<std::mutex> lk(notifications_m);
const auto check_that_sent = [&] {return notifications[i].sent != std::chrono::microseconds(0); };
notification_cv.wait(lk, check_that_sent);
notifications[i].acknowledged = timestamp();
}
notification_cv.notify_one();
}
notifying_thread.join();
//
for (const auto& notification : notifications)
{
const auto difference_us = notification.received - notification.sent;
std::cout << difference_us.count() << " microseconds" << std::endl;
}
return 0;
}
即使在某些延迟最高的系统上,往返也需要大约0.1ms的时间: https://coliru.stacked-crooked.com/a/9598c83dd05e09b5