『C++Concurrency in Action』ノート15 a thread-safe queue with condition variable
標準std::queueで次のように変更しました.
1.pop()とtop()を関数に結合します.
2.try_を追加pop()関数は、popが成功するかどうかにかかわらず、すぐに返されます.
2.wait_を追加and_pop()関数は、要素がpopできるまで待機します.
不完全な簡略化サンプルコードは、次のとおりです.
1.pop()とtop()を関数に結合します.
2.try_を追加pop()関数は、popが成功するかどうかにかかわらず、すぐに返されます.
2.wait_を追加and_pop()関数は、要素がpopできるまで待機します.
不完全な簡略化サンプルコードは、次のとおりです.
template
class threadsafe_queue
{
private:
mutable std::mutex mut;// mutable
std::queue data_queue;
std::condition_variable data_cond;
public:
threadsafe_queue()
{}
threadsafe_queue(threadsafe_queue const& other)
{
std::lock_guard<:mutex> lk(other.mut);// mutex
data_queue = other.data_queue;
}
threadsafe_queue& operator=(const threadsafe_queue&) = delete;
void push(T new_value)
{
std::lock_guard<:mutex> lk(mut);
data_queue.push(new_value);
data_cond.notify_one();// ,
}
void wait_and_pop(T& value)
{
std::unique_lock<:mutex> lk(mut);
data_cond.wait(lk, [this] {return !data_queue.empty(); });
value = data_queue.front();
data_queue.pop();
}
std::shared_ptr wait_and_pop()
{
std::unique_lock<:mutex> lk(mut);
data_cond.wait(lk, [this] {return !data_queue.empty(); });
std::shared_ptr res(std::make_shared(data_queue.front()));
data_queue.pop();
return res;
}
bool try_pop(T& value)
{
std::lock_guard<:mutex> lk(mut);
if (data_queue.empty())
return false;
value = data_queue.front();
data_queue.pop();
return true;
}
std::shared_ptr try_pop()
{
std::lock_guard<:mutex> lk(mut);
if (data_queue.empty())
return std::shared_ptr();
std::shared_ptr res(std::make_shared(data_queue.front()));
data_queue.pop();
return res;
}
bool empty() const
{
std::lock_guard<:mutex> lk(mut);//
return data_queue.empty();
}
};
では、このスレッドの安全なキューに外部からアクセスするコードは、次のようになります.threadsafe_queue data_queue;
void data_preparation_thread()
{
while (more_data_to_prepare())
{
data_chunk const data = prepare_data();
data_queue.push(data);
}
}
void data_processing_thread()
{
while (true)
{
data_chunk data;
data_queue.wait_and_pop(data);
process(data);
if (is_last_chunk(data))
break;
}
}
外部で条件変数を使用して同期を保証する必要はありません.