2011-04-20 5 views
18
thread_ = boost::thread(boost::function< void (void)>(boost::bind(&clientTCP::run , this))); 

는 실행이 같은 인수가 가능 있다는 것입니다 :boost :: thread에 인수를 전달하는 방법은?

void clientTCP::run(boost:function<void(std::string)> func); 

을하고 만약 그렇다면 내 부스트 : 스레드 호출이

감사를 작성하는 방법.

답변

27

다음 코드 boost::bind(&clientTCP::run , this)은 함수 콜백을 정의합니다. 현재 인스턴스 (this)에서 함수 run을 호출합니다. 부스트는 당신이 할 수있는 결합 :: 다음과 같은 : 당신은 기능 향상 : 스레드의 인스턴스 를 구성하려면
http://www.boost.org/doc/libs/1_46_1/doc/html/thread/thread_management.html

:

// Pass pMyParameter through to the run() function 
boost::bind(&clientTCP::run, this, pMyParameter) 

여기에 문서와 예제를 참조하십시오 또는 인수를 제공해야하는 호출 가능 객체 인 경우 을 boost :: thread 생성자에 추가 인수 을 전달하여 수행하면 이 될 수 있습니다.

void find_the_question(int the_answer); 

boost::thread deep_thought_2(find_the_question,42); 

희망이 있습니다.

7

미래의 작업을 위해 Boost가 기본적으로 인수를 값으로 전달한다는 점에 유의하기 바란다. 따라서 참조를 전달하려면 boost::ref()boost::cref() 메쏘드가 있고, 후자는 상수 참조 용입니다.

나는 여전히 참조 용으로 & 연산자를 사용할 수 있다고 생각하지만 확실하지는 않습니다. 항상 boost::ref을 사용했습니다.

+0

이 나를 비트. 고마워. – RandomInsano

6
thread_ = boost::thread(boost::function< void (void)>(boost::bind(&clientTCP::run , this))); 

bindfunction이 필요하고, 코드가 느린하고 더 많은 메모리를 사용합니다. 그냥 수행

thread_ = boost::thread(&clientTCP::run , this); 

는 인수가 단지 인수를 추가 추가하려면 :

thread_ = boost::thread(&clientTCP::run , this, f); 
관련 문제