c++ - Attempt to use a deleted function in std::thread -


my code follows:

void internal_listener(tnreceiver *t){     std::string oldval = "";     while (true) {         void *holder = t+offset;         std::string val = *(std::string *)holder;         if(val == oldval){          }else{             time_t tine;             std::cout << "[" << time(&tine) << "] : logger msg recv: " << val;         }     } }  tnreceiver::tnreceiver(int reg){     this->register_id = reg; }  void tnreceiver::register_to_net(tnnet *net){     net->add_transceiver(new tndata(this->register_id, "")); }  void tnreceiver::start_listen(){     std::thread listen{this};     listen.join(); } 

how fix this? error @ std::thread listen{this}. not want pass "copy" of tnreceiver. solutions great!

if try copy construct thread, you'll error because copy constructor deleted. can move construct thread:

thread t1(f);  thread t2 {std::move(t1)};   // move construct.   t2.join();  

online demo

but active thread no longer attached initial object. in code, give problems because apparently object constructing new thread thread , might break of assumptions.

note: intent not clear create thread join afterwards. if add more information intent , tnreceiver post comment , i'll update


Comments