/* When GPIEvent is invoked, start the predefined ROSpec for the event, only one time. * Because the invocation of ROSpec is based on the ROSpecID, * we can substitute it to other ROSpec if it is needed. */ void ELFIN::ROAdmin::onGPIEvent(LLRP::llrp_u16_t GPIPortNum, enum LLRP::EGPIPortState GPIEvent) { READER_LOG(LOGTYPE_INFO, "ROAdmin received GPIEvent (GPIPortNum %d, Event %d)\n", GPIPortNum, GPIEvent); boost::mutex::scoped_lock lock(_pGPIRegLock); for (GPIEventRegistry::iterator iter = _GPIEventRegistry.begin(); iter != _GPIEventRegistry.end();iter++) { if ((*iter).get<0>() == GPIPortNum && (*iter).get<1>() == GPIEvent) { int ROSpecID = (*iter).get<2>(); int trigType = (*iter).get<3>(); const char *trigTypeStr = (trigType == ELFIN::GPI_StartTrigger) ? "StartTrigger" : "StopTrigger"; READER_LOG(LOGTYPE_INFO, "GPIEventListener: (GPIPortNum %d, Event %d, TriggerType %s) invoked ROSpec %d.\n", GPIPortNum, GPIEvent, trigTypeStr, ROSpecID); if (trigType == ELFIN::GPI_StartTrigger) { boost::thread pThread(boost::bind(&ELFIN::ROAdmin::startROSpec, _LLRPCore->m_ROAdmin, ROSpecID)); //_LLRPCore->_ROAdmin->startROSpec(ROSpecID); } else if (trigType == ELFIN::GPI_StopTrigger) { boost::thread pThread(boost::bind(&ELFIN::ROAdmin::stopROSpec, _LLRPCore->m_ROAdmin, ROSpecID)); //_LLRPCore->_ROAdmin->stopROSpec(ROSpecID); } else { // not reached throw "error"; } } } }
DWORD WINAPI WaitingThread(LPVOID lParam) { std::auto_ptr<WaitingThreadData> pThread((WaitingThreadData *)lParam); WaitingThreadData &thread = *pThread; std::vector<HANDLE> handles; while (true) { thread.waitingToWait = true; ::WaitForSingleObject(thread.startWaiting, INFINITE); thread.waitingToWait = false; if (thread.terminate) break; if (!thread.count) continue; handles.resize(thread.count + 1); handles[0] = thread.stopWaiting; std::copy(thread.waitHandles, thread.waitHandles+thread.count, handles.begin()+1); DWORD result = ::WaitForMultipleObjects(handles.size(), &handles[0], FALSE, INFINITE); if (result == WAIT_OBJECT_0) continue; // another thread finished waiting first, so do nothing SetEvent(thread.stopWaiting); if (!(result > WAIT_OBJECT_0 && result < WAIT_OBJECT_0 + handles.size())) { assert(!"error in WaitingThread"); // break here so we can see which thread has an error *thread.error = ::GetLastError(); } } return S_OK; // return a value here to avoid compiler warning }
FILE * SessionImpl::OpenBZip2File (/*[in]*/ const PathName & path) { AutoBZ2 autoBz2 (BZ2_bzopen(path.Get(), "rb")); if (autoBz2.Get() == 0) { FATAL_MIKTEX_ERROR ("OpenBZip2File", T_("BZ2_bzopen() failed for some."), path.Get()); } FILE * pFiles[2]; Pipe (pFiles, PIPE_SIZE); try { auto_ptr<BZip2ReaderThreadArg> pArg (new BZip2ReaderThreadArg); pArg->bzin = autoBz2.Get(); pArg->fileout = pFiles[1]; auto_ptr<Thread> pThread (Thread::Start(BZip2ReaderThread, pArg.get())); pArg.release (); autoBz2.Detach (); return (pFiles[0]); } catch (const exception &) { if (pFiles[0] != 0) { fclose (pFiles[0]); } if (pFiles[1] != 0) { fclose (pFiles[1]); } throw; } }
void generate_key_pair_mt(T& n, T& d, uint32_t e, uint32_t modulusbits) { for (;;) { T p, q; std::thread pThread(cry::generate_probably_prime<T>, std::ref(p), modulusbits / 2, e); std::thread qThread(cry::generate_probably_prime<T>, std::ref(q), modulusbits / 2, e); pThread.join(); qThread.join(); T N = p * q; T Phi = (p - 1) * (q - 1); T D; const bool f = cry::mod_inverse(D, T(e), Phi); if (f) { n = N; d = D; break; } } }
adv_thread_ptr CAdvThreadPool::createSystemPoolThread() { if(systemPoolThread != nullptr) return nullptr; adv_thread_ptr pThread(new CAdvThread(0, poolSettings.coreQuantity)); systemPoolThread = pThread; systemPoolThread->setThreadType(CAdvThread::eThreadType::THREAD_NOT_SHARED); return systemPoolThread; }
adv_thread_ptr CAdvThreadPool::createAdditionalNotSharedThread(int& id) { std::unique_lock<std::mutex> locker(additionalThreadsMutex); int extraThreadsQuantity = 1000 + additionalThreadsArray.size(); adv_thread_ptr pThread(new CAdvThread(extraThreadsQuantity+1, poolSettings.coreQuantity)); pThread->setThreadType(CAdvThread::eThreadType::THREAD_NOT_SHARED_EXTRA); additionalThreadsArray.push_back(pThread); QString who = pThread->getWho(); id = pThread->getThreadNumber(); emitter->sendSignal_NewThread(id, pThread->getThreadType()); return pThread; }
int main(int argc, char* argv[]) { StringQueue queue; boost::asio::io_service io_service; boost::asio::io_service::work work(io_service); boost::shared_ptr<Consumer> consumer = boost::make_shared<Consumer>(boost::ref(io_service), boost::ref(queue)); consumer->consumeStrings(); boost::thread pThread(boost::bind(&produceStrings, boost::ref(queue))); boost::thread cThread(boost::bind(&boost::asio::io_service::run, boost::ref(io_service))); pThread.join(); cThread.join(); return 0; }
Error run(std::size_t threadPoolSize = 1) { try { // update state running_ = true; // get ready for next connection acceptNextConnection(); // initialize scheduled command timer waitForScheduledCommandTimer(); // block all signals for the creation of the thread pool // (prevents signals from occurring on any of the handler threads) core::system::SignalBlocker signalBlocker; Error error = signalBlocker.blockAll(); if (error) return error ; // create the threads for (std::size_t i=0; i < threadPoolSize; ++i) { // run the thread boost::shared_ptr<boost::thread> pThread(new boost::thread( &AsyncServer<ProtocolType>::runServiceThread, this)); // add to list of threads threads_.push_back(pThread); } } catch(const boost::thread_resource_error& e) { return Error(boost::thread_error::ec_from_exception(e), ERROR_LOCATION); } return Success(); }
bool CAdvThreadPool::createThreadPool(int unsharedThreadsQuantity, int sharedThreadsQuantity) { try { if(!poolSettings.readArchive() && unsharedThreadsQuantity != -1) { poolSettings.setQuantityUnsharedThreads(unsharedThreadsQuantity); poolSettings.setQuantitySharedThreads(sharedThreadsQuantity); } else if(!poolSettings.isValid()&& unsharedThreadsQuantity != -1) { poolSettings.setQuantityUnsharedThreads(unsharedThreadsQuantity); poolSettings.setQuantitySharedThreads(sharedThreadsQuantity); } if(poolSettings.masks.empty()) { poolSettings.masks.resize(poolSettings.getQuantityUnsharedThreads() + poolSettings.getQuantitySharedThreads()); for(int i=0; i<poolSettings.getQuantityUnsharedThreads() + poolSettings.getQuantitySharedThreads(); i++) { poolSettings.masks[i] = INT_MAX; } } int i; for (i = 0; i<poolSettings.getQuantityUnsharedThreads(); i++) { auto affinityMode = poolSettings.getAffinityMode(); adv_thread_ptr pThread(new CAdvThread(i+1, poolSettings.coreQuantity)); pThread->setThreadType(CAdvThread::eThreadType::THREAD_NOT_SHARED); if(affinityMode == eAffinityMode::Yes_Affinity || affinityMode == eAffinityMode::Yes_Affinity_Without_GUI_Edition) pThread->setCoreMask(poolSettings.masks[i]); threadsArray.push_back(pThread); } for (; i<poolSettings.getQuantityUnsharedThreads()+poolSettings.getQuantitySharedThreads(); i++) { auto affinityMode = poolSettings.getAffinityMode(); adv_thread_ptr pThread(new CAdvThread(i+1, poolSettings.coreQuantity)); pThread->setThreadType(CAdvThread::eThreadType::THREAD_SHARED); if(affinityMode == eAffinityMode::Yes_Affinity || affinityMode == eAffinityMode::Yes_Affinity_Without_GUI_Edition) pThread->setCoreMask(poolSettings.masks[i]); threadsArray.push_back(pThread); } poolSettings.isPoolStarted = true; if(poolSettings.poolModes.threadPoolMode == eThreadPoolMode::REPEATED_TASK_MODE) { systemTymerRunnable = new CLongTask<CAdvThreadPool, int>(this, &CAdvThreadPool::startPoolMainFunction, &CAdvThreadPool::stopSystemTymerRunnable, QString("System pool thread")); CAdvThreadPool::launchRunnableObject<int, CLongTask<CAdvThreadPool, int>>(systemTymerRunnable, true); } } catch(std::exception& e) { poolSettings.isPoolStarted = false; std::cout<<e.what(); } return poolSettings.isPoolStarted; }