c++


How to use thread pool where each thread has its own members


for simplicity, I have an object (class) which I want to run as thread.
The object has it's own members.
I want to use thread pool to create at 4 threads and run those 4 threads with different parameters each time.
The class (thread class) looks:
class CLogicThread {
public:
CLogicThread();
void RunLogic();
void SetParams(int num);
virtual ~CLogicThread();
private:
int m_Number = 0;
};
void CLogicThread::RunLogic() {
m_Number = m_Number + rand() % 2;
int miliToSleep = rand() % 2000 + 100;
usleep(miliToSleep);
std::cout << "Running Thread Id : " << std::this_thread::get_id() << " Value: " << m_Number;
}
void CLogicThread::SetParams(int num) {
m_Number = num;
}
Whenever there is a thread which has finished it's job earlier, it will get a new job to perform (without creating new thread, i.e use one of the vacant threads).
Something like:
Pool pool(4); // I dont have this class and looks for it
int counter = 0;
while (true) {
usleep(10);
counter++;
// the next code line will be blocked till new thread is avaliable
CLogicThread* pLogicThread = pool->GetThreadElement();
pLogicThread->SetParams(counter);
pLogicThread->RunLogic();
}
I have looked here:
Thread pooling in C++11
And here:
CTPL
but didn't found Pool with my need.
Is there ant open source of Pool I can use ? or do I need to implement it ?

Related Links

tried to use thread on my server
C++ vector [] operator throws error while at() works
Clion multiple debug configurations in cmake
C++ String object wrong characters [duplicate]
Syntex of Constructor Defining
Storing a child-class pointer in a map of parent-class pointer
In a composition relationship, can a member call a parent method?
Is it use an initialize a iterator of vector with a pointer in C++ in an init function?
How Do I Make My Program in Qt Continually Send A String to My Arduino?
C++ exception int catch block executes when string exception was thrown
why does it go wrong after push_back?
How can I stop input repeating for each char inputted?
Automated build can't find boost headers, sometimes
c++/Lua - Load multiple lua file names into a c++ string[automatically]
Is there a way to request a certain location of a text file to be read in C++?
Missing vc_runtimeminimum_x86.msi and installation won't work

Categories

HOME
perl
centos
asynchronous
unix
migration
soap
keyboard-shortcuts
filesystems
windows-forms-designer
unity5
datagrid
formula
spf
rectangles
apostrophe-cms
geometry
executable-jar
xamarin.mac
firebreath
pentaho-spoon
pad
netbeans-7.3
pyside
btrfs
google-surveys
spring-4
vim-syntax-highlighting
radio
mpd
slamdata
webdrivermanager-java
winrt-xaml
card-flip
event-listeners
media-type
o365rwsclient
matlab-guide
c#-to-vb.net
data-transfer
rollbar
jquery-tags-input
express-stormpath
suppress-warnings
ch
smf
ubuntu-15.10
arithmetic-expressions
snap-lang
mediafire
blackberry-simulator
frontpage
rebase
backspace
javascript-globalize
pdl
utilities
cpanm
c1flexgrid
cakedc
xcode-organizer
irssi
linux-capabilities
unassigned-variable
django-1.7
lcm
javascript-databinding
tbb
casbah
runscope
restkit-0.20
buttonclick
countries
problem-steps-recorder
boost-spirit-karma
struts2-tiles-plugin
generic-list
ora-01426
glossary
in-app
soaplite
cocos2d-python
xmlnodelist
tomahawk
ant4eclipse
clean-language
duck-typing
installshield-2008
incremental-linking
platform-independence

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile