Cicode Programming Reference > Cicode Function Categories > Task Functions Introduction > EnterCriticalSection

EnterCriticalSection

Requests permission for the current thread to have access to a critical section (shared critical resource). If the critical section is already being accessed by another thread (using the EnterCriticalSection() function), the current thread will be granted access when the other thread relinquishes ownership using the LeaveCriticalSection() function.

Once a thread has ownership of a critical section, it can access the same section repeatedly (using the EnterCriticalSection() function each time). Remember, however, that LeaveCriticalSection() needs to be called once for each EnterCriticalSection() used.

Note: This function is process-based, not computer-based, and so is only effective for threads within the same process. Any threads attempting to gain access to this critical section will be blocked until the thread relinquishes ownership. This function will have no effect on threads running within other processes.

 

UNINTENDED EQUIPMENT OPERATION

Do not attempt to access a critical section that is already in use by another thread. This will cause the thread to be blocked until the critical section is relinquished.

Failure to follow these instructions can result in death, serious injury, or equipment damage.

Syntax

EnterCriticalSection(sName)

sName:

The name of the critical section. The name needs to be entered in quotation marks.

Return Value

This function does not return a value.

Related Functions

LeaveCriticalSection

Example

/* Request access to critical section, execute code and relinquish 
ownership of critical section. */
FUNCTION
MyCriticalFunction()
EnterCriticalSection("MyCritical");
// critical code is placed here
LeaveCriticalSection("MyCritical");
END

See Also

Task Functions