Improve multithreading support for OneService (faster, dynamic adjustment of thread count based on HW concurrency).
This commit is contained in:
parent
f6450cd7e1
commit
90631adb9b
3 changed files with 87 additions and 71 deletions
|
@ -32,6 +32,8 @@
|
|||
#include <condition_variable>
|
||||
#include <chrono>
|
||||
|
||||
#include "Thread.hpp"
|
||||
|
||||
namespace ZeroTier {
|
||||
|
||||
/**
|
||||
|
@ -52,6 +54,23 @@ public:
|
|||
c.notify_one();
|
||||
}
|
||||
|
||||
inline void postWait(T t,unsigned long maxQueueSize)
|
||||
{
|
||||
for(;;) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
if (q.size() < maxQueueSize) {
|
||||
q.push(t);
|
||||
c.notify_one();
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!r)
|
||||
break;
|
||||
Thread::sleep(1);
|
||||
}
|
||||
}
|
||||
|
||||
inline void stop(void)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m);
|
||||
|
@ -98,8 +117,8 @@ public:
|
|||
private:
|
||||
volatile bool r;
|
||||
std::queue<T> q;
|
||||
std::mutex m;
|
||||
std::condition_variable c;
|
||||
mutable std::mutex m;
|
||||
mutable std::condition_variable c;
|
||||
};
|
||||
|
||||
} // namespace ZeroTier
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue