-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJobQueue.cpp
More file actions
69 lines (57 loc) · 1.16 KB
/
JobQueue.cpp
File metadata and controls
69 lines (57 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "JobQueue.h"
#include "IJob.h"
job_queue::job_queue(int i_id) : id_(i_id),
shutdown_requested_(false)
{
}
job_queue::~job_queue()
{
#ifdef BUILD_DEBUG
size_t num_unfinished_jobs = 0;
#endif
while (!job_queue_.empty())
{
delete job_queue_.front();
job_queue_.pop();
}
}
bool job_queue::add_job(i_job* i_new_job)
{
// validate inputs
bool result = false;
if (shutdown_requested_ == false)
{
{
std::lock_guard<std::mutex> lock(queue_mutex_);
job_queue_.push(i_new_job);
}
result = true;
start_searching_.notify_one();
}
return result;
}
i_job* job_queue::get_job()
{
if (shutdown_requested_)
{
return nullptr;
}
i_job* job = nullptr;
if (job_queue_.empty())
{
std::unique_lock<std::mutex> u_lock(queue_mutex_);
start_searching_.wait(u_lock);
u_lock.unlock();
}
if (shutdown_requested_)
{
return nullptr;
}
std::lock_guard<std::mutex> lock(queue_mutex_);
if (!job_queue_.empty())
{
job = job_queue_.front();
job_queue_.pop();
}
return job;
}