Routines (alphabetical) > Routines: S > SEM_CREATE

# SEM_CREATE

A semaphore is a software mechanism used to provide synchronization between multiple processes or between different threads of a single process. Often, semaphores are used to protect or restrict access to shared or key resources, such as shared memory segments.

The SEM_CREATE function creates a reference to a semaphore in the current IDL process. If the specified semaphore already exists in the system, a reference to the existing semaphore is created. If the semaphore does not exist, a new semaphore is created in the system and a reference to it is created in the current IDL process.

It is important to note that the semaphore itself is independent of any IDL process, but that the reference to the semaphore is unique to the IDL process that calls SEM_CREATE.

Note: On UNIX systems, a semaphore is marked for deletion when the process that created the semaphore exits. While this does not immediately delete the semaphore itself, it does free up the semaphore’s name. This means that after the process that created a semaphore exits — even if other processes have references to that semaphore — SEM_CREATE will create a new and distinct semaphore with the same name. Any subsequent processes that call SEM_CREATE with that name will receive a reference to the new semaphore.
If this behavior causes problems for your application, ensure that the process that creates a semaphore does not exit until all processes that use the semaphore have either exited or deleted their references to the semaphore.

## Syntax

Result = SEM_CREATE(strName [, /DESTROY_SEMAPHORE] )

## Return Value

The result is 1 (one) if the semaphore was created successfully or already exists in the system, or 0 (zero) if the semaphore does not exist but an error occurred during creation.

## Arguments

### strName

A scalar string containing the name associated with the semaphore. strName must conform to the rules for a valid file name on the target operating system.

Note: Semaphore names must be 24 characters or less on Macintosh platforms.

Tip: Use the IDL_VALIDNAME function to ensure that strName will be a valid semaphore name on all supported platforms.

## Keywords

### DESTROY_SEMAPHORE

Note: This keyword is ignored on Windows systems.
On Windows systems, a semaphore will be destroyed by a call to SEM_DELETE (in any IDL process) if no other processes have a reference to the semaphore.

Set the DESTROY_SEMAPHORE keyword to allow a call to SEM_DELETE in the current IDL process to destroy the semaphore, even if it was created in a different IDL process.

SEM_CREATE creates semaphores that persist in the operating system kernel until they are explicitly destroyed using the SEM_DELETE routine, or until the system is rebooted. A client program can access a semaphore, obtain or remove a lock, and then disconnect at any time. This is convenient in situations where the need for the semaphore is long lived, and programs that need it come and go. This behavior can create a problem, however, in that semaphores that are not explicitly destroyed can cause resource leaks in the operating system. It is important to properly destroy semaphores when they are no longer required.

The default behavior on UNIX systems is for IDL to destroy a semaphore when the SEM_DELETE routine is called only if the semaphore was created by the same IDL process — that is, if the call to SEM_CREATE in that IDL process created the semaphore itself and not just a reference to an existing semaphore. If the semaphore was created by another process, the call to SEM_DELETE destroys the reference to the semaphore within the current IDL process, but does not destroy the semaphore itself. This keyword alters the default behavior in the following way:

Value | Behavior
not set | The semaphore will only be destroyed by a call to SEM_DELETE in the same IDL process that created it. This is the default behavior.
1 | The semaphore will be destroyed by a call to SEM_DELETE in the current IDL process, even if the call to SEM_CREATE only created a reference to the semaphore.
0 | The semaphore will not be destroyed by a call to SEM_DELETE in the current IDL process, even if it was created by the current process.

## Example

See SEM_LOCK for an example using this function.

## Version History

6.3 | Introduced

## See Also

SEM_DELETE, SEM_LOCK, SEM_RELEASE, SHMMAP