I’m trying to port a C# thread pool into C++ but have encountered some serious problems. Some of the features of the C# thread pool are:
- Define the maximum number of concurrent threads explicitly
- Each thread is defined using a AutoResetEvent
- Each workitem in the thread are overloaded so that it can have delegate functions as its private members.
For example,
private static void RunOrBlock(WorkItem workitem) {
workItem.ThreadIndex = WaitHandle.WaitAny(threadUnoccupied);
ThreadPool.QueueUserWorkItem(threadWorker, workItem);
}
private static void threadWorker(object o) {
WorkItem workItem = (workItem) o;
workItem.Run();
threadUnoccupied[workItem.ThreadIndex].Set();
}
WorkItem is defined as:
public abstract class WorkItem {
protected int threadIndex;
public abstract void Run();
public int ThreadIndex {
get { return threadIndex; }
set { threadIndex = value; }
}
Does someone know if there exists a open-source threading pool that has similar functionalities? If not, what will be the correct way to implement such a threading pool? Thanks!
I’m not certain about their specific functionalities, but for open source threadpools in c++ look at boost threadpool or zthreads.
If you just need a threadpool like functionality and have a compiler which supports it, you could also just use
openmp 3.0tasks. This is what I would choose if possible, since"boost" threadpooldidn’t look very convincing at a glance (so might have quite a bit of overhead) and it seems likezthreadsisn’t actively developed anymore (at least at first glance, I’m not 100% sure).In the not exactly foss, but might be usable if you can live with the licensing (or are ready to invest quite a bit of money…)
Intel Threading Building Blocksis pretty much treadpool based