일부 직원이 여기까지 나를 도왔습니다. Call function inside a lambda passed to a thread 내 작업자 클래스가 이동 생성자와 이동 operator=
을 지원할 수 있지만 클래스에 문제가 있습니다. 스레드에 대한 복사 (또는 참조)를 통해 클래스 값에 액세스 할 수 있도록 this
을 바인딩합니다. 다음은 여러 개의 atomic<bool>
, condition_variable
및 mutex
입니다.스레드를 멤버 변수로 사용하는 클래스의 작업 이동
그러나 스레드가 다른 조건 변수 mutex
및 atomic
에 바인딩 된 이후로 이동하려고하면 아무 작업도 수행되지 않습니다. 이 문제를 어떻게 해결할 수 있습니까? 좀 더 복잡한 객체를 사용해야하고 람다 대신에 그것을 움직여야하므로 스레드가 그것에 대한 레퍼 런스를 가질 수 있습니까? 또는 다른 대안이 있습니다. 항상 도움이 될 것입니다 :).
다음은 구현의 스 니펫 (MWE)입니다.
class worker {
public:
template <class Fn, class... Args>
explicit worker(Fn func, Args... args) {
t = std::thread(
[&func, this](Args... cargs) -> void {
std::unique_lock<std::mutex> lock(mtx);
while (true) {
cond.wait(lock, [&]() -> bool { return ready; });
if (terminate)
break;
func(cargs...);
ready = false;
}
},
std::move(args)...);
}
worker(worker &&w) : t(std::move(w.t)) { /* here there is trouble */ }
worker &operator=(worker &&w) {
t = std::move(w.t);
terminate.store(wt.terminate);
ready.store(wt.ready);
return *this;
/* here too */
}
~worker() {
terminate = true;
if (t.joinable()) {
run_once();
t.join();
}
}
worker() {}
void run_once() {
std::unique_lock<std::mutex> lock(mtx);
ready = true;
cond.notify_one();
}
bool done() { return !ready; }
private:
std::thread t;
std::atomic<bool> ready, terminate; // What can I do with all these?
std::mutex mtx; //
std::condition_variable cond; //
};
int main() {
worker t;
t = worker([]() -> void { cout << "Woof" << endl; });
t.run_once();
while(!t.done()) ;
return 0;
}
큰 코드 덤프는 죄송합니다.
문제는 한 스레드에서 사용중인 개체를 다른 스레드에서 아래로 이동한다는 것입니다. 질문 : 왜? 개체를 동적으로 만들고 스마트 포인터를 사용하십시오. – kfsone
@kfsone Y-yeah는 내가 두 번째 질문에 대해 궁금해하는 점이었고, 좀 더 우아한 것이 있는지 또는 내가 빠져 있는지 알고 싶었습니다. – Aram