C++ (Qt)#include <iostream>#include <atomic>#include <memory>#include <thread> template <class Function>class runable{public: typedef std::atomic_bool atomic_bool; runable(Function && f, atomic_bool & finished) : m_f(f), m_finished(finished) {} void operator()() const { m_f(); m_finished = true; }private: Function m_f; atomic_bool & m_finished; }; class thread{public: typedef std::atomic_bool atomic_bool; template <class Function> thread(Function && f) : m_finished(false) { m_thread_ptr = std::make_shared<std::thread>(runable<Function>(f, m_finished)); } void join() { m_thread_ptr->join(); } bool fnished() const { return m_finished; }private: atomic_bool m_finished; std::shared_ptr<std::thread> m_thread_ptr;}; void func() { std::cout << " Child thread-id = " << std::this_thread::get_id() << std::endl;} int main(){ std::cout << "Main thread-id = " << std::this_thread::get_id() << std::endl; thread th(func); while (!th.fnished()) {} std::cout << "finished!\n"; th.join(); return 0;}
C++ (Qt) void operator()() const { m_f(); m_finished = true; }
C++ (Qt)void setvar::execute( connection_ptr c ){ calc::longops::instance()->start( boost::bind( &recalc, c->get_context(), boost::ref( m_vars ) ), // Запустить recalc в отдельной нитке boost::bind( &setvar::send_reply, shared_from_this(), c, _1 ) // После завершения выполнения вызвать send_reply. // В качестве аргумента send_reply будет передан результат выполнения recalc. );}