mirror of
https://github.com/libsdl-org/SDL.git
synced 2025-10-26 12:27:44 +00:00
Use atomic variables for thread communication
Fixes https://github.com/libsdl-org/SDL/issues/10711
This commit is contained in:
@@ -37,7 +37,8 @@ quit(int rc)
|
||||
static int SDLCALL
|
||||
SubThreadFunc(void *data)
|
||||
{
|
||||
while (!*(int volatile *)data) {
|
||||
SDL_AtomicInt *flag = (SDL_AtomicInt *)data;
|
||||
while (!SDL_AtomicGet(flag)) {
|
||||
SDL_Delay(10);
|
||||
}
|
||||
return 0;
|
||||
@@ -47,7 +48,7 @@ static int SDLCALL
|
||||
ThreadFunc(void *data)
|
||||
{
|
||||
SDL_Thread *sub_threads[NUMTHREADS];
|
||||
int flags[NUMTHREADS];
|
||||
SDL_AtomicInt flags[NUMTHREADS];
|
||||
int i;
|
||||
int tid = (int)(uintptr_t)data;
|
||||
|
||||
@@ -56,7 +57,7 @@ ThreadFunc(void *data)
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
char name[64];
|
||||
(void)SDL_snprintf(name, sizeof(name), "Child%d_%d", tid, i);
|
||||
flags[i] = 0;
|
||||
SDL_AtomicSet(&flags[i], 0);
|
||||
sub_threads[i] = SDL_CreateThread(SubThreadFunc, name, &flags[i]);
|
||||
}
|
||||
|
||||
@@ -67,7 +68,7 @@ ThreadFunc(void *data)
|
||||
|
||||
SDL_Log("Thread '%d' sending signals to subthreads\n", tid);
|
||||
for (i = 0; i < NUMTHREADS; i++) {
|
||||
flags[i] = 1;
|
||||
SDL_AtomicSet(&flags[i], 1);
|
||||
SDL_WaitThread(sub_threads[i], NULL);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user