Hi all
i am trying to create a thread pool class using c++
i already create one thread pool using boost asio and boost thread
however i am trying by following approch and stuck at some point.
can u suggest any solution.
thanks in advance.
I thought the idea of a thread pool was to create a set of threads, up to some maximum number, and have them process tasks of a task queue.
You then push work items onto the queue and the they're processed by the threads in the set concurrently, when each thread completes its task, it fetches the next work item off the queue until the queue is empty.
Your code doesn't appear to be doing anything like that. How does your thread pool work in principle?
there is no predefined queue for task.
my steps are
1> runtime i will get task.
2>i have to keep threads ready in pool.(not to create one on time) asign a thread to do task.
3>return a thread to pool.
4>pool of fix size, if more request comes , sorry (some limits up to max no of concerent threads only may be something like SQL connction pool).
well i achive it using boost::asio & using io_stream::post functionality
but i want to create general class for same.
It sounds like we're in agreement on what needs to happen, but I don't see anything on the public interface of your thread pool class that supports that.
#include<iostream>
#include<boost\thread\thread.hpp>
#include <boost\bind.hpp>
#include <boost\asio.hpp>
usingnamespace std;
usingnamespace boost;
//interface which has to follow
class process
{
public:
virtualvoid doProcess()=0;
virtual ~process()
{
}
};
//specific implementation 1
class processType1:virtualpublic process
{
int x;
public:
processType1(int i)
{
x = i;
}
virtualvoid doProcess()
{
cout<<"some process "<<x<<"\n";
Sleep(1000);
}
virtual ~processType1()
{
}
};
std::shared_ptr<process> getObj(int i) //just for simplicity
{
return std::shared_ptr<process>(new processType1(i));
}
void threadfun(std::shared_ptr<process> obj) // thread function to bind
{
obj->doProcess();
}
int main()
{
asio::io_service io_service;
asio::io_service::work work(io_service);
thread_group threads; // thread pool of 10 threads
for (std::size_t i = 0; i < 10; ++i)
threads.create_thread(boost::bind(&asio::io_service::run, &io_service));
for(int i=0 ; i<100 ; ++i) //this is infinite loop i make it simple
{
std::shared_ptr<process> obj = getObj(i); //i will be getting object here
io_service.post(boost::bind(threadfun, obj));//assign task to one of thread in pool
}
io_service.stop();
threads.join_all();
return 0;
}
now my intention is to remove interface, just a standalone class which will give threads ready to assig any task at run time. the task/process class may go on changing.
The implementation should never make it's way into the user's code. Interface is everything. It should look easy to use. I would expect:
1. Some kind of Task object with some kind of pure virtual Execute method that you derive from to make up your work units.
2. Some kind of ThreadPool object that you initialise with the number of threads, and minimally, an AddTask method that takes a dynamically allocated Task*.
3. Some way to pickup completed Tasks.
The user code creates a ThreadPool initialised with the number of threads, then adds Task objects to the ThreadPool instance and picks up the results somewhere.
My approach would be to think about how the whole thing would hang together and write it down in UML, then build the first draft, and improve as necessary in subsequent iterations.
well my 2'nd code meets above requirements.
the only change that need is to is to wrap up some functionality in main in some class.
however my 1'st code will be still having problems
1> how i can assign some function to thread pointer ?
2> how i can return thread pointer after work done so i can add it back to stack?
i go through boost thread i don't get anything which can add function to existing thread.
and there is no fnctionality in thread pool class to post task to one of thread in pool.
so only way remain is to put wrapper over boost::asio and make it close to above requirement.
What's the point of locking a mutex (lock_guard, btw) in the constructor when the mutex is your class's own non-static member? It's impossible for another thread to try to lock the same mutex.
Also, why does creating another threadpool (nonzero Scount) result in a pool that pretends to have a bunch of things available, but doesn't actually own any threads?
Also, it's either boost::this_thread::sleep(), you you need to #include something that provides Sleep()
oh, and there's a pure syntax error: ~process(); cannot have a body inside the class definition, it has to be outside, as "process::~process() {};"
i think ,i am trying to create one object only , i dont want another object, as of now i don't put any error message.
Scount will be 0 only.
i don't need to include anything for Sleep , i use visual studio 2010 express.
~process(); cannot have a body inside the class definition ?
well above code works and don't show any compiler \ syntax error
can u specify any specific compiler which shows error for same.