SyncLock Statement

Synchronizes tasks using a mutual exclusion lock.

Syntax
SyncLock syncID [, timeOut]

Parameters

syncID
Specify the signal number to receive (an integer from 0 to 63) as an expression or numeric value.
timeOut
Optional. Real expression representing the maximum time to wait for lock.

Description
Use SyncLock to lock use of a common resource so that only one task at a time can use it. When the task is finished with the resource, it must call SyncUnlock to release the lock so other tasks can use it.

A task can only unlock a syncID that it previously locked.

A task must execute SyncUnlock to release the lock. If the task is finished, then the lock it previously locked will releases.

When SynLock is second consecutive used to a same signal number, an error occurs.

If the timeOut parameter is used, then the Twcmd_tw function must be used to check if the lock was successful.

Note


In EPSON RC+ 6.0, RC+ 7.0, and Epson RC+ 8.0, the lock is automatically released when the task is finished while it is not in EPSON RC+5.0.


See Also
Signal, SyncLock, Tw, Wait, WaitPos

SyncLock Statement Example
The following example uses SyncLock and SyncUnlock to allow only one task at a time to write a message to a communication port.

Function Main

    Xqt Func1
    Xqt Func2
Fend

Function Func1
  Long count
  Do
    Wait .5
    count = count + 1
    LogMsg "Msg from Func1, " + Str$(count)
  Loop
Fend

Function Func2
  Long count
  Do
    Wait .5
    count = count + 1
    LogMsg "Msg from Func2, " + Str$(count)
  Loop
Fend



Function LogMsg(msg$ As String)
  SyncLock 1
  OpenCom #1
  Print #1, msg$
  CloseCom #1
  SyncUnlock 1
Fend

The following example uses SyncLock with optional time out. Tw is used to check if the lock was successful. By using a timeout, you can execute other code periodically while waiting to lock a resource.

Function MySyncLock(syncID As Integer)
  Do
    SyncLock syncID, .5
    If Tw = 0 Then
      Exit Function
    EndIf
    If Sw(1) = On Then
      Off 1
    EndIf
  Loop
Fend