c++之packaged_task和future實現異步效果_拷貝構造函數

class CObject {
public:
	CObject(string str) :m_str(unique_ptr<string>(new string(str))) {

		cout << "構造函數" << endl;
	}
	~CObject() {
		if (m_str)
			cout << "析構函數: " << *m_str << " [" << this << "]" << endl;
		else
			cout << "析構函數: (moved) [" << this << "]" << endl;
	}
	CObject(const CObject& obj) {
		m_str = make_unique<string>(*obj.m_str);
		cout << "拷貝構造函數" << endl;
	}

	CObject(CObject&& obj) noexcept {
		m_str = move(obj.m_str);
		cout << "移動構造函數" << endl;
	}
	CObject* show() {
		cout << "m_str=" << *m_str << endl;
		return this;
	}
	CObject* show_error() {
		cout << "this is error" << endl;
		return this;
	}

	void operator()(int num) {
		cout << "num=" << num << endl;
	}
	void update_str(char *str) {

		*m_str = string(str);
	}

private:

	unique_ptr<string>m_str;

};

調用:

std::packaged_task<CObject(string)> task([](string str) {
			return CObject(str); });
		std::future<CObject> fut = task.get_future();
		std::thread t(std::move(task),"蕭海");  // 任務交給線程執行
		CObject obj=move( fut.get());
		obj.show();
		t.join();