2011-08-16 3 views
14

boost :: condition :: timed_wait를 가장 쉽게 사용하는 방법에 대한 예제가 있습니까? 주제가 here, herehere 인 스레드가 있지만 작동 예제가 없습니다. 그리고 부스트 의사는 보통 드문 드문 드문 예입니다.boost :: condition :: timed_wait 사용 예제

+0

이것은 부스트에서 잘 설명되어 있으며 조건 변수의 시간 초과는 다중 스레드 환경에서 매우 일반적입니다. 구체적으로 무엇을 찾고 있습니까? – Chad

+4

@Chad : 아마도 내가 의사를 놓친 것일까 요? 내가 찾은 것은 [여기] (http://www.boost.org/doc/libs/1_47_0/doc/html/thread/synchronization.html#thread.synchronization.condvar_ref)입니다. 그건 단지 시간이 아닌 샘플, 즉 'while (! data_ready) {cond.wait (lock); }'. timed_wait의 경우, _the duration_wait의 과부하는 올바르게 사용하기가 어렵습니다. 술어를 사용하는 오버로드가 대부분의 경우 선호되어야합니다. _ 나는 이해하지 못합니다.이 스 니펫도 : while (! pred()) {if (! timed_wait (lock, abs_time)) {return pred(); }} return true;'pred()는 무엇입니까? – Cookie

답변

19

사실 나는 전체 예제가 here 인 링크를 발견했습니다. 적응의 약간을 가지고, 이것은 전화로 보인다.

boost::system_time const timeout=boost::get_system_time()+ boost::posix_time::milliseconds(35000); 
boost::mutex::scoped_lock lock(the_mutex); 
if(the_condition_variable.timed_wait(lock,timeout,&CondFulfilled)) 
{ 
    <cond fulfilled code> 
} 
else 
{ 
    <timeout code> 
} 
bool CondFulfilled() { ... } 
관련 문제