void Run(const Reciever &reciever) { boost::mutex mutex; boost::unique_lock<boost::mutex> lock(mutex); progress = 0.5f; started.notify_all(); end.wait(lock); progress = 1; ended.notify_all(); }
/** * Try mark the file for deletion. Only few file states permit this operation to happen. * * @return true if file was marked for deletion * No one should reference this file since it is marked for deletion */ inline bool mark_for_deletion(){ boost::mutex::scoped_lock lock(m_state_changed_mux); bool marked = false; LOG (INFO) << "Managed file OTO \"" << fqp() << "\" with state \"" << state() << "\" is requested for deletion." << "subscribers # = " << m_subscribers.load(std::memory_order_acquire) << "\n"; // check all states that allow to mark the file for deletion: State expected = State::FILE_IS_IDLE; // look for "idle marker" marked = m_state.compare_exchange_strong(expected, State::FILE_IS_MARKED_FOR_DELETION); if(marked){ // wait for all detaching clients to finish: boost::unique_lock<boost::mutex> detaching_lock(m_detaching_mux_guard); m_detaching_condition.wait(detaching_lock, [&]{ return (m_detaching_clients.size() == 0); }); detaching_lock.unlock(); // go ahead, no detaching clients are in progress more m_state_changed_condition.notify_all(); LOG (INFO) << "Managed file OTO \"" << fqp() << "\" with state \"" << state() << "\" is successfully marked for deletion." << "\n"; if(m_subscribers.load(std::memory_order_acquire) == 0) return true; return false; } expected = State::FILE_IS_FORBIDDEN; marked = m_state.compare_exchange_strong(expected, State::FILE_IS_MARKED_FOR_DELETION); if(marked){ m_state_changed_condition.notify_all(); LOG (INFO) << "Managed file OTO \"" << fqp() << "\" with state \"" << state() << "\" is successfully marked for deletion." << "\n"; if(m_subscribers.load(std::memory_order_acquire) == 0) return true; return false; } expected = State::FILE_IS_AMORPHOUS; marked = m_state.compare_exchange_strong(expected, State::FILE_IS_MARKED_FOR_DELETION); m_state_changed_condition.notify_all(); marked = (marked && (m_subscribers.load(std::memory_order_acquire) == 0)); std::string marked_str = marked ? "successfully" : "NOT"; LOG (INFO) << "Managed file OTO \"" << fqp() << "\" with state \"" << state() << "\" is " << marked_str << " marked for deletion." << "\n"; return marked; }
/** * Reschedules this timer. * * @param next The time when this timer should be called again. Use -1 to let * the timer figure out a suitable time based on the interval. */ void Timer::Reschedule(double next) { ASSERT(!OwnsLock()); boost::mutex::scoped_lock lock(l_Mutex); if (next < 0) { /* Don't schedule the next call if this is not a periodic timer. */ if (m_Interval <= 0) return; next = Utility::GetTime() + m_Interval; } m_Next = next; if (m_Started) { /* Remove and re-add the timer to update the index. */ l_Timers.erase(GetSelf()); l_Timers.insert(GetSelf()); /* Notify the worker that we've rescheduled a timer. */ l_CV.notify_all(); } }
void ConsoleCommand::AutocompleteScriptCompletionHandler(boost::mutex& mutex, boost::condition_variable& cv, bool& ready, const boost::exception_ptr& eptr, const Array::Ptr& result, Array::Ptr& resultOut) { if (eptr) { try { boost::rethrow_exception(eptr); } catch (const std::exception& ex) { Log(LogCritical, "ConsoleCommand") << "HTTP query failed: " << ex.what(); #ifdef HAVE_EDITLINE /* Ensures that the terminal state is resetted */ rl_deprep_terminal(); #endif /* HAVE_EDITLINE */ Application::Exit(EXIT_FAILURE); } } resultOut = result; { boost::mutex::scoped_lock lock(mutex); ready = true; cv.notify_all(); } }
/** * Reschedules this timer. * * @param completed Whether the timer has just completed its callback. * @param next The time when this timer should be called again. Use -1 to let * the timer figure out a suitable time based on the interval. */ void Timer::InternalReschedule(bool completed, double next) { ASSERT(!OwnsLock()); boost::mutex::scoped_lock lock(l_TimerMutex); if (completed) m_Running = false; if (next < 0) { /* Don't schedule the next call if this is not a periodic timer. */ if (m_Interval <= 0) return; next = Utility::GetTime() + m_Interval; } m_Next = next; if (m_Started && !m_Running) { /* Remove and re-add the timer to update the index. */ l_Timers.erase(this); l_Timers.insert(this); /* Notify the worker that we've rescheduled a timer. */ l_TimerCV.notify_all(); } }
void start() { runningState_mutex->lock(); *runningState = RUNNING; runningState_mutex->unlock(); cv->notify_all(); }
void pause() { runningState_mutex->lock(); *runningState = PAUSED; runningState_mutex->unlock(); cv->notify_all(); }
int main(int argc, char **argv) { ros::init(argc, argv, "recognizer"); ros::NodeHandle n("~"); n.param("configuration", JULIUS_CONFIGURATION, string("")); n.param("debug", JULIUS_DEBUG, (bool)FALSE); if (JULIUS_CONFIGURATION.empty()) { ROS_ERROR("No Julius configuration file provided"); return -1; } boost::thread julius_thread(recognize); commands = n.advertise<std_msgs::String>(std::string("output"), 1000); ros::ServiceServer start_service = n.advertiseService<std_srvs::Empty::Request, std_srvs::Empty::Response>(ros::this_node::getName() + std::string("/start"), start_service_callback); ros::ServiceServer stop_service = n.advertiseService<std_srvs::Empty::Request, std_srvs::Empty::Response>(ros::this_node::getName() + std::string("/stop"), stop_service_callback); n.createTimer(ros::Duration(5), watchdog); ros::spin(); if (recog) j_request_terminate(recog); pause_variable.notify_all(); //julius_thread.join(); return 0; }
void MANAGER :: cleanup (void) { LogString saved_context; LOGGER_PUSH_NDCTAG (LOGGER_TAG_MANAGER); LOG4CXX_INFO (_logger, "Cleaning up by joining all the workers."); if (_nWorkers <= 0) { LOG4CXX_INFO (_logger, "There are no workers in the pool. Hence no cleanup is required. Returning..."); LOGGER_POP_NDCTAG; return; } /* notify all workers about whole_job_completed */ { boost::lock_guard<boost::mutex> lock(mut); command_from_manager = 1; whole_job_completed = 1; free_workers_mutex.lock(); LOG4CXX_DEBUG (_logger, "sending whole_job_completed notification to all. Currently free_workers = " << free_workers); free_workers_mutex.unlock(); cond.notify_all (); } free_workers_mutex.lock(); LOG4CXX_INFO (_logger, "joining all. Currently free_workers = " << free_workers); free_workers_mutex.unlock(); join_all (); free_workers_mutex.lock(); LOG4CXX_INFO (_logger, "joined all. Currently free_workers = " << free_workers); free_workers_mutex.unlock(); LOGGER_POP_NDCTAG; }
/* threat functions*/ void datagen_thread_loop(void) { ostringstream testline; /* create testdata*/ int jj=0; for(int ii=0; ii < 8*8-1; ii++) //64 = 63 chars + end line { if(++jj > 9) jj = 0; testline << jj; } testline << "\n"; jj=0; while(record_thread_running && jj < NUM_LINES) { #if !defined(WITH_BOOST_TIME) clock_gettime(CLOCK_MONOTONIC, &ts_beg); // http://linux.die.net/man/3/clock_gettime #else start_time = boost::posix_time::microsec_clock::local_time(); #endif // ********************************* // *active_buffer += testline.str().c_str(); if(4*1024 < active_buffer->length()) //write 4 kbyte blocks { record_trigger.notify_all(); } // ********************************* // #if !defined(WITH_BOOST_TIME) clock_gettime(CLOCK_MONOTONIC, &ts_end); v_fTime_s.push_back(ts_end.tv_sec); v_fTime_us.push_back(ts_end.tv_nsec/1e+3); v_fDifftime.push_back((ts_end.tv_sec - ts_beg.tv_sec) + (ts_end.tv_nsec - ts_beg.tv_nsec) / 1e9); f_DT_s = (ts_end.tv_sec - ts_beg.tv_sec) + (ts_end.tv_nsec - ts_beg.tv_nsec) / 1e9; #else stop_time = boost::posix_time::microsec_clock::local_time(); time_duration = (stop_time - start_time); v_fTime_s.push_back( (stop_time-boost::posix_time::from_time_t(0)).total_seconds() ); v_fTime_us.push_back( (stop_time-boost::posix_time::from_time_t(0)).fractional_seconds() ); v_fDifftime.push_back( time_duration.total_seconds()+ time_duration.fractional_seconds()/ 1e6); f_DT_s = (time_duration.total_seconds()+ time_duration.fractional_seconds()/ 1e6); #endif #if defined(DEBUG) if(0.5 < 1.e+3*f_DT_s) // log only values above 0.5 ms { cout << "Line " << jj << " of " << NUM_LINES << ":" << "\t" << v_fTime_s.back() << "." << v_fTime_us.back() << "s: " << "\tdT: " << fixed << 1.e+3*f_DT_s << " ms" << endl; } #endif boost::this_thread::sleep(boost::posix_time::microseconds(4*1e+6*f_DT_s)); //about 50% CPU load jj++; }//while datagen_done = true; }
void step() { if(getRunningState() != RUNNING) { boost::lock_guard<boost::mutex> lock2(*esc64_mutex); esc64->step(); } cv->notify_all(); }
// for message queue void push(T data) { boost::mutex::scoped_lock lockHandle(mutexHandle); queueHandle.push(data); lockHandle.unlock(); condFlag.notify_all(); }
void MessageHandler(CoinNodeSocket* pNodeSocket, const CoinNodeMessage& message) { if (string(message.getCommand()) == "version") { cout << message.toIndentedString() << endl; boost::unique_lock<boost::mutex> lock(mutex); bGotVersion = true; gotVersion.notify_all(); } }
// for data queue void wait_and_push(T data) { boost::mutex::scoped_lock lockHandle(mutexHandle); condFlag.wait(lockHandle, isFull(&queueHandle, sizeMax)); queueHandle.push(data); lockHandle.unlock(); condFlag.notify_all(); }
void stop() { io_service_.stop(); { boost::lock_guard<boost::mutex> lock(mutex_); terminated_ = true; } condition_.notify_all(); }
void flush() { // If notification for pushed task was sent between lines // `while (tasks_.empty()) {` and `cond_.wait(lock);`, then // there is a small chance that the notification was lost. // // Forcing all the blocked threads to wake up. boost::lock_guard<boost::mutex> lock(tasks_mutex_); cond_.notify_all(); }
/** * Disables the timer sub-system. */ void Timer::Uninitialize(void) { { boost::mutex::scoped_lock lock(l_Mutex); l_StopThread = true; l_CV.notify_all(); } l_Thread.join(); }
ros::CallbackInterface::CallResult call() { if (--g_count == 0) { boost::mutex::scoped_lock lock(g_mutex); g_cond.notify_all(); } return Success; }
void thr::thr_quit() { boost::mutex::scoped_lock scope(mut); quitting = true; // Wake all threads. workers_waiting.notifyAll(&workers_active); workers_sleeping.notifyAll(&workers_active); // Wake all waiters, their wishes will not be fullfilled. cond_wait_normal.notify_all(); cond_wait_privileged.notify_all(); // Clear all tasks, they will not be done. tasks_normal.clear(); tasks_important.clear(); tasks_privileged.clear(); }
static void startup_notify( boost::system::error_code & result2, boost::mutex & mutex, boost::condition_variable & cond, boost::system::error_code const & result) { boost::mutex::scoped_lock lock(mutex); result2 = result; cond.notify_all(); }
void setNotify(bool state) { m_NotifyLock.lock(); m_bNotify = state; if (state) m_WaitCond.notify_all(); m_NotifyLock.unlock(); }
/** \brief Wake up anyone waiting for normal tasks to complete. * * Must be called from a locked context. * * Do not call this function if normal task queue is not empty. * * If the caller is a worker thread, it must be in the active worker storage. * * \param tid Thread id. */ static bool wake_normal() { if(1 != workers_active.size()) { return false; } // We were the only worker active. workers_waiting.notifyAll(&workers_active); cond_wait_normal.notify_all(); return true; }
bool start_service_callback(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res) { boost::lock_guard<boost::mutex> lock(request_mutex); if (recog) j_request_resume(recog); pause_variable.notify_all(); return true; }
/** * Disables the timer sub-system. */ void Timer::Uninitialize(void) { { boost::mutex::scoped_lock lock(l_TimerMutex); l_StopTimerThread = true; l_TimerCV.notify_all(); } if (l_TimerThread.joinable()) l_TimerThread.join(); }
void ServiceImpl::setState(SimControlState state) { { boost::lock_guard<boost::mutex> lock(simStateMutex); prevSimState = simState; simState = state; } simStateCond.notify_all(); //TODO: print state changes when usefull to know }
/** * Unregisters the timer and stops processing events for it. */ void Timer::Stop(void) { ASSERT(!OwnsLock()); boost::mutex::scoped_lock lock(l_Mutex); m_Started = false; l_Timers.erase(GetSelf()); /* Notify the worker thread that we've disabled a timer. */ l_CV.notify_all(); }
BOOL WINAPI HandlerRoutine(__in DWORD dwCtrlType) { switch(dwCtrlType) { case CTRL_CLOSE_EVENT: case CTRL_SHUTDOWN_EVENT: shutdown_event = application_state::shutdown; shutdown_cond.notify_all(); return true; } return false; }
/** setter for file state * @param state - file state to mark the file with */ inline void state(State state) { // do not change file state when it is marked for deletion: if(m_state.load(std::memory_order_acquire) == State::FILE_IS_MARKED_FOR_DELETION) return; if(state == State::FILE_IS_IN_USE_BY_SYNC) m_lastsyncattempt = boost::posix_time::microsec_clock::local_time(); // fire the condition variable for whoever waits for file status to be changed: m_state.exchange(state, std::memory_order_release); boost::mutex::scoped_lock lock(m_state_changed_mux); m_state_changed_condition.notify_all(); }
/// @brief Destructor. ~ThreadPool() { // Set running flag to false then notify all threads. { boost::unique_lock< boost::mutex > lock(mutex_); running_ = false; condition_.notify_all(); } try { threads_.join_all(); } // Suppress all exceptions. catch (const std::exception&) {} }
~ThreadPool() { threadsafe_print("Entering thread pool destructor\n"); { lock_guard tasksLock(m_tasksMutex); m_threadExit = true; } m_tasksChangedCondition.notify_all(); threadsafe_print("Threads should now exit when all tasks are done.\n"); for(int i = 0; i < (int)m_threads.size(); ++i) { m_threads[i]->join(); threadsafe_print("Thread exit.\n"); } }