简体   繁体   中英

Starting thread from for loop blocks UI

I am compressing video and in the future I'd like to compress multiple frames at once. I have a thread and I call it to do work on each of the frames - that's why I have for loop in my UI class. The problem is that UI is blocked. Why? Is this becuase I am calling the thread from for loop?

Code causing UI block (Dialog.cpp):

for(int i=0; i<nFrames; i++)
{
    //grab next frame from video source

    myThread.setFrame(newFrame);
    myThread.start();
    myThread.wait();
    result.append(myThread.GetResult());
}

You're calling wait , why would you expect that wait won't block ui?

From Qt docs:

Blocks the thread until either of these conditions is met:

The thread associated with this QThread object has finished execution (ie when it returns from run()). This function will return true if the thread has finished. It also returns true if the thread has not been started yet. time milliseconds has elapsed. If time is ULONG_MAX (the default), then the wait will never timeout (the thread must return from run()). This function will return false if the wait timed out.

From doc:

bool QThread::wait ( unsigned long time = ULONG_MAX )

Blocks the thread until either of these conditions is met:

The thread associated with this QThread object has finished execution (ie when it returns from run()).

...

This provides similar functionality to the POSIX pthread_join () function.

You shouldn't use wait here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM