Skip to content
Snippets Groups Projects
Commit 8c901050 authored by Joakim Skogø Langvand's avatar Joakim Skogø Langvand
Browse files

Alotta cleanup

parent 21a87b52
No related branches found
No related tags found
No related merge requests found
......@@ -17,34 +17,38 @@
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <mutex>
#include <thread>
#include <utility>
#include "workers.hpp"
namespace jlworkers {
/* Construct an event loop
*/
Workers::Workers() {
this->m_running = false;
this->m_runningThreadCount = 0;
this->m_runningTimeoutThreadCount = 0;
this->m_maxThreadCount = 1;
m_running = false;
m_runningThreadCount = 0;
m_runningTimeoutThreadCount = 0;
m_maxThreadCount = 1;
}
/* Construct a concurent worker thread
*/
Workers::Workers(int numThreads) {
this->m_running = false;
this->m_runningThreadCount = 0;
this->m_runningTimeoutThreadCount = 0;
m_running = false;
m_runningThreadCount = 0;
m_runningTimeoutThreadCount = 0;
// Ensure the number of threads is sane
this->m_maxThreadCount = numThreads > 0 ? numThreads : 1;
m_maxThreadCount = numThreads > 0 ? numThreads : 1;
}
/* Add a task to the queue
*/
void Workers::post(const std::function<void()>& f) {
std::unique_lock<std::mutex> lock(m_runningMutex);
this->m_queue.emplace_back(f);
m_queue.emplace_back(f);
m_runningCondition.notify_all();
}
......@@ -61,6 +65,8 @@ namespace jlworkers {
}));
}
/* Start the main thread
*/
void Workers::start() {
std::cout << "Starting worker thread, " << m_maxThreadCount
<< " thread(s)\n";
......@@ -93,19 +99,26 @@ namespace jlworkers {
});
}
/* Because the spec states there should be a stop() method
*/
void Workers::stop() {
if (m_running)
join();
}
void Workers::join() {
/* Sohuld we wait for queue to clear, or do we want to stop adding tasks?
* Let's do the former for now..
*/
while (!m_queue.empty() || m_runningTimeoutThreadCount) {
std::unique_lock<std::mutex> lock(this->m_runningMutex);
std::unique_lock<std::mutex> lock(m_runningMutex);
m_runningCondition.wait(lock);
}
// Scope this to avoid deadlock
{
std::unique_lock<std::mutex> lock(this->m_runningMutex);
this->m_running = false;
std::unique_lock<std::mutex> lock(m_runningMutex);
m_running = false;
m_runningCondition.notify_all();
}
......
......@@ -54,7 +54,7 @@ namespace jlworkers {
public:
Workers();
Workers(int numThreads);
~Workers() { if (m_running) join(); }
~Workers() { stop(); }
void post(const std::function<void ()>&);
void post_timeout(const std::function<void ()>&, int timeout);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment