저는 GUI 응용 프로그램 인 작은 응용 프로그램을 구현하려고하는데, 그 중 하나가 주요 작업 중 하나입니다. 분명히 나는이 "작업"을 별도의 스레드에 넣고이 스레드에서 start fnc를 호출하여이 스레드를 시작합니다. 불행히도 아무 일도 일어나지 않습니다. 다른 한편으로는 시작 대신에 구현 된 fnc 실행 계산을 수행해야합니다. 물론 gui와 같은 스레드에서 수행해야합니다. 무엇을해야합니까?
는 그래서 클래스 QThread 상속이 있습니다start가 실행을 호출하지 않습니다.
class Working_Thread : public QThread
{
Q_OBJECT
public:
typedef boost::filesystem3::path path_t;
private:
bool& cancel_flag_;
const std::set<path_t>& paths_;
int search_depth_;
typedef void (Dir_File_Select_Dialog::*fnc_ptr)(const std::set<path_t>&,int);
fnc_ptr fnc_;
Dir_File_Select_Dialog* parent_;
protected:
void run()
{
(parent_->*fnc_)(paths_,search_depth_);
}
public:
Working_Thread(bool& cancel_flag,const std::set<path_t>&,int&,fnc_ptr fnc,Dir_File_Select_Dialog* parent);
};
을 내가 새 스레드를 시작하려고 할 때 여기에 조각 GUI 스레드에서입니다 :
Working_Thread* working_thread = new Working_Thread(cancel_flag,paths,search_depth,&Dir_File_Select_Dialog::extract_files_,this);
working_thread->start();//this desn't invoke run fnc
을하지만 난 할 때
working_thread->run();//this will perform computations although in gui thread
업데이트 :
약간의 변경 이제 시작 fnc를 사용할 때 계산을 수행하지만 여전히 GUI를 차단합니다.
Working_Thread* working_thread = new Working_Thread(cancel_flag,paths,search_depth,&Dir_File_Select_Dialog::extract_files_,this);
working_thread->start();//hangs
working_thread->wait();//when I call wait here computation is performed but GUI is blocked.
일부 노트 : 1. 어떻게 너는 이해하고 있니? 2. GUI 객체가 계산을 수행하는 이유는 무엇입니까? 그들은 GUI와 연결되어 있습니까? 모든 GUI 작업은 GUI 스레드에서 호출되어야하며 Qt의 모든 부분은 스레드로부터 안전하지 않다는 것을 알고 계십니까? – Lol4t0
@ Lol4t0 1. 아무런 효과가 보이지 않기 때문에 실행되지 않았습니다. 직접 실행할 때와 달리보기에 결과가 표시되지 않습니다. 2. Gui는 계산을 수행하지 않습니다. 어디에서이 점을 이해 했습니까? 그렇습니다. 이해합니다. – smallB
간단한 디버그 출력을 함수에 추가하여 확인하십시오. 보기를 변경하려면 이미 다른 스레드에서 Gui가 변경 중이라는 의미입니다. – Lol4t0