patterncppMinor
Lockfree ThreadPool implementation
Viewed 0 times
implementationthreadpoollockfree
Problem
How could this template class
I use Boost queue to keep
It's not possible to store them directly in
That's why I emulate primitive lockfree container with
The
```
#include
#include
#include
#include
#include
#include
#include
#include
using std::cout ; using std::cin; using std::endl;
using std::mutex; using std::atomic;
using std::array; using std::function;
template using guardedArray = std::array,std::function>, NN> ;
template using lockfreequeue = boost::lockfree::queue>;
class Functor{
int myNN;
public:
Functor(int nn):myNN(nn){};
Functor(std::function ff) : myff(ff){};
std::function myff;
Functor(const Functor&) = default;
Functor operator=(const Functor&) ;
~Functor() = default;
void operator()(){
myff();
}
};
template class GuardedArray{
guardedArray myGuardedArray;
public:
int insertObject(std::function ff){
int kk ;
bool empty;
do{
kk = rand () % NN;
empty = myGuardedArray[kk].first;
cout getIndex(int ii){return myGuardedArray[ii].second ; };
std::thread garbageCollectorThread;
};
template class ThreadPool{
private:
std::thread tt[NN];
GuardedArray myGA;
bool endFlag;
public:
ThreadPool():endFlag(false){
for(int ii = 0 ; ii :: threadLoop, this);
tt[ii].swap(localThread);
}
};
void bookTask(std::function ff){
int ii = myGA.insertObject(ff);
mySync.push(ii);
};
bool demandedAccess[NN];
int waitingProcess;
~ThreadPool() {endFlag = true; for (int ii = 0 ; ii mySync;
};
template void ThreadPool::threadLoop(){
std::thread::id kk = std::this_threa
ThreadPool be improved?I use Boost queue to keep
std::function's numbers from std::array(simple hashing) from which I can later retrieve the std::function's objects to execute them with operator(). It's not possible to store them directly in
boost::lockfree::queue (Stored Object Type must have non-trivial destructor).That's why I emulate primitive lockfree container with
std::array<std::pair.The
GuarderArray template class lacks garbage collector. ```
#include
#include
#include
#include
#include
#include
#include
#include
using std::cout ; using std::cin; using std::endl;
using std::mutex; using std::atomic;
using std::array; using std::function;
template using guardedArray = std::array,std::function>, NN> ;
template using lockfreequeue = boost::lockfree::queue>;
class Functor{
int myNN;
public:
Functor(int nn):myNN(nn){};
Functor(std::function ff) : myff(ff){};
std::function myff;
Functor(const Functor&) = default;
Functor operator=(const Functor&) ;
~Functor() = default;
void operator()(){
myff();
}
};
template class GuardedArray{
guardedArray myGuardedArray;
public:
int insertObject(std::function ff){
int kk ;
bool empty;
do{
kk = rand () % NN;
empty = myGuardedArray[kk].first;
cout getIndex(int ii){return myGuardedArray[ii].second ; };
std::thread garbageCollectorThread;
};
template class ThreadPool{
private:
std::thread tt[NN];
GuardedArray myGA;
bool endFlag;
public:
ThreadPool():endFlag(false){
for(int ii = 0 ; ii :: threadLoop, this);
tt[ii].swap(localThread);
}
};
void bookTask(std::function ff){
int ii = myGA.insertObject(ff);
mySync.push(ii);
};
bool demandedAccess[NN];
int waitingProcess;
~ThreadPool() {endFlag = true; for (int ii = 0 ; ii mySync;
};
template void ThreadPool::threadLoop(){
std::thread::id kk = std::this_threa
Solution
Just some stylistic things that stick out to me:
-
I think it's a bit cumbersome to have all of this:
You're also still prefixing
-
Some of your indentation and semicolon placing are inconsistent. In some places you indent or not, and in some places you have a space before the ending semicolon. Keep the indentation consistent (you should utilize it) and choose what to do with all of your semicolons.
-
It would be more readable and maintainable to place the
-
I think it's a bit cumbersome to have all of this:
using std::cout ; using std::cin; using std::endl;
using std::mutex; using std::atomic;
using std::array; using std::function;You're also still prefixing
std:: in places, which defeats the purpose of having this. Since you could always end up using something not already in this list, you might as well keep prefixing std:: as you're doing already, and remove all of that above.-
Some of your indentation and semicolon placing are inconsistent. In some places you indent or not, and in some places you have a space before the ending semicolon. Keep the indentation consistent (you should utilize it) and choose what to do with all of your semicolons.
-
It would be more readable and maintainable to place the
template statements on separate lines from the class or function, especially if either statement is lengthy:template
class Class
{
};template
void function()
{
}Code Snippets
using std::cout ; using std::cin; using std::endl;
using std::mutex; using std::atomic;
using std::array; using std::function;template <typename T>
class Class
{
};template <typename T>
void function()
{
}Context
StackExchange Code Review Q#46672, answer score: 3
Revisions (0)
No revisions yet.