more optimizations

pull/1/head
Jeff Becker 6 years ago
parent 3bcc5604a6
commit 284c55f5aa
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -15,7 +15,7 @@ Pool::Pool(size_t workers) {
lock, [this] { return this->stop || !this->jobs.empty(); });
if (this->stop && this->jobs.empty()) return;
job = std::move(this->jobs.front());
this->jobs.pop();
this->jobs.pop_front();
}
// do work
job.work(job.user);
@ -39,14 +39,14 @@ void Pool::Join() {
for (auto &t : threads) t.join();
}
void Pool::QueueJob(llarp_thread_job job) {
void Pool::QueueJob(const llarp_thread_job & job) {
{
lock_t lock(queue_mutex);
// don't allow enqueueing after stopping the pool
if (stop) throw std::runtime_error("enqueue on stopped ThreadPool");
jobs.emplace(job);
jobs.emplace_back(job);
}
condition.notify_one();
}

@ -5,7 +5,7 @@
#include <condition_variable>
#include <mutex>
#include <queue>
#include <deque>
#include <thread>
#include <vector>
@ -15,10 +15,10 @@ typedef std::mutex mtx_t;
typedef std::unique_lock<mtx_t> lock_t;
struct Pool {
Pool(size_t sz);
void QueueJob(llarp_thread_job job);
void QueueJob(const llarp_thread_job & job);
void Join();
std::vector<std::thread> threads;
std::queue<llarp_thread_job> jobs;
std::deque<llarp_thread_job> jobs;
mtx_t queue_mutex;
std::condition_variable condition;

@ -41,8 +41,8 @@ int main(int argc, char *argv[]) {
tp = llarp_init_threadpool(2);
b_main.num = 100000;
b_main.jobs = 1000;
b_main.num = 500000;
b_main.jobs = 10;
b_main.completed = 0;
llarp_sharedkey_t key;
b_main.crypto.randbytes(key, sizeof(llarp_sharedkey_t));

Loading…
Cancel
Save