c++ - MoveConstructor for classes with std::thread member -


i'm bit unsure writing move constructor, class executing member function, whom running in member thread (copy constructor/assign been deleted, 1 of afaik obvious reason). know far i'm copying object pointer inital thread constructor, can use right function in right context. this->worker = std::thread (&class::working_loop,this); assume question does std::thread move constructor change function context?

i doubt that, i'm not sure how sophisticated thread move constructor is.

otherwise assume, have stop thread in old object, move/copy relevant data, , create new thread on new object?

(to essential stripped example below)

class directory     {     private:         std::thread worker;         std::atomic<bool> stop;         void working_loop();     public:         void start_monitor();         void stop_monitor();          directory (directory &&dir);         directory(const directory&) = delete;         directory& operator= (const directory&) = delete;         ~directory();     }; void directory::start_monitor() {     stop = false;     worker = std::thread (&directory::working_loop,this); } void directory::stop_monitor() {     stop = true;     if (worker.joinable())     {         worker.join();       }     else      {         std::cerr << "warning thread " << worker.get_id() << " joined!" << std::endl;            } }    

edit: working loop connects database further data belonging object(i.e. directory) , uses them later directory scanning/notification callbacks, while class user uses organise multiple directories. class type should able std::vector::emplace_back ed vector class user.

worker = std::thread (&directory::working_loop,this); 

here danger when moving...

the thread of execution bound member function on specific instance of on object. if object changes location in memory, sorts of trouble awaits thread.

don't move it.

you don't show implementation of thread's function working_loop(), i'll assume requires use of std::atomic<bool> stop communicate condition stop thread's execution. consider alternatives implementation of thread being bound member function (i.e. free functions, or static functions). use thread communication mechanisms such condition_variables, shared mutex objects or share stop variable.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -