Skip to content

Commit

Permalink
Merged v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
iminkin committed Feb 2, 2025
1 parent f3ed223 commit 7e85995
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/graphconstructor/taskqueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#ifndef _TASK_QUEUE_H_
#define _TASK_QUEUE_H_
#define __STDC_LIMIT_MACROS

#include <mutex>
#include <deque>
#include "common.h"

namespace TwoPaCo
{
class TaskQueue
{
public:
TaskQueue()
{

}

bool try_push(const Task & task)
{
bool ret = false;
mutex_.lock();

if (queue_.size() < capacity_)
{
queue_.push_back(task);
ret = true;
}

mutex_.unlock();
return ret;
}

bool try_pop(Task& task)
{
bool ret = false;
mutex_.lock();
if (queue_.size() > 0)
{
task = queue_.front();
queue_.pop_front();
ret = true;
}

mutex_.unlock();
return ret;
}

void set_capacity(size_t capacity)
{
mutex_.lock();
capacity_ = capacity;
mutex_.unlock();
}

size_t size()
{
mutex_.lock();
size_t ret = queue_.size();
mutex_.unlock();
return ret;
}

size_t capacity() const
{
return capacity_;
}

private:
size_t capacity_;
std::mutex mutex_;
std::deque<Task> queue_;
DISALLOW_COPY_AND_ASSIGN(TaskQueue);
};

typedef std::unique_ptr<TaskQueue> TaskQueuePtr;
}

#endif

0 comments on commit 7e85995

Please sign in to comment.