diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-08-25 18:59:03 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2023-08-25 18:59:03 +0100 |
commit | 62647556fadb5407a8b144c5b3067772288b458f (patch) | |
tree | 26f9f16ab79053cff1078d973a98ada510650ecc /main.c | |
parent | 985a883fdaf01771fff30f23ba8c19f1f1a997df (diff) | |
download | abelian-sandpile-62647556fadb5407a8b144c5b3067772288b458f.tar.gz abelian-sandpile-62647556fadb5407a8b144c5b3067772288b458f.tar.bz2 abelian-sandpile-62647556fadb5407a8b144c5b3067772288b458f.zip |
(main)+threading for steps
Doing this experiment with threads shows a strong improvement in
render speed by separating "steps" from the direct render.
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 31 |
1 files changed, 25 insertions, 6 deletions
@@ -4,8 +4,12 @@ * Description: Entry point of program */ #include <malloc.h> +#include <stdbool.h> #include <stdio.h> +#include <pthread.h> +#include <unistd.h> + #include <raylib.h> #include <raymath.h> @@ -17,6 +21,8 @@ struct State size_t window_len; int multiplier; + + bool thread_alive; }; typedef struct State state_t; @@ -44,22 +50,32 @@ void step(state_t *state) } } +void *compute_thread(void *input) +{ + state_t *state = input; + while (state->thread_alive) + step(state); + return NULL; +} + int main(void) { - state_t state = {NULL, 256, 512, 0}; + state_t state = {NULL, 512, 512, 0, true}; state.data = calloc(state.dwidth * state.dwidth, sizeof(*state.data)); state.multiplier = state.window_len / state.dwidth; - Camera2D camera = {0}; - camera.zoom = 1.0f; + const float zoom = 0.125f; + Camera2D camera = {0}; + camera.zoom = 1.0f; + + pthread_t step_thread; + pthread_create(&step_thread, NULL, &compute_thread, &state); + InitWindow(state.window_len, state.window_len, "Abelian sand pile"); SetTargetFPS(60); - const float zoom = 0.125f; while (!WindowShouldClose()) { - step(&state); - if (IsKeyPressed(KEY_UP) || IsKeyDown(KEY_UP)) { Vector2 centre = {state.window_len / 2, state.window_len / 2}; @@ -110,6 +126,9 @@ int main(void) EndMode2D(); EndDrawing(); } + + if (state.thread_alive) + pthread_cancel(step_thread); CloseWindow(); return 0; } |