Wiki Page Content

Differences between revisions 5 and 6
Revision 5 as of 2010-10-18 02:17:46
Size: 1984
Editor: SheenaSmith
Comment: update content - pointers, structs
Revision 6 as of 2013-08-12 05:57:49
Size: 1177
Editor: Sam Lantinga
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
#pragma disable-camelcase
||<tablewidth="100%" style="color: #FF0000;" :> DRAFT||
#pragma camelcase off
Line 6: Line 5:
Use this function to suspend the calling thread until the semaphore pointed to by '''sem''' has a positive count, then atomically decrease the semaphore count ''-or-'' ^...suspend the calling thread until the semaphore can be atomically decremented^ ''-or-'' *lock a semaphore and suspend the thread if the semaphore value is zero*. Use this function wait until a semaphore has a positive value and then decrements it.
Line 16: Line 15:
||'''sem'''||^the semaphore to monitor^|| ||'''sem'''||the semaphore wait on||
Line 21: Line 20:
If the semaphore was not successfully locked, the semaphore will be unchanged. ???
Line 24: Line 21:
*
{{{#!highlight cpp
if (SDL_SemWait(my_sem) == -1) {
        return WAIT_FAILED;
}

...

SDL_SemPost(my_sem);
}}}
*
<<Include(SDL_CreateSemaphore, , , from="## Begin Semaphore Example", to="## End Semaphore Example")>>
Line 37: Line 24:
*<<BR>>[[SDL_SemWait]]() suspends the calling thread until either the semaphore pointed to by '''sem''' has a positive value or the call is interrupted by a signal or error. If the call is successful it will atomically decrement the semaphore value. This function suspends the calling thread until either the semaphore pointed to by '''sem''' has a positive value or the call is interrupted by a signal or error. If the call is successful it will atomically decrement the semaphore value.
Line 39: Line 26:
After [[SDL_SemWait]]() is successful, the semaphore can be released and its count atomically incremented by a successful call to [[SDL_SemPost]](). <<BR>>*

<<Color2(green,Should the Remarks include any discussion of locking and unlocking for clarity? For example is the lock/unlock pair a built-in feature of this function or does the programmer have to 'manually' lock then unlock the semaphore or thread when using this function? Would it be beneficial to a user to have some discussion of that here or is it just understood/given?)>>
This function is the equivalent of calling [[SDL_SemWaitTimeout]]() with a time length of SDL_MUTEX_MAXWAIT.
Line 44: Line 29:
 .[[SDL_SemPost]] *
 .[[SDL_SemTryWait]] *
 .[[SDL_SemValue]] *
 .[[SDL_SemWaitTimeout]] *
 .[[SDL_CreateSemaphore]]
 .[[SDL_DestroySemaphore]]
.[[SDL_SemPost]]
 .[[SDL_SemTryWait]]
 .[[SDL_SemValue]]
.[[SDL_SemWait]]
 .[[SDL_SemWaitTimeout]]

SDL_SemWait

Use this function wait until a semaphore has a positive value and then decrements it.

Syntax

int SDL_SemWait(SDL_sem* sem)

Function Parameters

sem

the semaphore wait on

Return Value

Returns 0 on success or a negative error code on failure; call SDL_GetError() for more information.

Code Examples

Typical use of semaphores:

SDL_atomic_t done;
SDL_sem *sem;

SDL_AtomicSet(&done, 0);
sem = SDL_CreateSemaphore(0);
.
.
Thread A:
    while (!SDL_AtomicGet(&done)) {
        add_data_to_queue();
        SDL_SemPost(sem);
    }

Thread B:
    while (!SDL_AtomicGet(&done)) {
        SDL_SemWait(sem);
        if (data_available()) {
            get_data_from_queue();
        }
    }
.
.
SDL_AtomicSet(&done, 1);
SDL_SemPost(sem);
wait_for_threads();
SDL_DestroySemaphore(sem);

Remarks

This function suspends the calling thread until either the semaphore pointed to by sem has a positive value or the call is interrupted by a signal or error. If the call is successful it will atomically decrement the semaphore value.

This function is the equivalent of calling SDL_SemWaitTimeout() with a time length of SDL_MUTEX_MAXWAIT.


CategoryAPI, CategoryMutex

None: SDL_SemWait (last edited 2013-10-05 19:37:34 by PhilippWiesemann)

(Page Info.)
Feedback
Please include your contact information if you'd like to receive a reply.
Submit