Class

# CriticalSection

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

Used to protect a resource in a multithreaded environment.

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                 | Parameters            | Returns                            | Shared |
|--------------------------------------|-----------------------|------------------------------------|--------|
| `Enter<criticalsection.enter>`       |                       |                                    |        |
| `Leave<criticalsection.leave>`       |                       |                                    |        |
| `TryEnter<criticalsection.tryenter>` |                       | `Boolean</api/data_types/boolean>` |        |
| `Type<criticalsection.type>`         | `Types<thread.types>` |                                    |        |

## Method descriptions

<div id="criticalsection.enter">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

CriticalSection.Enter

**Enter**

> Attempts to get a lock on the resource managed by the <span class="title-ref">CriticalSection</span>.
>
> When the call to Enter succeeds, the function returns and your code has exclusive access to the protected resource. If the lock cannot be obtained immediately, Enter will block the current thread from continuing to run. It will wait for the resource to become available.
>
> Enter differs from the `Signal<semaphore.signal>` method of the `Semaphore</api/language/threading/semaphore>` class in that it can be called multiple times from the currently executing thread. If the calling thread already owns the lock, the method returns immediately. This makes CriticalSections very useful for calling a method recursively.

<div id="criticalsection.leave">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

CriticalSection.Leave

**Leave**

> Call Leave when you are finished using the protected resource and you want to give it back to the <span class="title-ref">CriticalSection</span>.
>
> Every time you call `Enter<criticalsection.enter>` or `TryEnter<criticalsection.tryenter>` and succeed, you must call Leave. This includes calling it recursively. Otherwise the resource will be protected indefinitely.

<div id="criticalsection.tryenter">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

CriticalSection.TryEnter

**TryEnter** As `Boolean</api/data_types/boolean>`

> Attempts to get a lock on the resource managed by the <span class="title-ref">CriticalSection</span>.
>
> TryEnter is similar to `Enter<criticalsection.enter>` but returns a `Boolean</api/data_types/boolean>`. If it succeeds, it returns `True</api/language/true>` and the thread has exclusive use of the resource. If it fails, it returns `False</api/language/false>` but does not block execution of the thread.

<div id="criticalsection.type">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

CriticalSection.Type

**Type** As `Types<thread.types>`

> Indicates the type of the <span class="title-ref">CriticalSection</span>, Cooperative or Preemptive. Cooperative is the default.
>
> Carefully read the `Preemptive Threads<thread.preemptive_threads>` notes before using them.

## Notes

A <span class="title-ref">CriticalSection</span> is similar to a `Semaphore</api/language/threading/semaphore>`, except a <span class="title-ref">CriticalSection</span> protects only one resource. The `Semaphore</api/language/threading/semaphore>` class, on the other hand, can protect more than one resource. The `Mutex</api/language/threading/mutex>` class is similar to a <span class="title-ref">CriticalSection</span>, but its scope is all the applications that are running on the user's computer, not just the current application. You can use a `Mutex</api/language/threading/mutex>`, for example, check whether another copy of the application is running and is using a needed resource.

You use critical sections in conjunction with `Threads</api/language/threading/thread>`. In a situation in which two or more of the threads might try to access the same item, then you should enclose the code with calls to the `Enter<criticalsection.enter>` (or `TryEnter<criticalsection.tryenter>`) and `Leave<criticalsection.leave>` methods.

One strategy is this:

- Create a subclass of `Thread</api/language/threading/thread>` which does the operations that might compete for access to a resource.
- Create a <span class="title-ref">CriticalSection</span> property of Public scope in, for example, the window that contains the control that calls the threads.
- In the Run event handler of the `Thread</api/language/threading/thread>`, call `Enter<criticalsection.enter>` (to get ownership and lock the resource) and `Leave<criticalsection.leave>` (to release ownership and the lock) before and after the code that tries to access the shared resource.

If a thread that has an ownership lock crashes, the lock is not released.

## Compatibility

|                       |     |
|-----------------------|-----|
| **Project Types**     | All |
| **Operating Systems** | All |

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `Mutex</api/language/threading/mutex>`, `Semaphore</api/language/threading/semaphore>`, `Thread</api/language/threading/thread>` classes

</div>
