---
name: "SDL_SemTryWait"
section: "3"
platform: "linux"
package: "libsdl1.2-dev"
version: "1.2.15+dfsg2-8"
release: "Debian 12 (Bookworm)"
platforms: ["linux"]
url: "https://new.carta.tech/SDL_SemTryWait"
---

# SDL\_SemTryWait(3)

Attempt to lock a semaphore but don't suspend the thread.

## NAME

SDL\_SemTryWait - Attempt to lock a semaphore but don't suspend the thread.

## SYNOPSIS

**#include "SDL.h"** **#include "SDL\_thread.h"**

**int SDL\_SemTryWait**(**SDL\_sem \*sem**);

## DESCRIPTION

**SDL\_SemTryWait** is a non-blocking varient of **SDL\_SemWait**. If the value of the semaphore pointed to by **sem** is positive it will atomically decrement the semaphore value and return 0, otherwise it will return **SDL\_MUTEX\_TIMEOUT** instead of suspending the thread.

After **SDL\_SemTryWait** is successful, the semaphore can be released and its count atomically incremented by a successful call to *SDL\_SemPost*.

## RETURN VALUE

Returns **0** if the semaphore was successfully locked or either **SDL\_MUTEX\_TIMEOUT** or **-1** if the thread would have suspended or there was an error, respectivly.

If the semaphore was not successfully locked, the semaphore will be unchanged.

## EXAMPLES

```
res = SDL_SemTryWait(my_sem);
if (res == SDL_MUTEX_TIMEOUT) {
        return TRY_AGAIN;
}
if (res == -1) {
        return WAIT_ERROR;
}
...
SDL_SemPost(my_sem);
```

## SEE ALSO

**SDL\_CreateSemaphore**, **SDL\_DestroySemaphore**, **SDL\_SemWait**, **SDL\_SemWaitTimeout**, **SDL\_SemPost**, **SDL\_SemValue**
