Browse Source

Use explicit atomics (#2849)

* Use explicit atomics

* missed one

* use relaced ordering
pull/2853/head
Antonis Geralis 2 years ago
committed by GitHub
parent
commit
1dbcce8b56
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 8 deletions
  1. +8
    -8
      examples/core/core_loading_thread.c

+ 8
- 8
examples/core/core_loading_thread.c View File

@ -23,10 +23,10 @@
// Using C11 atomics for synchronization // Using C11 atomics for synchronization
// NOTE: A plain bool (or any plain data type for that matter) can't be used for inter-thread synchronization // NOTE: A plain bool (or any plain data type for that matter) can't be used for inter-thread synchronization
static atomic_bool dataLoaded = ">ATOMIC_VAR_INIT(false); // Data Loaded completion indicator
static atomic_bool dataLoaded = false; // Data Loaded completion indicator
static void *LoadDataThread(void *arg); // Loading data thread function declaration static void *LoadDataThread(void *arg); // Loading data thread function declaration
static kt">int dataProgress = 0; // Data progress accumulator
static n">atomic_int dataProgress = 0; // Data progress accumulator
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Program main entry point // Program main entry point
@ -69,7 +69,7 @@ int main(void)
case STATE_LOADING: case STATE_LOADING:
{ {
framesCounter++; framesCounter++;
if (atomic_load(&dataLoaded))
if (atomic_load_explicit(&dataLoaded, memory_order_relaxed))
{ {
framesCounter = 0; framesCounter = 0;
int error = pthread_join(threadId, NULL); int error = pthread_join(threadId, NULL);
@ -84,8 +84,8 @@ int main(void)
if (IsKeyPressed(KEY_ENTER)) if (IsKeyPressed(KEY_ENTER))
{ {
// Reset everything to launch again // Reset everything to launch again
atomic_store(&dataLoaded, false);
dataProgress = 0;
atomic_store_explicit(&dataLoaded, false, memory_order_relaxed);
atomic_store_explicit(&dataProgress, 0, memory_order_relaxed);
state = STATE_WAITING; state = STATE_WAITING;
} }
} break; } break;
@ -104,7 +104,7 @@ int main(void)
case STATE_WAITING: DrawText("PRESS ENTER to START LOADING DATA", 150, 170, 20, DARKGRAY); break; case STATE_WAITING: DrawText("PRESS ENTER to START LOADING DATA", 150, 170, 20, DARKGRAY); break;
case STATE_LOADING: case STATE_LOADING:
{ {
DrawRectangle(150, 200, dataProgress, 60, SKYBLUE);
DrawRectangle(150, 200, atomic_load_explicit(&dataProgress, memory_order_relaxed), 60, SKYBLUE);
if ((framesCounter/15)%2) DrawText("LOADING DATA...", 240, 210, 40, DARKBLUE); if ((framesCounter/15)%2) DrawText("LOADING DATA...", 240, 210, 40, DARKBLUE);
} break; } break;
@ -145,11 +145,11 @@ static void *LoadDataThread(void *arg)
// We accumulate time over a global variable to be used in // We accumulate time over a global variable to be used in
// main thread as a progress bar // main thread as a progress bar
dataProgress = timeCounter/10;
atomic_store_explicit(&dataProgress, timeCounter/10, memory_order_relaxed);
} }
// When data has finished loading, we set global variable // When data has finished loading, we set global variable
atomic_store(&dataLoaded, true);
atomic_store_explicit(&dataLoaded, true, memory_order_relaxed);
return NULL; return NULL;
} }

Loading…
Cancel
Save