Previous IDL Reference Guide: Procedures and Functions Next

SEM_LOCK

Syntax | Return Value | Arguments | Keywords | Example | Version History | See Also

The SEM_LOCK function attempts to gain the lock on an existing semaphore (created by a call to the SEM_CREATE function) for the current IDL process. Once the lock has been obtained by one IDL process, other IDL processes will not be able to gain the lock until the SEM_RELEASE process is called within the IDL process that owns the lock.

Syntax

Result = SEM_LOCK(strName)

Return Value

The result is 1 (one) if the lock on the specified semaphore was successfully obtained, or 0 (zero) if the lock could not be obtained.

Arguments

strName

A scalar string containing the name associated with the semaphore. This is the name used when creating the semaphore with SEM_CREATE.

Keywords

None

Example

This example illustrates the way semaphores operate using two independent IDL sessions on the same machine.

  1. The user starts IDL session 1.
  2.  

  3. The user creates a semaphore using the following command:
  4. status = SEM_CREATE('semaphore1')  
    

     

    This creates a semaphore called "semaphore1".

     

  5. The user sets a lock on the semaphore using the following command:
  6. status = SEM_LOCK('semaphore1')  
    

     

    If the lock is obtained, status is set equal to 1.

     

  7. The user starts IDL session 2.
  8.  

  9. In session 2, the user creates a reference to the same semaphore created in session 1:
  10. status = SEM_CREATE('semaphore1')  
    

     

  11. In session 2, the user attempts to gain the lock on the semaphore with the following command:
  12. status = SEM_LOCK('semaphore1')  
    

     

    Since the lock is owned by session 1, status is set equal to 0 (the lock was not obtained).

     

  13. In session 1, the lock on the semaphore is released using the following command:
  14. SEM_RELEASE, 'semaphore1'  
    

     

  15. Once the lock has been released in session 1, the user once again attempts to gain the lock on the semaphore in session 2:
  16. status = SEM_LOCK('semaphore1')  
    

     

    In this case, status is set equal to 1, indicating session 2 now has the lock

     

  17. In session 1, attempting to retrieve the lock will result in failure, since session 2 has the lock on the semaphore.
  18.  

  19. In session 2, the user executes the following command:
  20. SEM_DELETE, 'semaphore1'  
    

     

    This removes the reference to the semaphore in session 2, but does not destroy the actual semaphore in the operating system.

     

  21. In session 1, the user executes the following command:
  22. SEM_DELETE, 'semaphore1'  
    

     

    This removes both the reference to the semaphore in session 1 and the actual semaphore in the operating system, because:

    • If the user is on a UNIX system, the semaphore itself is destroyed because it was created in session 1 and there are no other references it.
    •  

    • If the user is on a Windows system, the semaphore itself is destroyed because session 1 has the last reference to it.

Version History

6.3
Introduced

See Also

SEM_CREATE, SEM_DELETE, SEM_RELEASE

  IDL Online Help (March 06, 2007)