Signal
Signals are tools provided by the 4D language to manage interactions and avoid conflicts between processes in a multiprocess application. Signals allow you to make sure one or more process(es) will wait for a specific task to be completed before continuing execution. Any process can wait and/or release a signal.
Semaphores can also be used to manage interactions. Semaphores allow you to make sure that two or more processes do not modify the same resource (file, record...) at the same time. Sólo el proceso que define el semáforo puede eliminarlo.
Objeto signal
A signal is a shared object that must be passed as a parameter to commands that call or create workers or processes.
A 4D.Signal
object contains the following built-in methods and properties:
Any worker/process calling the .wait()
method will suspend its execution until the .signaled
property is true. While waiting for a signal, the calling process does not use any CPU. This can be very interesting for performance in multiprocess applications. The .signaled
property becomes true when any worker/process calls the .trigger()
method.
Note that to avoid blocking situations, the .wait()
can also return after a defined timeout has been reached.
Signal objects are created with the New signal command.
Trabajar con señales
In 4D, you create a new signal object by calling the New signal
command. Once created, this signal must be passed as a parameter to the New process
or CALL WORKER
commands so that they can modify it when they have finished the task you want to wait for.
signal.wait()
must be called from the worker/process that needs another worker/process to finish a task in order to continue.signal.trigger()
must be called from the worker/process that finished its execution in order to release all others.
Once a signal has been released using a signal.trigger()
call, it cannot be reused again. If you want to set another signal, you need to call the New signal
command again.
Since a signal object is a shared object, you can use it to return results from called workers/processes, provided that you do not forget to write values within a Use...End use
structure (see example).
Ejemplo
var $signal : 4D.Signal
// Creation of a signal
$signal:=New signal
// call main process and execute OpenForm method
CALL WORKER(1;"OpenForm";$signal)
// do another calculation
...
// Waiting for the end of the process
$signaled:=$signal.wait()
// Processing of the results
$calc:=$signal.result+...
OpenForm method :
#DECLARE ($signal : 4D.Signal)
var $form : Object
$form:=New object("value";0)
// Open the form
$win:=Open form window("Information";Movable form dialog box)
DIALOG("Information";$form)
CLOSE WINDOW($win)
// Add a new attribute to your $signal shared object to pass your result to the other process:
Use($signal)
$signal.result:=$form.value
End use
// Trigger the signal to the waiting process
$signal.trigger()
Resumen
.description : Text contiene una descripción personalizada para el objeto |
.signaled: Boolean contiene el estado actual del objeto |
.trigger( ) pone la propiedad |
.wait( { timeout : Real } ) : Boolean hace que el proceso actual espere hasta que la propiedad |
Para evitar que el código se bloquee, puede pasar un tiempo máximo de espera en segundos en el parámetro timeout (se aceptan decimales).
Warning: Calling
.wait( )
without a timeout in the 4D main process is not recommended because it could freeze the whole 4D application.
Si signal ya está en el estado de señalización (es decir, la propiedad .signaled
ya es true), la función devuelve inmediatamente, sin esperar.
La función devuelve el valor de la propiedad .signaled
. La evaluación de este valor permite saber si la función devuelta porque el .trigger( )
se ha llamado (.signaled
es true) o si el timeout vencido (.signaled
es false).
The state of a process that waits for a signal is
Waiting for internal flag
.
|
New signal
Histórico
Versión | Modificaciones |
---|---|
v17 R4 | Añadidos |
New signal { ( description : Text ) } : 4D.Signal
Parámetros | Tipo | Descripción | |
---|---|---|---|
description | Texto | -> | Descripción para la señal |
Resultado | 4D.Signal | <- | Objeto nativo que encapsula la señal |
Descripción
El comando New signal
crea un objeto 4D.Signal
.
Una señal es un objeto compartido que puede ser pasado como parámetro de un worker o proceso a otro worker o proceso, de manera que:
- the called worker/process can update the signal object after specific processing has completed
- the calling worker/process can stop its execution and wait until the signal is updated, without consuming any CPU resources.
Opcionalmente, en el parámetro description puede pasar un texto personalizado que describa la señal. Este texto también puede definirse después de la creación de la señal.
Dado que el objeto señal es un objeto compartido, también se puede utilizar para mantener las propiedades del usuario, incluyendo la propiedad .description
, llamando a la estructura Use...End use
.
Valor devuelto
Un nuevo objeto 4D.Signal
.
Ejemplo
Este es un ejemplo típico de un worker que fija una señal:
var $signal : 4D.Signal
$signal:=New signal("This is my first signal")
CALL WORKER("myworker";"doSomething";$signal)
$signaled:=$signal.wait(1) //wait for 1 second max
If($signaled)
ALERT("myworker finished the work. Result: "+$signal.myresult)
Else
ALERT("myworker no ha terminado en menos de 1s")
End if
El método doSomething puede ser:
#DECLARE ($signal : 4D.Signal)
//todo proceso
//...
Use($signal)
$signal.myresult:=$processingResult //return the result
End use
$signal.trigger() // The work is finished
.description
Histórico
Versión | Modificaciones |
---|---|
v17 R4 | Añadidos |
.description : Text
Descripción
La propiedad .description
contiene una descripción personalizada para el objeto Signal
..
.description
puede definirse al crear el objeto signal o en cualquier momento. Tenga en cuenta que, dado que el objeto Signal
es un objeto compartido, cualquier acceso en modo de escritura a la propiedad .description
debe estar rodeado por una estructura Use...End use
.
Esta propiedad es lectura-escritura.
.signaled
Histórico
Versión | Modificaciones |
---|---|
v17 R4 | Añadidos |
.signaled: Boolean
Descripción
La propiedad .signaled
contiene el estado actual del objeto Signal
. Cuando se crea signal, .signaled
es False. Se convierte en True cuando se llama al objeto .trigger( )
.
Esta propiedad es de sólo lectura.
.trigger()
Histórico
Versión | Modificaciones |
---|---|
v17 R4 | Añadidos |
.trigger( )
| Parámetros | Tipo | | Descripción | | ---------- | ---- |::| ---------------------------- | | | | | No requiere ningún parámetro |
Descripción
La función .trigger( )
pone la propiedad signaled
del objeto signal como true y despierta a todos los workers o procesos que esperan esta señal.
Si la señal ya está en el estado de señalización (es decir, la propiedad signaled
ya es true), la función no hace nada.
.wait()
Histórico
Versión | Modificaciones |
---|---|
v17 R4 | Añadidos |
.wait( { timeout : Real } ) : Boolean
Parámetros | Tipo | Descripción | |
---|---|---|---|
timeout | Real | -> | Tiempo máximo de espera de la señal en segundos |
Resultado | Booleano | <- | Estado de la propiedad .signaled |
Descripción
La función .wait( )
hace que el proceso actual espere hasta que la propiedad .signaled
del objeto signal se convierta en true o expire el timeout opcional.
Para evitar que el código se bloquee, puede pasar un tiempo máximo de espera en segundos en el parámetro timeout (se aceptan decimales).
Warning: Calling
.wait( )
without a timeout in the 4D main process is not recommended because it could freeze the whole 4D application.
Si signal ya está en el estado de señalización (es decir, la propiedad .signaled
ya es true), la función devuelve inmediatamente, sin esperar.
La función devuelve el valor de la propiedad .signaled
. La evaluación de este valor permite saber si la función devuelta porque el .trigger( )
se ha llamado (.signaled
es true) o si el timeout vencido (.signaled
es false).
The state of a process that waits for a signal is
Waiting for internal flag
.