简体   繁体   中英

QCoreApplication::processEvents - why must it be called from main thread only?

Since there is only one QCoreApplication object, why does it matter from which thread QCoreApplication::processEvents() is called?

After all, the threads all share the same address space.

Your title is not true. Probably you are assuming QCoreApplication::processEvents is a system-wide "process all possible events of all threads" call, which it is not. You can call it from any thread you are in (and which is running an event loop).

Why does it matter from which thread it is called: In Qt the event loop is a per-thread resource (you can run own event loop on any thread) and QCoreApplication::processEvents processes event queue of the current thread.

If you're seeing this warning it is because code inside the processEvents is not thread-safe . Even though they share the same address space, work or data could be accumulated in a state on one thread, then the thread may be interrupted, the state could be modified by another thread, and then the original thread will resume its process as if the original state was still valid, but now it isn't. You could lose data or corrupt memory if you were doing things like modifying a linked list or any non-threadsafe data structure, among other hazards.

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