QT QEventLoop / QThread ¿cómo sincronizar en un bucle de eventos?

I need a way to know when an event loop has started and when it is about to exit. I am trying to synchronize between threads and there is hard to resolve race condition involving isRunning at the times when the loop starts and when it exits.

I believe for starting I can simply post an event since it will be queued and dispatched once it starts. But for exiting I'm not sure how to handle it. I need some kind of event that will happen just prior to the loop completing.

¿Hay alguna forma de lograr esto?


The reason I need this is for posting events to the thread. I have a function, a cleanup function in particular, say cleanupObject. This function can be called from any thread and is expected to do cleanup on the object. These objects are owned by a thread with an event loop. If this event loop is running this cleanupObject request has to be processed as an event in that event loop. If the event loop is not running then the cleanup has to be handled differently (this happens during shutdown).

The race exists because isRunning has no synchronization I can use, so while it said true when I called it, it may not longer be running the instant after. I need to know whethere the event I post to the loop will be processed or not, because if it will not be processed I must do cleanup in a different fashion.

preguntado el 09 de marzo de 12 a las 16:03

haría isFinished funciona mejor que isRunning at the end of the event loop? It might be useful to provide more information about the nature of the race condition and how your synchronization is currently designed. -

It'd help if you told us what you plan to do in this "just before exiting loop" event, i.e. why QThread::finished() is not good. -

Rather than posting an event directly, have you tried using signals and slots via the AutoConnection connection type? It may be smart enough to be executed in either case - in the target thread if the thread is running, or in the caller thread if not. I haven't heard of signal calls being lost when a thread stops running, so I suspect this approach may work for you. -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.