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

Remove debug output, tweak tests

parent d4bcab61
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -9,24 +9,26 @@
#include "workers.hpp"
auto helloWorld = [](int i) {
std::cout << "Task " << i << "\n";
};
void helloWorld() {
std::cout << "Task defined as regular (static) function\n";
}
void workersTest() {
jlworkers::Workers workerThread(16);
void workersTest(int threads) {
jlworkers::Workers workerThread(threads);
std::mutex printMutex;
workerThread.post([]() {
/*workerThread.post([]() {
std::cout << "Hello, World!\n";
});
});*/
workerThread.post(helloWorld);
workerThread.start();
workerThread.post([]() {
// Let's delay this a bit
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::cout << "Hello again, World!\n";
std::cout << "Anonymous function with 500ms sleep before print\n";
});
for (int i = 0; i < 10; i++)
......@@ -35,8 +37,6 @@ void workersTest() {
<< ", thread id " << std::this_thread::get_id() <<"\n";
});
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
workerThread.stop();
}
......@@ -66,7 +66,12 @@ void stuff() {
int main(int argc, const char** argv) {
workersTest();
// Test workerthread
workersTest(16);
// Test eventloop
workersTest(1);
return 0;
}
......@@ -31,11 +31,12 @@ namespace jlworkers {
}
void Workers::start() {
std::cout << "Starting worker thread, " << m_maxThreadCount
<< " thread(s)\n";
m_running = true;
std::cout << "Starting task runner thread\n";
m_runnerThread = std::thread([this] {
while (m_running) {
std::cout << "Queue: " << m_queue.size() << "\n";
//while (!m_queue.empty()) {
while (m_runningThreadCount < m_maxThreadCount && !m_queue.empty()) {
// Fetch next task in queue
......@@ -54,21 +55,17 @@ namespace jlworkers {
//}
// Wait for any status updates
std::cout << "Create lock and wait for update\n";
std::unique_lock<std::mutex> lock(m_runningMutex);
m_runningCondition.wait(lock);
}
std::cout << "Queue empty\n";
for (; !m_workers.empty(); m_workers.pop_back()) m_workers.back().join();
});
}
void Workers::stop() {
/* Sohuld we wait for queue to clear, or do we want to stop adding tasks?
* Let's do the latter for now..
* Let's do the former for now..
*/
std::cout << "Stop runner thread; locking mutex\n";
while (!m_queue.empty()) {
std::unique_lock<std::mutex> lock(this->m_runningMutex);
m_runningCondition.wait(lock);
......@@ -78,13 +75,10 @@ namespace jlworkers {
{
std::unique_lock<std::mutex> lock(this->m_runningMutex);
this->m_running = false;
std::cout << "Notify threads\n";
m_runningCondition.notify_all();
}
std::cout << "Wait for runner to join\n";
m_runnerThread.join();
std::cout << "Runner done!\n";
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment