本文整理汇总了C++中ThreadPool类的典型用法代码示例。如果您正苦于以下问题:C++ ThreadPool类的具体用法?C++ ThreadPool怎么用?C++ ThreadPool使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了ThreadPool类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: upto
void Scene::computeColorsAntialiasing(std::function<void(int, int, ColorCRef)> paint, int threadCount, int level) {
upto(threadCount, 1);
downto(threadCount, 4);
std::vector< std::vector <ColorFloat> > m(screen.width, std::vector<ColorFloat> (screen.height, ColorFloat()));
computeColors([paint, &m](int x, int y, ColorCRef c) { m[x][y] = c; paint(x, y, c); }, threadCount);
if (level > 1) {
for (int x = 1; x + 1 < screen.width; x++) {
for (int y = 1; y + 1 < screen.height; y++) {
int badness = 0;
for (int dx : {-1, 1})
for (int dy : {-1, 1})
badness += (m[x][y] - m[x + dx][y + dy]).norm();
if (badness > badnessLimit) {
paint(x, y, Color(255, 0, 0));
}
}
}
int taskCount = 10;
ThreadPool pool;
for (int t = 0; t < taskCount; t++)
pool.addTask([this, &paint, &m, level, t, taskCount]()
{ multiThreadAntialiasePart(paint, m, level, t, taskCount); });
pool.executeAll(threadCount);
}
}
示例2: debug
void Server::serve(int port, int maxClients, int numThreads) {
// set up the server socket
debug("Server::serve -- initializing listening socket");
int serverId = this->initSocket(port);
// set up the client manager
debug("Server::serve -- setting up client manager");
ClientManager* mgr = new ClientManager(maxClients);
// set up the thread pool
debug("Server::serve -- setting up thread pool");
ThreadPool* pool = new ThreadPool(numThreads);
pool->run(&run, mgr);
// set up client
int clientId;
struct sockaddr_in client_addr;
socklen_t clientlen = sizeof(client_addr);
// accept clients
debug("Server::serve -- waiting for client");
while ((clientId = accept(serverId, (struct sockaddr *)&client_addr, &clientlen)) > 0) {
ClientProxy* client = new ClientProxy(clientId);
debug("Server::serve -- adding client to queue");
mgr->pushClient(client);
}
// close the server socket
debug("Server::serve -- closing server socket");
close(serverId);
// clean up some memory (even though this will likely not ever be reached)
delete mgr;
delete pool;
}
示例3: TEST
//-----------------------------------------------------------------------------
TEST(ThreadPool, Sequential_WithMPSCBoundedQueue) {
// add N jobs to the queue, and wait until all will be completed
// this test should be faster than Sequential_WithStdQueue
ThreadPool pool;
MPMCBoundedQueue<std::future<int>> queue(128);
const int repetitions = 100;
for (int i = 1; i <= repetitions; i++) {
queue.push(std::move(pool.process([i]() {
std::this_thread::sleep_for(std::chrono::milliseconds(20));
return i;
})));
}
int sum_actual = 0;
std::future<int> item;
while (queue.pop(item)) {
sum_actual += item.get();
}
auto sum = [](int n) -> int { return n * (n + 1) / 2; };
ASSERT_EQ(sum_actual, sum(repetitions));
}
示例4: main
int main(int argc, char **argv){
bool ret = true;
long size = sysconf(_SC_NPROCESSORS_ONLN);
cout<<"number of workers: "<<size<<endl;
ThreadPool p;
ret = p.Init(size, start_routine, arg);
if (!ret){
return -1;
}
ret = p.Start();
if (!ret){
p.Shutdown();
return -1;
}
int n = 3;
while(n-->0){
sleep(2);
}
p.Shutdown();
return 0;
}
示例5: poll
void* ThreadPool::poll(void *arg) {
ThreadPool* ths = (ThreadPool*)arg;
while (true) {
if (!ths->prioritypooling) {
if (ths->wpool->tasksPending()) {
Task *task = ths->wpool->getTask();
PoolThread *idleThread = ths->getIdleThread();
idleThread->task = task;
idleThread->idle = false;
cout << "got task" << endl;
idleThread->mthread->interrupt();
} else {
Thread::mSleep(1);
}
} else {
if (ths->wpool->tasksPPending()) {
Task *task = ths->wpool->getPTask();
PoolThread *idleThread = ths->getIdleThread();
idleThread->task = task;
idleThread->idle = false;
idleThread->mthread->interrupt();
} else {
Thread::mSleep(1);
}
}
}
return NULL;
}
示例6: xp
bool ApService::InitApService(std::string ApListenIp, int ApListenPort, int threadPoolNum) {
bool status = true;
_ApListenIp = ApListenIp;
_ApListenPort = ApListenPort;
_threadPoolNum = threadPoolNum;
SharedPointer<IProcessor> xp(
new apapiProcessor(
SharedPointer<apapi>(
new ApService)));
ServiceManager sm;
sm.add_service(xp);
ThreadPool tp;
tp.init(_threadPoolNum);
Server* server = new Server(&sm, &tp, _ApListenPort);
if (0 != server->serve()) {
status = false;
}
return status;
}
示例7: testExecPerSecond
void testExecPerSecond(int numberOfThreads, int numberOfKeys)
{
ThreadPool<Task> pool;
pool.init(numberOfThreads, numberOfKeys, 10000);
for (long long int i = 0; ; ++i) {
Task t(i % numberOfKeys);
pool.enqueue(t);
if (i % 100000 == 0) {
int execCounterCur = execCounter.load();
int millisCur = getMilliseconds();
if (execCounterPrev != 0) {
int counterDif = execCounterCur - execCounterPrev;
long long int millisDif = millisCur - millisPrev;
long long int execPerSec = (counterDif / millisDif) * 1000;
cout << "exec per sec: " << execPerSec << "" << endl;
}
execCounterPrev = execCounterCur;
millisPrev = millisCur;
}
}
}
示例8: testThreadPool
int testThreadPool()
{
#ifdef __LINUX__
cout << "this is linux" << endl;
#endif
#ifdef __WINDOWS__
cout << "this is windows" << endl;
#endif
ThreadPool pool;
pool.Run(8);
pool.AddTask(new MyTask());
MySignal si;
for (int i = 0; i < 8; ++i)
//while (running)
{
pool.AddTask(new MyTask());
cout << "add task ...." << endl;
//Timer::Sleep(500);
}
int sec = 0;
while (running)
{
cout << "wait sec " << sec++ << endl;
Timer::Sleep(1000);
}
return 0;
}
示例9: CoInitialize
DWORD WINAPI ThreadPool::WorkerThread(LPVOID lpParam)
{
CoInitialize(NULL);
ThreadPool *obj = (ThreadPool *)lpParam;
CARBONLOG_CLASS_PTR logger(carbonLogger::getLoggerPtr());
CARBONLOG_INFO(logger, "[WorkerThread] : starting worker thread now..");
while(obj->shouldContinue)
{
std::string jobMsg, jobOutput;
if(obj->getJobMsg(jobMsg))
{
CARBONLOG_DEBUG(logger, "[WorkerThread] : Processing job ..");
processJob(jobMsg,jobOutput);
if(!obj->writeJobOutput(jobOutput))
CARBONLOG_ERROR(logger, "[WorkerThread] : Failed to write packet to BI");
}
else
Sleep(10);
}
CARBONLOG_INFO(logger, "[WorkerThread] : exiting worker thread now..");
CoUninitialize();
return 0;
}
示例10: calculateDirectIxn
void CpuNonbondedForce::calculateDirectIxn(int numberOfAtoms, float* posq, const vector<RealVec>& atomCoordinates, const vector<pair<float, float> >& atomParameters,
const vector<set<int> >& exclusions, vector<AlignedArray<float> >& threadForce, double* totalEnergy, ThreadPool& threads) {
// Record the parameters for the threads.
this->numberOfAtoms = numberOfAtoms;
this->posq = posq;
this->atomCoordinates = &atomCoordinates[0];
this->atomParameters = &atomParameters[0];
this->exclusions = &exclusions[0];
this->threadForce = &threadForce;
includeEnergy = (totalEnergy != NULL);
threadEnergy.resize(threads.getNumThreads());
gmx_atomic_t counter;
gmx_atomic_set(&counter, 0);
this->atomicCounter = &counter;
// Signal the threads to start running and wait for them to finish.
ComputeDirectTask task(*this);
threads.execute(task);
threads.waitForThreads();
// Combine the energies from all the threads.
if (totalEnergy != NULL) {
double directEnergy = 0;
int numThreads = threads.getNumThreads();
for (int i = 0; i < numThreads; i++)
directEnergy += threadEnergy[i];
*totalEnergy += directEnergy;
}
}
示例11: execute
static void NO_INLINE execute(const Source & data, size_t num_threads, std::vector<std::unique_ptr<Map>> & results,
Creator && creator, Updater && updater,
ThreadPool & pool)
{
results.reserve(num_threads);
for (size_t i = 0; i < num_threads; ++i)
results.emplace_back(new Map);
for (size_t i = 0; i < num_threads; ++i)
{
auto begin = data.begin() + (data.size() * i) / num_threads;
auto end = data.begin() + (data.size() * (i + 1)) / num_threads;
auto & map = *results[i];
pool.schedule([&, begin, end]()
{
for (auto it = begin; it != end; ++it)
{
typename Map::iterator place;
bool inserted;
map.emplace(*it, place, inserted);
if (inserted)
creator(place->second);
else
updater(place->second);
}
});
}
pool.wait();
}
示例12: main
int main(int argc, char **args) {
int ret = log_init("./conf", "simple_log.conf");
if (ret != 0) {
printf("log init error!");
return 0;
}
if (argc < 2) {
LOG_ERROR("usage: ./http_multi_thread_demo [port]");
return -1;
}
pthread_key_create(&g_tp_key,NULL);
ThreadPool tp;
tp.set_thread_start_cb(test_start_fn);
tp.set_thread_exit_cb(test_exit_fn);
tp.set_pool_size(4);
HttpServer http_server;
http_server.set_thread_pool(&tp);
http_server.add_mapping("/hello", hello);
http_server.add_bind_ip("127.0.0.1");
http_server.set_port(atoi(args[1]));
http_server.set_backlog(100000);
http_server.set_max_events(100000);
http_server.start_async();
//sleep(1);
http_server.join();
return 0;
}
示例13: DoObjectLockTest
void DoObjectLockTest( void )
{
cout << "Starting DoObjectLockTest" << endl;
LockableObjects & objects = GetLockableObjects();
objects.reserve( ObjectCount );
for ( unsigned int ii = 0; ii < ObjectCount; ++ii )
{
LockableObject * object = new LockableObject( ii );
objects.push_back( object );
}
{
ThreadPool pool;
pool.Create( ThreadCount, &RunObjectTest );
pool.Start();
pool.Join();
}
unsigned int totalFails = 0;
for ( unsigned int ii = 0; ii < ThreadCount; ++ii )
{
const unsigned int failCount = FailCounts[ ii ];
::Loki::Printf( "Thread: [%u] Failures: [%u]\n" )( ii )( failCount );
totalFails += failCount;
}
const char * result = ( 0 == totalFails ) ? "Passed" : "FAILED";
cout << "Finished DoObjectLockTest. Total Fails: " << totalFails << " Result: "
<< result << endl;
}
示例14: ThreadPool
unsigned RainbowTable::findPasswordByHash(unsigned* hash) {
int i;
Chain resultChain;
vector<vector<string> > result;
ThreadPool* myPool = new ThreadPool(THREADS_COUNT);
myPool->initializeThreads();
for (i = CHAIN_LENGTH - 1; i >= 0; --i) {
PasswordFindWorkerThread* myThread =
new PasswordFindWorkerThread(i, hash);
myPool->assignWork(myThread);
if (PasswordFindWorkerThread::result != 0) {
break;
}
}
myPool->destroyPool(1);
delete myPool;
printf("false alarm count: %d\n",
PasswordFindWorkerThread::falseAlarmCount);
if (PasswordFindWorkerThread::result != 0) {
return PasswordFindWorkerThread::result;
}
return 0;
}
示例15: main
int main (int argc, const char * argv[])
{
//Create a new thread pool
ThreadPool pool;
//Get the number of threads, this defaults to the number of threads on your machine
const unsigned int numThreads = pool.getThreadPoolSize();
//Set the number of tasks that we want to pass to the thread pool
const unsigned int numTasks = 100;
//Setup a vector to store the results
std::vector< double > results( numTasks );
std::vector< std::future< void > > status( numTasks );
//Add some tasks to the pool
for(unsigned int i=0; i<numTasks; i++){
status[i] = pool.enqueue( task, 99999, results[i] );
}
//Wait for the tasks to complex
for(unsigned int i=0; i<numTasks; i++){
status[i].wait();
}
//Get the maximum value across all the task results
double maxValue = results[0];
for(unsigned int i=1; i<numTasks; i++){
if( results[i] > maxValue ) maxValue = results[i];
}
infoLog << "maximum value: " << maxValue << std::endl;
return EXIT_SUCCESS;
}