forループで作業スレッドを実行しようとすると問題が発生しました。
私のコードは次のようなものです:
connect(&myworkingthread,SIGNAL(updataprocess(int)),processbar2,SLOT(setValue(int)));
for (int i=0;i<n;i++){
// each individual data will be loaded in this part...
myworkingthread.start();// this thread will take 5 secs to finish, a signal is
// also emitted to show the process of this thread(processbar2).
// after working thread, the processed data will be saved...
processbar1->setValue(i); // processbar is used to show the processing process
//problem of this code: data is totally wrong because the next thread will start before the last one finish.
}
また、シグナルとスロットによって実装されるはずの myworkingthread のプロセスも示したいと思います。上記のコードを使用すると、データが完全に間違っています。最初のスレッドが終了する前に 2 番目のスレッドが開始されるためです。
次に、コードを次のように変更します。
connect(&myworkingthread,SIGNAL(updataprocess(int)),processbar2,SLOT(setValue(int)));
for (int i=0;i<n;i++){
// each individual data will be loaded in this part...
myworkingthread.start();// this thread will take 5 secs to finish
// signal is also emitted to show the process of this thread(processbar2).
myworkingthread.wait();// i will wait the thread until it finish
// after working thread, the processed data will be saved...
processbar1->setValue(i); // processbar is used to show the processing process
}
このコードの問題は、for ループがすべてのファイルを通過するまで、スレッドのプロセスバーが機能しないことです。
forループでスレッドプロセスを作成する方法はありますか?