그래서 사용자 정의 스레드에서 C++로 동기 이벤트 대기열을 만들고 싶습니다. 늘어나는만큼, boost :: asio :: strand는 이것에 대한 탁월한 후보이다. asio :: run()이 호출되면, 가닥의 큐에 이벤트가있는 동안에 만 실행된다. 코드 :boost :: asio 및 strands를 사용한 이벤트 대기열 : 새 이벤트 대기
this->control_strand_.reset(new boost::asio::strand(control_io_service_));
control_thread_ = boost::thread(boost::bind(&boost::asio::io_service::run,&control_io_service_));
control_thread_.join();
즉시 반환. 이제 나는 Boost Asio - How to know when the handler queue is empty?의 대답으로 갈 수 있지만, 여기에는 while-loop-wait가 있습니다. 차라리 더 많은 이벤트를 기반으로 할 수 있습니다 (일명 대기열이 비어있는 동안 잠시 동안 "포장"통화를 기다립니다). 일이 유일한 방법 나는 그것을 할 때마다 "포장"신호를 트리거 가진 완전히 가닥 클래스를 래핑되어이 일을 생각할 수 이것은 잠시없이 영원히 큐를 실행
//some member variables
boost::condition_variable cond_var;
boost::mutex mut;
std::unique_ptr<boost::asio::strand> control_strand_;
boost::asio::io_service control_io_service_
//while loop,running on event processing thread
void MessageProcessor()
{
while (true)
{
{
boost::unique_lock<boost::mutex> lock(mut);
cond_var.wait(lock);
}
control_io_service_.run();
}
}
//post call,from different thread
template <typename Handler>
void wrap(Handler hand)
{
cond_var.notify_all();
control_strand_->wrap(hand);
}
(뭔가 같은 의사 코드)라고 루프 (내 동기화 조금 떨어져,하지만 문제가 atm). 더 나은, 표준, 방법 있는가?
'io_service :: work' 객체와 함께'io_service'를 사용할 수없는 이유는 무엇입니까? –
@IgorR. 이봐, 내가 부스트에서 뭔가를하려고 할 때마다 내가 부스트 라이브러리가 완전히 해낸 문제에 봉착한다. 내 인터넷 검색 기술은 그것을 놓칠 것 같다. 이것은 작동하는 것처럼 보입니다. (이중 체크 만하면, 이것은 "암시적인 가닥"입니다. 즉, 큐는 동 기적으로 비어 있습니다.) 대답에 넣으면 나는 그것을 표시 할 것입니다. – IdeaHat