Skip to content

Commit

Permalink
debug/benchmark information has been added into Worker
Browse files Browse the repository at this point in the history
  • Loading branch information
SuprunenkoO committed Mar 29, 2018
1 parent 626006d commit 8db6bb8
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion include/thread_pool/worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include <atomic>
#include <thread>
#include <iostream>

#include <chrono>

namespace tp
{
Expand Down Expand Up @@ -67,6 +70,15 @@ class Worker
*/
static size_t getWorkerIdForCurrentThread();

int dump()
{
std::cout << " idx-> j->" << m_thread.joinable() << " " << m_thread.get_id() << " cnt->( " << m_count << " + " << m_steal_count << " )\n";
long long int p1 = std::chrono::high_resolution_clock::now().time_since_epoch().count();
p1 -= p;
std::cout << "\t\t" << p1 << "\t" << p << "\n";
return m_count + m_steal_count;
}

private:
/**
* @brief threadFunc Executing thread function.
Expand All @@ -78,6 +90,9 @@ class Worker
Queue<Task> m_queue;
std::atomic<bool> m_running_flag;
std::thread m_thread;
////
int m_count = 0, m_steal_count = 0;
long long int p;
};


Expand All @@ -97,6 +112,7 @@ inline Worker<Task, Queue>::Worker(size_t queue_size)
: m_queue(queue_size)
, m_running_flag(true)
{
p = std::chrono::high_resolution_clock::now().time_since_epoch().count();
}

template <typename Task, template<typename> class Queue>
Expand All @@ -113,6 +129,9 @@ inline Worker<Task, Queue>& Worker<Task, Queue>::operator=(Worker&& rhs) noexcep
m_queue = std::move(rhs.m_queue);
m_running_flag = rhs.m_running_flag.load();
m_thread = std::move(rhs.m_thread);
m_count = std::move(rhs.m_count);
m_steal_count = std::move(rhs.m_steal_count);
p = std::move(rhs.p);
}
return *this;
}
Expand Down Expand Up @@ -158,7 +177,8 @@ inline void Worker<Task, Queue>::threadFunc(size_t id, Worker* steal_donor)

while (m_running_flag.load(std::memory_order_relaxed))
{
if (m_queue.pop(handler) || steal_donor->steal(handler))
// if ( m_queue.pop(handler) || steal_donor->steal(handler) )
if ( (m_queue.pop(handler)? ++m_count, true : false) || (steal_donor->steal(handler)? ++m_steal_count, true : false) )
{
try
{
Expand Down

0 comments on commit 8db6bb8

Please sign in to comment.