8000 use std::lock_guard instead of std::unique_lock by mjopenglsdl · Pull Request #24 · mtrebi/thread-pool · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

use std::lock_guard instead of std::unique_lock #24

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and cont 8000 act its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions include/SafeQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
#include <mutex>
#include <queue>

// Thread safe implementation of a Queue using a std::queue

// Thread safe implementation of a Queue using an std::queue
template <typename T>
class SafeQueue {
class SafeQueue
{
private:
std::queue<T> m_queue;
std::mutex m_mutex;
Expand All @@ -24,22 +26,22 @@ class SafeQueue {


bool empty() {
std::unique_lock<std::mutex> lock(m_mutex);
std::lock_guard <std::mutex> lock(m_mutex);
return m_queue.empty();
}

int size() {
std::unique_lock<std::mutex> lock(m_mutex);
std::lock_guard <std::mutex> lock(m_mutex);
return m_queue.size();
}

void enqueue(T& t) {
std::unique_lock<std::mutex> lock(m_mutex);
std::lock_guard <std::mutex> lock(m_mutex);
m_queue.push(t);
}

bool dequeue(T& t) {
std::unique_lock<std::mutex> lock(m_mutex);
std::lock_guard <std::mutex> lock(m_mutex);

if (m_queue.empty()) {
return false;
Expand Down
0