A program in the background thread waits for notification from another process, creating a QSystemSemaphore and calling it the acquire() method. When you close the program, you must correctly complete the background thread. Is there a way to do this (in fact, to interrupt acquire() ) without generating an additional ("fake") event?

  • Specify: Do you want to interrupt the acquire() semaphore in the background process, thereby indicating to him that it is time to round out when activating the closure of the main program? - alexis031182
  • Exactly. It seemed to me that this is a fairly common scenario. - groaner

1 answer 1

In QSystemSemaphore release acquire() used to free resources occupied by acquire() release() . This is the only way to unlock a previously created lock.

System semaphores (and non-system ones too) are usually not used to indicate a certain state of the process while waiting for an application-level event. The word “usually” comes from the fact that there can be many types of events and then you have to produce your own semaphore for each of them, which in turn will lead to completely unnecessary complication of the code.

Semaphores are intended for unambiguous access of execution context (contexts) to a certain shared resource. The state of the application is not their diocese.

QSystemSemaphore used, for example, in a QSharedMemory that provides secure access to the inter-process shared memory. Also, using QSystemSemaphore you can organize secure access to other resources. For example, to a file, if you need to perform with it a certain transactional set of read and write operations.

The idea of ​​using semaphores is such that the execution context of one process (thread) should not be blocked if this does not interfere with the work of another. Simply put, worked, kindly release as soon as possible. The state of the application is just a flag, sometimes implying a very long-lasting immutability, as well as the ability to take values ​​that are not limited only to "available" and "not available."

To organize interprocess communication, when you need to notify one process about the state of another, it is better to use tools designed for this purpose. This can be the aforementioned QSharedMemory , and named pipes (in the context of Qt, this is QLocalSocket / QLocalServer ).

Of course, for a simple process, this may seem redundant, but it is, if you can put it that way, a good tone, as is the good practice of the processing of signals coming directly from the operating system. For example, the need to close the process in an emergency or because of a reboot of the machine.