다중 스레드 응용 프로그램에서 자동 연결 관리로 부스트 신호 2를 사용하고 싶습니다. 내 클래스는 enable_shared_from_this<>
에서 상속 받고 다른 멤버 메서드 내에서 멤버 메서드를 연결하려고합니다. 내 코드는 가능한 한 빨리해야한다, 그래서 연결이 자주 재건 될 수있다 (에도 불구하고 부스트 signals2 성능 자체) :enable_shared_from_this가 임베디드 weak_ptr에 직접 액세스 할 수없는 이유는 무엇입니까?
typedef boost::signals2::signal<void()> signal_type;
struct Cat : public enable_shared_from_this<Cat>
{
void meow();
void connect (signal_type& s)
{
// can't write this
s.connect (signal_type::slot_type (&Cat::meow, this, _1).track (weak_from_this()));
// ok, but slow?! two temporary smart pointers
weak_ptr<Cat> const myself (shared_from_this());
s.connect (signal_type::slot_type (&Cat::meow, this, _1).track (myself));
}
// i am missing something like this in the base class
// protected:
// weak_ptr<Cat> const& weak_from_this();
};
내 설계 목표는 충돌 할 수 있음을 알고 (자동 연결 관리 및 스레드 안전뿐만 아니라 어쨌든 빠른 코드) 만 :
이유에
enable_shared_from_this<>
부족 직접 액세스가 포함 된 않습니다weak_ptr<>
? 나는 반대의 이유를 볼 수 없습니다. 내 비슷한 유스 케이스가 없습니까?위의 해결 방법보다 빠른 해결 방법이 있습니까?
편집 :
나는이 같은 somethink을 할 수있어,하지만 난 추가 스토리지/초기화 검사 처벌 피하려고 : 아니오 있기 때문에
template <typename T>
struct enable_weak_from_this : public enable_shared_from_this<T>
{
protected:
weak_ptr<T> /* const& */ weak_from_this()
{
if (mWeakFromThis.expired())
{
mWeakFromThis = this->shared_from_this();
}
return mWeakFromThis;
}
private:
weak_ptr<T> mWeakFromThis;
};
"느린"사람은 누구입니까? 그것에 대해 "느린"무엇입니까? –
2 개의 임시 스마트 포인터 인스턴스가 필요합니다.이 인스턴스는 이미 존재하는 weak_ptr을 생성하기 위해 적어도 4 개의 불필요한 연동 연산을 발생시킵니다. – eel76
첫째, * * "이미 존재하지 않습니다"; 내 대답을 보라. 둘째로, 그렇게 느리게는되지 않습니다. 이 문제가 있음을 나타내는 프로파일 링 데이터가 있습니까? –