Wiki Page Content

Differences between revisions 3 and 7 (spanning 4 versions)
Revision 3 as of 2010-09-08 23:31:40
Size: 1443
Editor: SheenaSmith
Comment: update content (old wiki)
Revision 7 as of 2013-08-12 05:48:11
Size: 852
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 *unlock a semaphore and* atomically increase the semaphore's count ,,(not blocking),,.

<<Color2(g
reen,See Remarks.)>>
Use this function to atomically increment a semaphore's value and wake waiting threads.
Line 18: Line 15:
||'''sem'''||^a pointer to the SDL_sem structure / semaphore to be incremented^|| ||'''sem'''||the semaphore to increment||
Line 21: Line 18:
Returns 0 on success ^(and increments the semaphore count)^ or a negative error code on failure *(leaving the semaphore unchanged)*; call [[SDL_GetError]]() for more information. Returns 0 on success or a negative error code on failure ; call [[SDL_GetError]]() for more information.
Line 24: Line 21:
{{{#!highlight cpp
SDL_SemPost(my_sem);
}}}
<<Include(SDL_CreateSemaphore, , , from="## Begin Semaphore Example", to="## End Semaphore Example")>>
Line 29: Line 24:
*<<BR>>[[SDL_SemPost]]() unlocks the semaphore pointed to by '''sem''' and atomically increments the semaphore's value. Threads that were blocking on the semaphore may be scheduled after this call succeeds.

[[SDL_SemPost]]() should be called after a semaphore is locked by a successful call to [[SDL_SemWait]](), [[SDL_SemTryWait]]() or [[SDL_SemWaitTimeout]](). <<BR>>*

<<Color2(green,Should the (not blocking) addendum from the header description be moved down here and explained more fully?)>>
''You can add useful comments here''
Line 36: Line 27:
 .[[SDL_SemTryWait]] *
 .[[SDL_SemValue]] *
 .[[SDL_SemWait]] *
 .[[SDL_SemWaitTimeout]] *
 .[[SDL_CreateSemaphore]]
 .[[SDL_DestroySemaphore]]
.[[SDL_SemTryWait]]
 .[[SDL_SemValue]]
 .[[SDL_SemWait]]
 .[[SDL_SemWaitTimeout]]

SDL_SemPost

Use this function to atomically increment a semaphore's value and wake waiting threads.

Syntax

int SDL_SemPost(SDL_sem* sem)

Function Parameters

sem

the semaphore to increment

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

You can add useful comments here


CategoryAPI, CategoryMutex

None: SDL_SemPost (last edited 2013-08-12 05:48:11 by Sam Lantinga)

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