2017-08-14 2 views
-1

共有メモリを使用してスレッドを中断しようとしていますが、それほど簡単ではありません。私はBoost.lockfreeとBoost.Threadを使用しています。他のスレッドのスタックに空きメモリがありません

typedef std::shared_ptr<Parameters_parser> sh_params_t; 
typedef std::shared_ptr<Shared_objects> sh_obj_t; 
class Abstract_thread { 
    public: 
     Abstract_thread(sh_params_t params, sh_obj_t sh_obj): 
      params(params), sh_obj(sh_obj) {} 
     virtual ~Abstract_thread() = 0; 
     virtual void launch() = 0; 
    protected: 
     /** The shared parameters of the execution. */ 
     sh_params_t params; 
     /** The Shared_objects containing all queues. */ 
     sh_obj_t sh_obj; 
}; 
inline Abstract_thread::~Abstract_thread(){ 
    cout << "Abstract_thread destructor with params: " 
     << params.use_count() << " and sh_obj: " << 
     sh_obj.use_count() << endl; 
} 
class Interface_sniffer : public Abstract_thread { 
    Interface_sniffer(sh_params_t params, sh_obj_t sh_obj): 
     Abstract_thread(params, sh_obj) {} 
    ~Interface_sniffer(){ 
     cout << "Good bye from Interface_sniffer1 !" << endl; 
    } 
    void launch(){ 
     try { 
      while(true) 
       interruption_point(); 
     } catch(thread_interrupted const& e) { 
      disable_interruption di; 
      delete this; 
     } 
    } 
}; 

メインに、次に:ここで私が作成したクラスがある

void main(int argc, char* argv[]) { 
    sh_obj_t sh_obj = std::make_shared<Shared_objects>(); 
    sh_params_t params = std::make_shared<Parameters_parser>(); 
    Interface_sniffer sniffer1(params, sh_obj); 
    boost::thread t1(bind(&Interface_sniffer::launch, &sniffer1)); 
    boost::this_thread::sleep_for(boost::chrono::seconds(5)); 
    cout << "trying to stop 1" << endl; 
    t1.interrupt(); 
    t1.join(); 
} 

Parameters_parserShared_objectsのみ、または::ブーストと私のスレッドによって読み込まれるフィールドを含む基本クラスですlockfree :: queue、これは問題ではありません。私はそれを実行したときに

だから、valgrindのは、私に語った:

trying to stop 1 
Good bye from Interface_sniffer1 ! 
Abstract_thread destructor with params: 2 and sh_obj: 2 
==2095== Thread 2: 
==2095== Invalid free()/delete/delete[]/realloc() 
==2095== at 0x4C2F24B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==2095== by 0x41A1C1: Interface_sniffer::~Interface_sniffer() (interface_sniffer.cpp:34) 
==2095== by 0x41A6DC: Interface_sniffer::launch() (interface_sniffer.cpp:69) 
==2095== by 0x44D1E8: void std::_Mem_fn_base<void (Interface_sniffer::*)(), true>::operator()<, void>(Interface_sniffer*) const (in somewhere) 
==2095== by 0x44D162: void std::_Bind<std::_Mem_fn<void (Interface_sniffer::*)()> (Interface_sniffer*)>::__call<void, , 0ul>(std::tuple<>&&, std::_Index_tuple<0ul>) (in somewhere) 
==2095== by 0x44D115: void std::_Bind<std::_Mem_fn<void (Interface_sniffer::*)()> (Interface_sniffer*)>::operator()<, void>() (in somewhere) 
==2095== by 0x44CB9B: boost::detail::thread_data<std::_Bind<std::_Mem_fn<void (Interface_sniffer::*)()> (Interface_sniffer*)> >::run() (thread.hpp:116) 
==2095== by 0x50C95D4: ??? (in /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.58.0) 
==2095== by 0x604D6B9: start_thread (pthread_create.c:333) 
==2095== by 0x636A3DC: clone (clone.S:109) 
==2095== Address 0xfff000240 is on thread 1's stack 
==2095== in frame #3, created by main (main.cpp:23) 
==2095== 

だから、私はメインスレッドのスタック上にあるものを削除しています(私はparamssh_objを想定)が、それはので、真実ではないことをふりshared_ptrが指すオブジェクトはヒープ上にあります。私に何ができる ? shared_ptr<>*をスレッドに渡す必要がありますか?params->reset()sh_obj->reset()をデストラクタで手動で呼び出しますか?

答えて

3

Interface_snifferは、このコードが含まれています

delete this; 

そしてmain()は、このコードが含まれています。

Interface_sniffer sniffer1(params, sh_obj); 

だから、valgrindの言うとおりに、スタックに割り当てられたオブジェクトを削除しています。これは未定義の動作です。

delete thisは非常に珍しいですが(時には有効であり、有用であるかもしれません)、それがなぜ必要なのかを再考することは良い考えです。あなたのコードから、あなたが必要とすることは明らかではありません。

関連する問題