SDL_sem* SDL_CreateSemaphore(Uint32 initial_value)
initial_value | セマフォの初期値 |
セマフォの典型的な使い方:
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);