当我尝试在 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 的过程。如果我使用上面的代码,数据是完全错误的。因为第二个线程将在第一个线程完成之前开始。
然后我像这样更改我的代码:
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
}
这段代码的问题是线程的 processbar 直到 for 循环遍历所有文件才工作。
有什么方法可以在 for 循环中进行线程处理?