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

Implement post_timeout

parent b3f17dea
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -64,6 +64,14 @@ void stuff() {
t.join();
}
void timeoutTest() {
jlworkers::Workers eventLoop;
eventLoop.start();
eventLoop.post_timeout([] { std::cout << "Hello\n"; }, 1000);
eventLoop.stop();
}
int main(int argc, const char** argv) {
// Test workerthread
......@@ -72,6 +80,9 @@ int main(int argc, const char** argv) {
// Test eventloop
workersTest(1);
// Test timeout
std::cout << "Testing post_timeout\n";
timeoutTest();
return 0;
}
#include <chrono>
#include <condition_variable>
#include <cstdlib>
#include <functional>
......@@ -23,11 +24,20 @@ namespace jlworkers {
}
void Workers::post(const std::function<void()>& f) {
//std::cout << "Adding task to queue, size before: " << m_queue.size();
std::unique_lock<std::mutex> lock(m_runningMutex);
this->m_queue.emplace_back(f);
m_runningCondition.notify_all();
//std::cout << ", size after: " << m_queue.size() << "\n";
}
/* Crude implementation of timeout method.
*/
void Workers::post_timeout(const std::function<void()>& f, int timeout) {
std::unique_lock<std::mutex> lock(m_runningMutex);
this->m_queue.emplace_back([f, timeout] {
std::this_thread::sleep_for(std::chrono::milliseconds(timeout));
f();
});
m_runningCondition.notify_all();
}
void Workers::start() {
......
......@@ -35,6 +35,7 @@ namespace jlworkers {
Workers(int numThreads);
void post(const std::function<void ()>&);
void post_timeout(const std::function<void ()>&, int timeout);
void start();
void stop();
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment