TemporaryRef<SharedThreadPool> SharedThreadPool::Get(const nsCString& aName, uint32_t aThreadLimit) { MOZ_ASSERT(NS_IsMainThread()); EnsureInitialized(); MOZ_ASSERT(sMonitor); ReentrantMonitorAutoEnter mon(*sMonitor); SharedThreadPool* pool = nullptr; nsresult rv; if (!sPools->Get(aName, &pool)) { nsCOMPtr<nsIThreadPool> threadPool(CreateThreadPool(aName)); NS_ENSURE_TRUE(threadPool, nullptr); pool = new SharedThreadPool(aName, threadPool); // Set the thread and idle limits. Note that we don't rely on the // EnsureThreadLimitIsAtLeast() call below, as the default thread limit // is 4, and if aThreadLimit is less than 4 we'll end up with a pool // with 4 threads rather than what we expected; so we'll have unexpected // behaviour. rv = pool->SetThreadLimit(aThreadLimit); NS_ENSURE_SUCCESS(rv, nullptr); rv = pool->SetIdleThreadLimit(aThreadLimit); NS_ENSURE_SUCCESS(rv, nullptr); sPools->Put(aName, pool); } else if (NS_FAILED(pool->EnsureThreadLimitIsAtLeast(aThreadLimit))) { NS_WARNING("Failed to set limits on thread pool"); } MOZ_ASSERT(pool); RefPtr<SharedThreadPool> instance(pool); return instance.forget(); }
int main() { char szTmp[] = "hello world"; MyTask taskObj; taskObj.setArg((void*)szTmp); zl::ThreadPool threadPool(10); for(int i = 0; i < 20; i++) { threadPool.addTask(&taskObj); } while(1) { printf("there are still %d tasks need to process\n", threadPool.size()); if (threadPool.size() == 0) { threadPool.stop(); printf("Now I will exit from main\n"); exit(0); } sleep(2); } return 0; }
int main(int, char **) { unsigned int const thread_count = CloudI::API::thread_count(); Output outputObject; ThreadPool<Input, ThreadData, Output, OutputData> threadPool(thread_count, thread_count, outputObject); uint32_t timeout_terminate = 0; for (unsigned int i = 0; i < thread_count; ++i) { Input inputObject(i); if (timeout_terminate == 0) { timeout_terminate = inputObject.timeout_terminate(); assert(timeout_terminate >= 1000); } bool const result = threadPool.input(inputObject); assert(result); } while (outputObject.got_output() == false) ::sleep(1); threadPool.exit(timeout_terminate - 100); return 0; }
int main() { // random number generator std::random_device rd; std::mt19937 urng(rd()); // Create a threadpool with 5 threads ::Uplinkzero::Common::ThreadPool<Uplinkzero::Common::ThreadData> threadPool(5); // Pointer to the function we want the threads in the pool to execute auto pFunctionForThreadToRun = std::function<void(Uplinkzero::Common::FunctionParams &)>(functionForThreadToRun); // enque tasks into the threadpool for (int i = 0; i < 1000; i++) { Uplinkzero::Common::FunctionParams params; params.i = urng(); Uplinkzero::Common::ThreadData td; td.setThreadFunction(pFunctionForThreadToRun); td.setThreadFunctionParams(params); threadPool.enqueue(td); } // sleep for 10 seconds std::this_thread::sleep_for(std::chrono::milliseconds(10000)); std::cout << "Main thread finished!" << std::endl; return 0; }
void testCleanupIdleThread() { if (verbose) { cout << "testCleanupIdleThread" << endl; } try { struct timeval deallocateWait = { 0, 1 }; ThreadPool threadPool(0, "test cleanup", 0, 6, deallocateWait); threadPool.allocate_and_awaken( (void*)1, funcSleepSpecifiedMilliseconds); Threads::sleep(1000); PEGASUS_TEST_ASSERT(threadPool.idleCount() == 1); threadPool.cleanupIdleThreads(); PEGASUS_TEST_ASSERT(threadPool.idleCount() == 0); } catch (const Exception& e) { cout << "Exception in testCleanupIdleThread: " << e.getMessage() << endl; PEGASUS_TEST_ASSERT(false); } }
int main(int argc, char* argv[]) { WSADATA Ws; if ( WSAStartup(MAKEWORD(2,2), &Ws) != 0 ) { cout<<"Init Windows Socket Failed::"<<GetLastError()<<endl; return -1; } CSocket mysocket; if ( false == mysocket.init() ) { cout<<"Create Socket Failed!"<<endl; return -1; } if ( false == mysocket.connect(AF_INET, IP_ADDRESS, PORT) ) { cout<<"Connect Failed!"<<endl; return -1; } else { cout<<"Connect Success!"<<endl; } //SOCKET clientSocket = mysocket.accept_connect(); ThreadPool threadPool(1); threadPool.Call(&DoJob); fd_set fdread; int ret; while(true) { // FD_ZERO(&fdread); // FD_SET(mysocket.get_sock(), &fdread); // if (SOCKET_ERROR == (ret = select(0, &fdread, NULL, NULL, NULL))) // { // continue; // } // // if (ret > 0) // { // if (FD_ISSET(mysocket.get_sock(), &fdread)) // { // // } // } } WSACleanup(); return 0; }
void Gather(const char* archivePathName, const char* outputPathName) { Framework::CStdStream outputStream(outputPathName, "wb"); CBasicBlock::SetAotBlockOutputStream(&outputStream); { Framework::CThreadPool threadPool(std::thread::hardware_concurrency()); filesystem::path archivePath = filesystem::path(archivePathName); auto archive = std::unique_ptr<CPsfArchive>(CPsfArchive::CreateFromPath(archivePath)); for(const auto& fileInfo : archive->GetFiles()) { filesystem::path archiveItemPath = fileInfo.name; filesystem::path archiveItemExtension = archiveItemPath.extension(); if(CPlaylist::IsLoadableExtension(archiveItemExtension.string().c_str() + 1)) { threadPool.Enqueue( [=] () { printf("Processing %s...\r\n", archiveItemPath.string().c_str()); CPsfVm virtualMachine; CPsfLoader::LoadPsf(virtualMachine, archiveItemPath.wstring(), archivePath); int currentTime = 0; virtualMachine.OnNewFrame.connect( [¤tTime] () { currentTime += 16; }); virtualMachine.Resume(); #ifdef _DEBUG static const unsigned int executionTime = 1; #else static const unsigned int executionTime = 10; #endif while(currentTime <= (executionTime * 60 * 1000)) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); } virtualMachine.Pause(); } ); } } } CBasicBlock::SetAotBlockOutputStream(nullptr); }
void testOverloadPool() { if (verbose) { cout << "testOverloadPool" << endl; } try { struct timeval deallocateWait = { 0, 1 }; ThreadPool threadPool(0, "test overload", 0, 4, deallocateWait); ThreadStatus threadStarted; threadStarted = threadPool.allocate_and_awaken( (void*)3000, funcSleepSpecifiedMilliseconds); PEGASUS_TEST_ASSERT(threadStarted == PEGASUS_THREAD_OK); threadStarted = threadPool.allocate_and_awaken( (void*)3000, funcSleepSpecifiedMilliseconds); PEGASUS_TEST_ASSERT(threadStarted == PEGASUS_THREAD_OK); threadStarted = threadPool.allocate_and_awaken( (void*)3000, funcSleepSpecifiedMilliseconds); PEGASUS_TEST_ASSERT(threadStarted == PEGASUS_THREAD_OK); threadStarted = threadPool.allocate_and_awaken( (void*)3000, funcSleepSpecifiedMilliseconds); PEGASUS_TEST_ASSERT(threadStarted == PEGASUS_THREAD_OK); threadStarted = threadPool.allocate_and_awaken( (void*)300, funcSleepSpecifiedMilliseconds); PEGASUS_TEST_ASSERT(threadStarted == PEGASUS_THREAD_INSUFFICIENT_RESOURCES); ThreadStatus rc = PEGASUS_THREAD_OK; while ( (rc =threadPool.allocate_and_awaken( (void*)100, funcSleepSpecifiedMilliseconds)) != PEGASUS_THREAD_OK) { if (rc ==PEGASUS_THREAD_INSUFFICIENT_RESOURCES) Threads::yield(); else throw Exception("Could not allocate and awaken a thread."); } } catch (const Exception& e) { cout << "Exception in testOverloadPool: " << e.getMessage() << endl; PEGASUS_TEST_ASSERT(false); } }
void Command::delegate(std::unique_ptr<Activity> activity) { assert(activity); connect(activity.get(), SIGNAL(finished()), this, SLOT(activityFinished()), Qt::QueuedConnection); ++activityCount_; #ifdef NC_USE_THREADS activity->setAutoDelete(true); threadPool()->start(activity.release()); #else activity->run(); #endif }
int main(int, char *[]) { FastOS_ThreadPool threadPool(512*1024); printf("Main stack(%p)\n", &threadPool); Thread context; FastOS_ThreadInterface * th[4]; for (size_t i=0; i<sizeof(th)/sizeof(th[0]); i++) { th[i] = threadPool.NewThread(&context); } for (size_t i=0; i<sizeof(th)/sizeof(th[0]); i++) { th[i]->Join(); delete th[i]; } return 0; }
std::vector<Node::Ptr> RegionsBuilder::BuildCountryRegionTrees(RegionsBuilder::Regions const & countries) { std::vector<std::future<Node::Ptr>> tmp; { base::thread_pool::computational::ThreadPool threadPool(m_threadsCount); for (auto const & country : countries) { auto result = threadPool.Submit(&RegionsBuilder::BuildCountryRegionTree, country, m_regions); tmp.emplace_back(std::move(result)); } } std::vector<Node::Ptr> res; res.reserve(tmp.size()); std::transform(std::begin(tmp), std::end(tmp), std::back_inserter(res), [](auto & f) { return f.get(); }); return res; }
already_AddRefed<SharedThreadPool> SharedThreadPool::Get(const nsCString& aName, uint32_t aThreadLimit) { MOZ_ASSERT(sMonitor && sPools); ReentrantMonitorAutoEnter mon(*sMonitor); SharedThreadPool* pool = nullptr; nsresult rv; if (auto entry = sPools->LookupForAdd(aName)) { pool = entry.Data(); if (NS_FAILED(pool->EnsureThreadLimitIsAtLeast(aThreadLimit))) { NS_WARNING("Failed to set limits on thread pool"); } } else { nsCOMPtr<nsIThreadPool> threadPool(CreateThreadPool(aName)); if (NS_WARN_IF(!threadPool)) { sPools->Remove(aName); // XXX entry.Remove() return nullptr; } pool = new SharedThreadPool(aName, threadPool); // Set the thread and idle limits. Note that we don't rely on the // EnsureThreadLimitIsAtLeast() call below, as the default thread limit // is 4, and if aThreadLimit is less than 4 we'll end up with a pool // with 4 threads rather than what we expected; so we'll have unexpected // behaviour. rv = pool->SetThreadLimit(aThreadLimit); if (NS_WARN_IF(NS_FAILED(rv))) { sPools->Remove(aName); // XXX entry.Remove() return nullptr; } rv = pool->SetIdleThreadLimit(aThreadLimit); if (NS_WARN_IF(NS_FAILED(rv))) { sPools->Remove(aName); // XXX entry.Remove() return nullptr; } entry.OrInsert([pool] () { return pool; }); } MOZ_ASSERT(pool); RefPtr<SharedThreadPool> instance(pool); return instance.forget(); }
int main(int, char **) { unsigned int const thread_count = CloudI::API::thread_count(); Output outputObject; ThreadPool<Input, ThreadData, Output, OutputData> threadPool(thread_count, thread_count, outputObject); for (unsigned int i = 0; i < thread_count; ++i) { Input inputObject(i); bool const result = threadPool.input(inputObject); assert(result); } while (outputObject.got_output() == false) ::sleep(1); threadPool.exit(3000); return 0; }
int main(int argc, char **argv) { if (argc < 2) { std::cout << "Usaga: ./PageCommonFansCompute ids.txt" << std::endl; exit(1); } /* MyUtil::IntSeq common; try { common = xce::buddy::adapter::PageCommonFansCacheAdapter::instance().GetCommon(299331400, 600098863, 0, -1); } catch (Ice::Exception& e) { LOG(LERROR) << e.what(); } catch (...) { LOG(LERROR) << "...exeption"; } LOG(INFO) << "common.size():" << common.size(); for (size_t i = 0; i < common.size(); ++i) { LOG(INFO) << "i:" << i << "\t" << common[i]; } return 0; */ std::vector<int> ids; ReadFileGetId(argv[1], ids); xce::ThreadPool threadPool(8, 20); std::vector<xce::buddy::CMessage*> cmess; for (size_t i = 0; i < ids.size(); ++i) { int id = ids[i]; xce::buddy::CMessage* msg = new xce::buddy::CMessage(id); cmess.push_back(msg); if (i % 1000 == 0) { LOG(INFO) << "total:" << ids.size() << "\tcount:" << i; } } threadPool.Post(cmess.begin(), cmess.end()); sleep(3); threadPool.Stop(); threadPool.Join(); return 0; }
/** * Store the simulated events in the given workspace. This clears the calculated * values * @param resultWS :: An output workspace that has all of its meta-data set up * and just needs to * be filled with events. */ void ResolutionConvolvedCrossSection::storeSimulatedEvents( const API::IMDEventWorkspace_sptr &resultWS) { auto outputWS = boost::dynamic_pointer_cast<MDEventWorkspace4>(resultWS); if (!outputWS) { throw std::invalid_argument( "ResolutionConvolvedCrossSection currently only supports 4 dimensions"); } auto iterEnd = m_simulatedEvents.end(); for (auto iter = m_simulatedEvents.begin(); iter != iterEnd; ++iter) { outputWS->addEvent(*iter); } m_simulatedEvents.clear(); // This splits up all the boxes according to split thresholds and sizes. auto threadScheduler = new ThreadSchedulerFIFO(); ThreadPool threadPool(threadScheduler); outputWS->splitAllIfNeeded(threadScheduler); threadPool.joinAll(); outputWS->refreshCache(); }
void testWorkException() { if (verbose) { cout << "testWorkException" << endl; } try { struct timeval deallocateWait = { 0, 1 }; ThreadPool threadPool(0, "test exception", 0, 6, deallocateWait); threadPool.allocate_and_awaken((void*)1, funcThrow); Threads::sleep(100); } catch (const Exception& e) { cout << "Exception in testWorkException: " << e.getMessage() << endl; PEGASUS_TEST_ASSERT(false); } }
void testDestructWithRunningThreads() { if (verbose) { cout << "testDestructWithRunningThreads" << endl; } try { struct timeval deallocateWait = { 0, 1 }; ThreadPool threadPool(0, "test destruct", 0, 0, deallocateWait); threadPool.allocate_and_awaken( (void*)100, funcSleepSpecifiedMilliseconds); threadPool.allocate_and_awaken( (void*)200, funcSleepSpecifiedMilliseconds); threadPool.allocate_and_awaken( (void*)300, funcSleepSpecifiedMilliseconds); threadPool.allocate_and_awaken( (void*)400, funcSleepSpecifiedMilliseconds); threadPool.allocate_and_awaken( (void*)500, funcSleepSpecifiedMilliseconds); threadPool.allocate_and_awaken( (void*)600, funcSleepSpecifiedMilliseconds); threadPool.allocate_and_awaken( (void*)700, funcSleepSpecifiedMilliseconds); threadPool.allocate_and_awaken( (void*)800, funcSleepSpecifiedMilliseconds); threadPool.allocate_and_awaken( (void*)900, funcSleepSpecifiedMilliseconds); PEGASUS_TEST_ASSERT(threadPool.runningCount() > 0); } catch (const Exception& e) { cout << "Exception in testDestructWithRunningThreads: " << e.getMessage() << endl; PEGASUS_TEST_ASSERT(false); } }
void Eval::doEval(int high, int low) { XenOption* opt = XenOption::getInstance(); StaticData* sD = StaticData::getInstance(); if (opt->getSVocab()->getFileName().compare("") == 0) { sD->getVocabs()->getPtrSourceVoc()->initialize(sD->getSourceCorps()->getPtrInCorp()); } else { sD->getVocabs()->getPtrSourceVoc()->initialize(opt->getSVocab()); } pool threadPool(opt->getThreads()); int pc = low; if (pc == 0) { pc = opt->getStep(); } std::vector<std::string> parts; while (pc <= high) { EvalMap::iterator found = ptrDist->find(pc); if (found == ptrDist->end()) { std::string partName = sD->getXenResult()->getXenFile()->getDirName() + "/" + sD->getXenResult()->getXenFile()->getPrefix() + "-" + XenCommon::toString(pc) + "pc.gz"; XenIO::writeXRpart(sD->getXenResult(), pc, partName); boost::shared_ptr<Corpus> c = boost::make_shared<Corpus>(); c->initialize(partName, "xx"); threadPool.schedule( boost::bind(taskEval, pc, c, sD->getVocabs()->getPtrSourceVoc(), sD->getDevCorp(), ptrDist)); parts.push_back(partName); } pc += opt->getStep(); } threadPool.wait(); for (int i = 0; i < parts.size(); i++) XenIO::delFile(parts[i]); std::cout << "Evaluation done." << std::endl; }
void FileSystemItem::load() { if (!m_rendererFactory) { return; } QImage *cachedCover = g_coverCache[coverCacheKey()]; if (cachedCover) { bool *isDefaultFolderCover = g_isDefaultFolderCover[coverCacheKey()]; markIsDefaultFolderCover( isDefaultFolderCover ? *isDefaultFolderCover : true); setThumbnail(new QImage(*cachedCover)); } else { LoadRunnable *r = new LoadRunnable(m_pathInfo); connect(r, SIGNAL(markIsDefaultFolderCover(bool)), this, SLOT(markIsDefaultFolderCover(bool))); connect(r, SIGNAL(gotThumbnail(QString)), this, SLOT(gotThumbnail(QString))); connect(r, SIGNAL(loadCover(QString)), this, SLOT(loadCover(QString))); threadPool()->start(r); } }
int main() { zl::ThreadPool threadPool(10); MyTask taskObj[20]; for(int i = 0; i < 20; i++) { threadPool.addTask(std::bind(&MyTask::run, &taskObj[i], i, "helloworld")); } while(1) { printf("there are still %d tasks need to process\n", threadPool.size()); if (threadPool.size() == 0) { threadPool.stop(); printf("Now I will exit from main\n"); exit(0); } sleep(2); } return 0; }
void go( int portNumber, const char* directory ) { printf("Creating objects ...\n"); SystemThreadApi threadApi; RawSocketApi socketApi; HttpSocketReader socketReader( socketApi ); HttpResponseSocketWriter socketWriter( socketApi ); SystemDirectoryApi directoryApi; SystemFileFactory fileFactory; SystemFileApi fileApi( fileFactory ); std::string basePath( directory ); HttpRequestParserImpl requestParser; HttpRequestHandlerFactoryImpl requestHandlerFactory( basePath, fileApi, directoryApi ); StlWorkItemQueue workItemQueue; unsigned numberOfThreads = 50; SystemSlaveThreadFactory slaveThreadFactory( threadApi ); ThreadPool threadPool( threadApi, slaveThreadFactory, workItemQueue, numberOfThreads ); ThreadedRequestHandler connectionhandler ( socketReader, requestParser, requestHandlerFactory, socketWriter, threadPool ); SocketConnectionReceiver receiver( socketApi, portNumber ); printf("Creating server...\n"); Server server( receiver, connectionhandler ); printf("Starting server (you won't see a Server Started message :) )...\n"); server.start(); }
void testBlockingThread() { if (verbose) { cout << "testBlockingThread" << endl; } try { struct timeval deallocateWait = { 5, 0 }; ThreadPool threadPool(0, "test blocking", 0, 6, deallocateWait); Semaphore blocking(0); ThreadStatus rt = PEGASUS_THREAD_OK; while ( (rt =threadPool.allocate_and_awaken( (void*)16, funcSleepSpecifiedMilliseconds, &blocking)) != PEGASUS_THREAD_OK) { if (rt == PEGASUS_THREAD_INSUFFICIENT_RESOURCES) { Threads::yield(); } else { throw Exception("Could not allocate thread for" " funcSleepSpecifiedMilliseconds function."); } } blocking.wait(); threadPool.cleanupIdleThreads(); } catch (const Exception& e) { cout << "Exception in testBlockingThread: " << e.getMessage() << endl; PEGASUS_TEST_ASSERT(false); } }
int main() { // Create thread pool printf("Creating thread pool\n"); int NUM_THREADS = 4; DataSource dataSource; ResultData result; //TODO change to smart pointers std::vector<WorkerThread*> threadPool(NUM_THREADS); for(std::vector<WorkerThread*>::iterator it = threadPool.begin(); it != threadPool.end(); ++it) { *it = new WorkerThread(); } unsigned t0 = clock(); for(std::vector<WorkerThread*>::iterator it = threadPool.begin(); it != threadPool.end(); ++it) { (*it)->Run(&dataSource, &result); } printf("Exiting the program\n"); for(std::vector<WorkerThread*>::iterator it = threadPool.begin(); it != threadPool.end(); ++it) { delete(*it); } unsigned t1 = clock() - t0; printf("Exec time(%d), Final result: %d\n", t1, result.GetResult()); }
int main(int argc, char** argv) { try { if (argc > 1 && argv[1] == std::string("-h")) { std::cerr << "Usage: server [WORK_THREAD_COUNT] [PORT]" << std::endl; return 1; } auto threadCount = (argc <= 1) ? yatest::ThreadPool::cpuCoreCount() : static_cast<size_t>(std::stoi(argv[1])); auto port = (argc <= 2) ? yatest::DEFAULT_PORT : static_cast<uint16_t>(std::stoi(argv[2])); yatest::ThreadPool threadPool(threadCount); yatest::Server::Processor processor = [&threadPool](const std::string& path) { auto wc = yatest::WordCounter::countDirectory(path, threadPool); auto top = wc.top(TOP_COUNT); return boost::algorithm::join(top, "\n"); }; yatest::Server server(processor, port); server.sync(); // wait for SIG_KILL or whatever } catch (const std::exception& error) { std::cerr << "Error: " << error.what() << std::endl; } catch (...) { std::cerr << "Something terrible has happend" << std::endl; } }
//------------------------------------------------------------------------------------- bool Dbmgr::initInterfacesHandler() { pInterfacesAccountHandler_ = InterfacesHandlerFactory::create(g_kbeSrvConfig.interfacesAccountType(), threadPool(), *static_cast<KBEngine::DBThreadPool*>(DBUtil::pThreadPool())); pInterfacesChargeHandler_ = InterfacesHandlerFactory::create(g_kbeSrvConfig.interfacesChargeType(), threadPool(), *static_cast<KBEngine::DBThreadPool*>(DBUtil::pThreadPool())); INFO_MSG(fmt::format("Dbmgr::initInterfacesHandler: interfaces addr({}), accountType:({}), chargeType:({}).\n", g_kbeSrvConfig.interfacesAddr().c_str(), g_kbeSrvConfig.interfacesAccountType(), g_kbeSrvConfig.interfacesChargeType())); if(strlen(g_kbeSrvConfig.interfacesThirdpartyAccountServiceAddr()) > 0) { INFO_MSG(fmt::format("Dbmgr::initInterfacesHandler: thirdpartyAccountService_addr({}:{}).\n", g_kbeSrvConfig.interfacesThirdpartyAccountServiceAddr(), g_kbeSrvConfig.interfacesThirdpartyAccountServicePort())); } if(strlen(g_kbeSrvConfig.interfacesThirdpartyChargeServiceAddr()) > 0) { INFO_MSG(fmt::format("Dbmgr::initInterfacesHandler: thirdpartyChargeService_addr({}:{}).\n", g_kbeSrvConfig.interfacesThirdpartyChargeServiceAddr(), g_kbeSrvConfig.interfacesThirdpartyChargeServicePort())); } return pInterfacesAccountHandler_->initialize() && pInterfacesChargeHandler_->initialize(); }
void DebugRenderer::render(const Scene &scene, const RenderSettings &settings) const { #ifdef DEBUG uint32_t numThreads = 1; #else uint32_t numThreads = std::thread::hardware_concurrency(); #endif XORShiftRNG topRand(settings.getInt(RenderSettingItem::RNGSeed)); std::unique_ptr<ArenaAllocator[]> mems = std::unique_ptr<ArenaAllocator[]>(new ArenaAllocator[numThreads]); std::unique_ptr<IndependentLightPathSampler[]> samplers = std::unique_ptr<IndependentLightPathSampler[]>(new IndependentLightPathSampler[numThreads]); for (int i = 0; i < numThreads; ++i) { new (mems.get() + i) ArenaAllocator(); new (samplers.get() + i) IndependentLightPathSampler(topRand.getUInt()); } std::unique_ptr<IndependentLightPathSampler*[]> samplerRefs = std::unique_ptr<IndependentLightPathSampler*[]>(new IndependentLightPathSampler*[numThreads]); for (int i = 0; i < numThreads; ++i) samplerRefs[i] = &samplers[i]; const Camera* camera = scene.getCamera(); ImageSensor* sensor = camera->getSensor(); Job job; job.renderer = this; job.scene = &scene; job.mems = mems.get(); job.pathSamplers = samplerRefs.get(); job.camera = camera; job.timeStart = settings.getFloat(RenderSettingItem::TimeStart); job.timeEnd = settings.getFloat(RenderSettingItem::TimeEnd); job.sensor = sensor; job.imageWidth = settings.getInt(RenderSettingItem::ImageWidth); job.imageHeight = settings.getInt(RenderSettingItem::ImageHeight); job.numPixelX = sensor->tileWidth(); job.numPixelY = sensor->tileHeight(); sensor->init(job.imageWidth, job.imageHeight); DefaultAllocator &defMem = DefaultAllocator::instance(); std::array<std::unique_ptr<Image2D, Allocator::DeleterType>, (int)ExtraChannel::NumChannels> chImages; for (int i = 0; i < m_channels.size(); ++i) { if (!m_channels[i]) { chImages[i] = nullptr; continue; } switch ((ExtraChannel)i) { case ExtraChannel::GeometricNormal: chImages[i] = defMem.createUnique<TiledImage2D>(job.imageWidth, job.imageHeight, ColorFormat::RGB8x3, &defMem); break; case ExtraChannel::ShadingNormal: chImages[i] = defMem.createUnique<TiledImage2D>(job.imageWidth, job.imageHeight, ColorFormat::RGB8x3, &defMem); break; case ExtraChannel::ShadingTangent: chImages[i] = defMem.createUnique<TiledImage2D>(job.imageWidth, job.imageHeight, ColorFormat::RGB8x3, &defMem); break; case ExtraChannel::Distance: chImages[i] = defMem.createUnique<TiledImage2D>(job.imageWidth, job.imageHeight, ColorFormat::Gray8, &defMem); break; default: break; } } job.chImages = &chImages; ThreadPool threadPool(numThreads); for (int ty = 0; ty < sensor->numTileY(); ++ty) { for (int tx = 0; tx < sensor->numTileX(); ++tx) { job.basePixelX = tx * sensor->tileWidth(); job.basePixelY = ty * sensor->tileHeight(); threadPool.enqueue(std::bind(&Job::kernel, job, std::placeholders::_1)); } } threadPool.wait(); // char filename[256]; // sprintf(filename, "output.bmp"); // sensor->saveImage(filename, 1); for (int i = 0; i < chImages.size(); ++i) { if (!chImages[i]) continue; std::string filename; switch ((ExtraChannel)i) { case ExtraChannel::GeometricNormal: filename = "geometric_normal.bmp"; break; case ExtraChannel::ShadingNormal: filename = "shading_normal.bmp"; break; case ExtraChannel::ShadingTangent: filename = "shading_tangent.bmp"; break; case ExtraChannel::Distance: filename = "distance.bmp"; break; default: break; } chImages[i]->saveImage(filename, false); } }
/** * Create an output event workspace filled with data simulated with the fitting * function. * @param baseName :: The base name for the workspace * @param inputWorkspace :: The input workspace. * @param values :: The calculated values * @param outputWorkspacePropertyName :: The property name */ boost::shared_ptr<API::Workspace> FitMD::createEventOutputWorkspace( const std::string &baseName, const API::IMDEventWorkspace &inputWorkspace, const API::FunctionValues &values, const std::string &outputWorkspacePropertyName) { auto outputWS = MDEventFactory::CreateMDWorkspace(inputWorkspace.getNumDims(), "MDEvent"); // Add events // TODO: Generalize to ND (the current framework is a bit limiting) auto mdWS = boost::dynamic_pointer_cast< DataObjects::MDEventWorkspace<DataObjects::MDEvent<4>, 4>>(outputWS); if (!mdWS) { return boost::shared_ptr<API::Workspace>(); } // Bins extents and meta data for (size_t i = 0; i < 4; ++i) { boost::shared_ptr<const Geometry::IMDDimension> inputDim = inputWorkspace.getDimension(i); Geometry::MDHistoDimensionBuilder builder; builder.setName(inputDim->getName()); builder.setId(inputDim->getDimensionId()); builder.setUnits(inputDim->getUnits()); builder.setNumBins(inputDim->getNBins()); builder.setMin(inputDim->getMinimum()); builder.setMax(inputDim->getMaximum()); builder.setFrameName(inputDim->getMDFrame().name()); outputWS->addDimension(builder.create()); } // Run information outputWS->copyExperimentInfos(inputWorkspace); // Coordinates outputWS->setCoordinateSystem(inputWorkspace.getSpecialCoordinateSystem()); // Set sensible defaults for splitting behaviour BoxController_sptr bc = outputWS->getBoxController(); bc->setSplitInto(3); bc->setSplitThreshold(3000); outputWS->initialize(); outputWS->splitBox(); auto inputIter = inputWorkspace.createIterator(); size_t resultValueIndex(0); const float errorSq = 0.0; do { const size_t numEvents = inputIter->getNumEvents(); const float signal = static_cast<float>(values.getCalculated(resultValueIndex)); for (size_t i = 0; i < numEvents; ++i) { coord_t centers[4] = { inputIter->getInnerPosition(i, 0), inputIter->getInnerPosition(i, 1), inputIter->getInnerPosition(i, 2), inputIter->getInnerPosition(i, 3)}; mdWS->addEvent(MDEvent<4>(signal, errorSq, inputIter->getInnerRunIndex(i), inputIter->getInnerDetectorID(i), centers)); } ++resultValueIndex; } while (inputIter->next()); delete inputIter; // This splits up all the boxes according to split thresholds and sizes. auto threadScheduler = new Kernel::ThreadSchedulerFIFO(); Kernel::ThreadPool threadPool(threadScheduler); outputWS->splitAllIfNeeded(threadScheduler); threadPool.joinAll(); outputWS->refreshCache(); // Store it if (!outputWorkspacePropertyName.empty()) { declareProperty( new API::WorkspaceProperty<API::IMDEventWorkspace>( outputWorkspacePropertyName, "", Direction::Output), "Name of the output Workspace holding resulting simulated spectrum"); m_manager->setPropertyValue(outputWorkspacePropertyName, baseName + "Workspace"); m_manager->setProperty(outputWorkspacePropertyName, outputWS); } return outputWS; }
// Initialize the context menu handler. IFACEMETHODIMP FileContextMenuExt::Initialize( LPCITEMIDLIST pidlFolder, LPDATAOBJECT pDataObj, HKEY hKeyProgID) { if (NULL == pDataObj) { return E_INVALIDARG; } HRESULT hr = E_FAIL; FORMATETC fe = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL }; STGMEDIUM stm; // The pDataObj pointer contains the objects being acted upon. In this // example, we get an HDROP handle for enumerating the selected files and // folders. if (SUCCEEDED(pDataObj->GetData(&fe, &stm))) { // Get an HDROP handle. HDROP hDrop = static_cast<HDROP>(GlobalLock(stm.hGlobal)); if (hDrop != NULL) { //! start of Haponov CHANGES: //!**************************************************** //! Haponov: number of threads depends on number of selected files UINT nFiles = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0); if (nFiles) { wchar_t temp_forName[MAX_PATH]; if (nFiles > 1) { //std::vector<std::thread> threads; ThreadPool threadPool(std::thread::hardware_concurrency()); for (UINT i = 0; i < nFiles - 1; ++i) { // Get full path of the file. if (0 != DragQueryFile(hDrop, i, temp_forName /*such path is written to temp_forName*/, ARRAYSIZE(temp_forName))) { std::wstring ws(temp_forName); filePaths.push_back(ws); //threads.push_back(std::thread(&FileContextMenuExt:: // processSelectedFiles, this, filePaths[i])); threadPool.doJob(std::bind(&FileContextMenuExt::processSelectedFiles, this, filePaths[i])); } } //! Haponov: use the main thread to do part of the work - //! process the last file if (0 != DragQueryFile(hDrop, nFiles - 1, temp_forName, ARRAYSIZE(temp_forName))) { std::wstring ws(temp_forName); processSelectedFiles(ws); } //!Haponov: no need for joining here anymore //for (auto &t : threads) //{ // t.join(); //} } //! Haponov: use only main thread if a sinle file is selected else { if (0 != DragQueryFile(hDrop, 0, temp_forName, ARRAYSIZE(temp_forName))) { std::wstring ws(temp_forName); processSelectedFiles(ws); } } } if (sortedFiles.size()) hr = S_OK; //! end of Haponov changes //!**************************************************** GlobalUnlock(stm.hGlobal); } ReleaseStgMedium(&stm); } // If any value other than S_OK is returned from the method, the context // menu item is not displayed. return hr; }
//------------------------------------------------------------------------------------- bool Dbmgr::initBillingHandler() { pBillingAccountHandler_ = BillingHandlerFactory::create(g_kbeSrvConfig.billingSystemAccountType(), threadPool(), dbThreadPool_); pBillingChargeHandler_ = BillingHandlerFactory::create(g_kbeSrvConfig.billingSystemChargeType(), threadPool(), dbThreadPool_); INFO_MSG(boost::format("Dbmgr::initBillingHandler: billing addr(%1%), accountType:(%2%), chargeType:(%3%).\n") % g_kbeSrvConfig.billingSystemAddr().c_str() % g_kbeSrvConfig.billingSystemAccountType() % g_kbeSrvConfig.billingSystemChargeType()); if(strlen(g_kbeSrvConfig.billingSystemThirdpartyAccountServiceAddr()) > 0) { INFO_MSG(boost::format("Dbmgr::initBillingHandler: thirdpartyAccountService_addr(%1%:%2%).\n") % g_kbeSrvConfig.billingSystemThirdpartyAccountServiceAddr() % g_kbeSrvConfig.billingSystemThirdpartyAccountServicePort()); } if(strlen(g_kbeSrvConfig.billingSystemThirdpartyChargeServiceAddr()) > 0) { INFO_MSG(boost::format("Dbmgr::initBillingHandler: thirdpartyChargeService_addr(%1%:%2%).\n") % g_kbeSrvConfig.billingSystemThirdpartyChargeServiceAddr() % g_kbeSrvConfig.billingSystemThirdpartyChargeServicePort()); } return pBillingAccountHandler_->initialize() && pBillingChargeHandler_->initialize(); }
// Entry point for commandlet // Sample commandline: // <app-name> threadpooltest <uint:num threads> <uint:stack size kb> PkInt Main( const PkInt argc, char** argv ) { PkAssert( ECmdArgs_MaxArgs == argc ); // Parse commandline arguments const PkUInt numThreads = atoi( argv[ ECmdArgs_NumThreads ] ); const PkUInt stackSizeKb = atoi( argv[ ECmdArgs_StackSizeKb ] ); // Convert stack size to bytes const PkUInt stackSizeBytes = stackSizeKb * 1024; // Initialize thread pool PkThreadPool threadPool( numThreads, stackSizeBytes ); // Initialize jobs to run on the pool BusyLoopRunnable jobs[] = { BusyLoopRunnable( 5 /* jobDurationSeconds */ ) , BusyLoopRunnable( 3 /* jobDurationSeconds */ ) , BusyLoopRunnable( 4 /* jobDurationSeconds */ ) , BusyLoopRunnable( 2 /* jobDurationSeconds */ ) , BusyLoopRunnable( 1 /* jobDurationSeconds */ ) , BusyLoopRunnable( 5 /* jobDurationSeconds */ ) , BusyLoopRunnable( 7 /* jobDurationSeconds */ ) , BusyLoopRunnable( 2 /* jobDurationSeconds */ ) , BusyLoopRunnable( 3 /* jobDurationSeconds */ ) , BusyLoopRunnable( 1 /* jobDurationSeconds */ ) , BusyLoopRunnable( 6 /* jobDurationSeconds */ ) , BusyLoopRunnable( 2 /* jobDurationSeconds */ ) , BusyLoopRunnable( 3 /* jobDurationSeconds */ ) , BusyLoopRunnable( 4 /* jobDurationSeconds */ ) , BusyLoopRunnable( 20 /* jobDurationSeconds */ ) , BusyLoopRunnable( 30 /* jobDurationSeconds */ ) }; // Determine number of jobs const PkInt numJobs = sizeof(jobs)/sizeof(BusyLoopRunnable); // Queue the jobs for ( PkInt i=0; i<numJobs; ++i ) { threadPool.queueJob( jobs[i] ); } // Wait for jobs to finish const time_t startTime = PkClock::getSeconds(); while ( BusyLoopRunnable::getJobCompletionCounter() < numJobs ) { // Man pages for unix indicate that sleep equivalents // sleep the entire process? There is pthread_yield // but that does not take a time parameter. Therefore, // only using Sleep for Windows at the moment #if WINDOWS Sleep( 1000 /* dwMilliseconds */ ); #endif // Destroy thread pool while jobs are running if ( BusyLoopRunnable::getJobCompletionCounter() >= (PkInt)(numJobs * 0.75f) ) { #if WINDOWS Sleep(1); #endif PkLogf( "Destroying thread pool while running jobs\n" ); threadPool.destroy(); break; } } const time_t elapsedTime = PkClock::getSeconds() - startTime; PkLogf( "Finished all jobs in %d seconds", (PkInt)elapsedTime ); return 0; }