THIS PAGE IS A WORK IN PROGRESS ... Please make edits to this page to improve it!
Try to lock a mutex without blocking.
int SDL_TryLockMutex(SDL_Mutex *mutex) SDL_TRY_ACQUIRE(0, mutex);
mutex | the mutex to try to lock |
Returns 0, SDL_MUTEX_TIMEDOUT
, or -1 on error; call SDL_GetError() for more information.
This works just like SDL_LockMutex(), but if the mutex is not available, this function returns SDL_MUTEX_TIMEDOUT
immediately.
This technique is useful if you need exclusive access to a resource but don't want to wait for it, and will return to it to try again later.
This function is available since SDL 3.0.0.
int status;
SDL_mutex *mutex;
mutex = SDL_CreateMutex();if (!mutex) {
"Couldn't create mutex\n");
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, return;
}
status = SDL_TryLockMutex(mutex);
if (status == 0) {
"Locked mutex\n");
SDL_Log(
SDL_UnlockMutex(mutex);else if (status == SDL_MUTEX_TIMEDOUT) {
} /* Mutex not available for locking right now */
else {
} "Couldn't lock mutex\n");
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
}
SDL_DestroyMutex(mutex);
CategoryAPI, CategoryMutex, CategoryDraft