Qt4를 사용하여 멀티 스레드 환경을 구현하고 싶습니다.멀티 스레딩 환경 구현
class Thread : public QThread {
QList<SubThread*> threads_;
public:
void run() {
foreach(SubThread* thread : threads) {
thread.start();
}
foreach(SubThread* thread : threads) {
thread.wait();
}
}
void abort() {
foreach(SubThread* thread : threads) {
thread.cancel();
}
}
public slots:
// This method is called from the main-thread
// (sometimes via some signal-slot-connection)
void changeSomeSettings() {
abort();
// change settings
start();
}
}
class SubThread : public QThread {
bool isCancelled_;
public:
void run() {
while(!isCancelled or task completed) {
// something that takes some time...
}
}
void cancel() {
if(isRunning() {
isCancelled_ = true;
}
}
}
목적은 슬롯 changeSomeSettings()가 실행중인 모든 스레드를 죽이는 것을, 그 변경 내용을 커밋하고 다시 시작 : 모두 의사 코드 - C++에서 다음과 같이 좋습니다. 이 메서드를 시작한 후에는 "abort"를 호출 한 다음 모든 스레드가 종료 될 때까지 대기합니다. 잘못된 방법으로 뮤텍스를 사용 :이 실제로에서라도에서 Qt를 작동
void Thread::changeSomeSettings() {
mutex1.lock();
abort();
mutex2.lock();
start();
mutex1.unlock();
}
void Thread::run() {
foreach(Thread* thread : threads) {
thread.start();
}
foreach(Thread* thread : threads) {
thread.wait();
}
mutex2.unlock();
}
을 아직 문서 mutex2에 따라 동일한 스레드에서 잠금을 해제해야합니다 (및 Windows에서 오류가 발생합니다). 경쟁 조건과 교착 상태에 빠지지 않고 내 목표를 달성하는 가장 좋은 방법은 무엇입니까? 내가 제안한 디자인보다 더 좋은 디자인이 있습니까?
QWaitConditions에 대한 힌트를 제공해 주셔서 감사합니다. – Searles