text
stringlengths 0
2.2M
|
---|
mX->processJob(job);
|
}
|
}
|
} // close namespace TEST_CASE_6
|
// ============================================================================
|
// COMPONENT USAGE EXAMPLE
|
// ----------------------------------------------------------------------------
|
namespace TEST_CASE_USAGE_EXAMPLE {
|
class JobQueue {
|
// This class defines a generic processor for user-defined functions
|
// ("jobs"). Jobs specified to the 'processJob' method are executed in the
|
// thread pool specified at construction.
|
public:
|
// PUBLIC TYPES
|
typedef bdlmt::ThreadMultiplexor::Job Job;
|
// A callback of this type my be specified to the 'processJob' method.
|
private:
|
// DATA
|
bdlmt::FixedThreadPool *d_threadPool_p; // (held, not owned)
|
bdlmt::ThreadMultiplexor d_multiplexor; // used to partition threads
|
private:
|
// NOT IMPLEMENTED
|
JobQueue(const JobQueue&);
|
JobQueue& operator=(const JobQueue&);
|
public:
|
// CREATORS
|
JobQueue(int maxProcessors,
|
bdlmt::FixedThreadPool *threadPool,
|
bslma::Allocator *basicAllocator = 0);
|
// Create a job queue that executes jobs in the specified 'threadPool'
|
// using no more than the specified 'maxProcessors'. Optionally specify
|
// a 'basicAllocator' used to supply memory. If 'basicAllocator' is 0,
|
// the currently installed default allocator is used.
|
~JobQueue();
|
// Destroy this object.
|
// MANIPULATORS
|
int processJob(const Job& job);
|
// Process the specified 'job' in the thread pool specified at
|
// construction. Return 0 on success, and a non-zero value otherwise.
|
};
|
// CREATORS
|
JobQueue::JobQueue(int maxProcessors,
|
bdlmt::FixedThreadPool *threadPool,
|
bslma::Allocator *basicAllocator)
|
: d_threadPool_p(threadPool)
|
, d_multiplexor(maxProcessors, threadPool->queueCapacity(), basicAllocator)
|
{
|
}
|
JobQueue::~JobQueue()
|
{
|
}
|
// MANIPULATORS
|
int JobQueue::processJob(const JobQueue::Job& job)
|
{
|
return d_threadPool_p->tryEnqueueJob(bdlf::BindUtil::bind(
|
&bdlmt::ThreadMultiplexor::processJob<Job>,
|
&d_multiplexor,
|
job));
|
}
|
int usageExample(bslma::Allocator *allocator)
|
{
|
enum {
|
NUM_THREADS = 5, // total number of threads
|
NUM_QUEUES = 3, // total number of JobQueue objects
|
MAX_QUEUESIZE = 20 // total number of pending jobs
|
};
|
int maxProc = static_cast<int>(
|
bsl::max(1.0, ceil(double(NUM_THREADS) / (NUM_QUEUES - 1)) - 1));
|
bdlmt::FixedThreadPool tp(NUM_THREADS, MAX_QUEUESIZE, allocator);
|
JobQueue importantQueue(maxProc, &tp);
|
JobQueue urgentQueue(maxProc, &tp);
|
JobQueue criticalQueue(maxProc, &tp);
|
if (0 != tp.start()) {
|
ASSERT(!"Could not start thread pool! Threads cannot be created!");
|
return -1; // RETURN
|
}
|
bsls::AtomicInt iCheck, uCheck, cCheck;
|
JobQueue::Job ijob =
|
bdlf::BindUtil::bind(&bsls::AtomicInt::add, &iCheck, 1);
|
JobQueue::Job ujob =
|
bdlf::BindUtil::bind(&bsls::AtomicInt::add, &uCheck, 1);
|