mirror of
https://github.com/YACReader/yacreader
synced 2025-06-03 09:08:20 -04:00
ConcurrentQueue::jobsLeft: int => std::size_t
This data member's type can be unsigned because its value is never negative now. Matching std::queue::size_type allows to improve type safety, get rid of a static_cast and remove two assertions. The only downside is a slight increase of sizeof(ConcurrentQueue).
This commit is contained in:
parent
d8a6b7f432
commit
d026050d49
@ -3,7 +3,6 @@
|
|||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <limits>
|
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
@ -60,9 +59,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
const auto size = oldQueue.size();
|
const auto size = oldQueue.size();
|
||||||
assert(size <= std::numeric_limits<int>::max());
|
|
||||||
if (size != 0)
|
if (size != 0)
|
||||||
finalizeJobs(static_cast<int>(size));
|
finalizeJobs(size);
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -79,7 +77,7 @@ public:
|
|||||||
private:
|
private:
|
||||||
std::vector<std::thread> threads;
|
std::vector<std::thread> threads;
|
||||||
std::queue<std::function<void(void)>> _queue;
|
std::queue<std::function<void(void)>> _queue;
|
||||||
int jobsLeft; //!< @invariant jobsLeft >= _queue.size()
|
std::size_t jobsLeft; //!< @invariant jobsLeft >= _queue.size()
|
||||||
bool bailout;
|
bool bailout;
|
||||||
std::condition_variable jobAvailableVar;
|
std::condition_variable jobAvailableVar;
|
||||||
std::condition_variable _waitVar;
|
std::condition_variable _waitVar;
|
||||||
@ -115,11 +113,11 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void finalizeJobs(int count)
|
void finalizeJobs(std::size_t count)
|
||||||
{
|
{
|
||||||
assert(count > 0);
|
assert(count > 0);
|
||||||
|
|
||||||
int remainingJobs;
|
std::size_t remainingJobs;
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(jobsLeftMutex);
|
std::lock_guard<std::mutex> lock(jobsLeftMutex);
|
||||||
assert(jobsLeft >= count);
|
assert(jobsLeft >= count);
|
||||||
@ -127,7 +125,6 @@ private:
|
|||||||
remainingJobs = jobsLeft;
|
remainingJobs = jobsLeft;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(remainingJobs >= 0);
|
|
||||||
if (remainingJobs == 0)
|
if (remainingJobs == 0)
|
||||||
_waitVar.notify_all();
|
_waitVar.notify_all();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user