mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 months ago
8 changed files with 103 additions and 90 deletions
@ -0,0 +1,51 @@ |
|||
#pragma once |
|||
|
|||
/// The concurrency test helper class implements the Executor and Racer based
|
|||
/// on the BS::thread_pool. The Executor allows you to submit multiple tasks
|
|||
/// and dispatch them to different threads. The Racer schedules the specified
|
|||
/// function to a certain number of threads at one time.
|
|||
|
|||
#include <functional> |
|||
#include <BS_thread_pool.hpp> |
|||
|
|||
namespace helper { |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
class Executor final { |
|||
public: |
|||
/// Specify the number of threads, default to the number of CPU cores.
|
|||
explicit Executor(const int num = 0) : pool_(num) {} |
|||
|
|||
/// Get the executor entry for submitting tasks.
|
|||
std::function<void(std::function<void()> &&func)> Entry(); |
|||
|
|||
~Executor(); |
|||
|
|||
private: |
|||
BS::thread_pool pool_; |
|||
}; |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
class Racer final { |
|||
public: |
|||
/// Create race tester with a specified number of concurrency.
|
|||
explicit Racer(const int num = 256) : race_num_(num), pool_(num) {} |
|||
|
|||
/// Start the racing test process with specified number.
|
|||
void Execute(std::function<void()> &&item); |
|||
|
|||
/// Get the number of concurrency in Racer.
|
|||
int RaceNum() const; |
|||
|
|||
~Racer(); |
|||
|
|||
private: |
|||
const int race_num_; |
|||
BS::thread_pool pool_; |
|||
}; |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
} // namespace helper
|
@ -0,0 +1,36 @@ |
|||
#include "helper/concurrent.h" |
|||
|
|||
using helper::Racer; |
|||
using helper::Executor; |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
std::function<void(std::function<void()> &&)> Executor::Entry() { |
|||
return [this](auto &&func) { |
|||
pool_.detach_task(func); |
|||
}; |
|||
} |
|||
|
|||
Executor::~Executor() { |
|||
pool_.wait(); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
|||
|
|||
void Racer::Execute(std::function<void()> &&item) { |
|||
auto wrapper = [item = std::move(item)](const int) { |
|||
item(); // execute racing function
|
|||
}; |
|||
pool_.detach_sequence(0, race_num_, wrapper); |
|||
pool_.wait(); |
|||
} |
|||
|
|||
int Racer::RaceNum() const { |
|||
return race_num_; // number of racing threads
|
|||
} |
|||
|
|||
Racer::~Racer() { |
|||
pool_.wait(); |
|||
} |
|||
|
|||
// ----------------------------------------------------------------------------------------- //
|
@ -1,65 +0,0 @@ |
|||
#pragma once |
|||
|
|||
/// The concurrency test helper class implements the Executor and Racer based
|
|||
/// on the BS::thread_pool. The Executor allows you to submit multiple tasks
|
|||
/// and dispatch them to different threads. The Racer schedules the specified
|
|||
/// function to a certain number of threads at one time.
|
|||
|
|||
#include <functional> |
|||
|
|||
#include "BS_thread_pool.hpp" |
|||
|
|||
namespace co { |
|||
|
|||
class Executor final { |
|||
public: |
|||
explicit Executor(const int num) : pool_(num) {} |
|||
|
|||
std::function<void(std::function<void()> &&)> Entry() { |
|||
return [this](auto &&func) { |
|||
pool_.detach_task(func); |
|||
}; |
|||
} |
|||
|
|||
~Executor() { |
|||
pool_.wait(); |
|||
} |
|||
|
|||
private: |
|||
BS::thread_pool pool_; |
|||
}; |
|||
|
|||
class Racer final { |
|||
public: |
|||
explicit Racer(const int num) : race_num_(num), pool_(num) {} |
|||
|
|||
int RaceNum() const { |
|||
return race_num_; // number of racing threads
|
|||
} |
|||
|
|||
void Race(std::function<void()> &&item) { |
|||
Start(std::move(item)); |
|||
Join(); |
|||
} |
|||
|
|||
void Start(std::function<void()> &&item) { |
|||
auto wrapper = [item = std::move(item)](const int) { |
|||
item(); // execute racing function
|
|||
}; |
|||
pool_.detach_sequence(0, race_num_, wrapper); |
|||
} |
|||
|
|||
void Join() { |
|||
pool_.wait(); |
|||
} |
|||
|
|||
~Racer() { |
|||
Join(); |
|||
} |
|||
|
|||
private: |
|||
const int race_num_; |
|||
BS::thread_pool pool_; |
|||
}; |
|||
|
|||
} // namespace co
|
Loading…
Reference in new issue