From 4b150c809700d59b24e4fb1e5278a5e85e1e34b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petar=20Kapri=C5=A1?= Date: Tue, 7 Oct 2025 20:28:34 +0100 Subject: [PATCH] Modify argument to worker thread functions Instead of only supplying the index, the pointer to the threadInfo structure is passed. --- visor.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/visor.c b/visor.c index 798e5b5..647e825 100644 --- a/visor.c +++ b/visor.c @@ -109,11 +109,11 @@ void color_from_iteration(int *r, int *g, int *b, double x0, double y0) #undef MAX_ITERATION } -void draw_mandelbrot(int32_t threadIndex) +void draw_mandelbrot(struct threadInfo *info) { int h = mandelbrot.height; int w = mandelbrot.width; - int p = threadIndex, c = thread_count; + int p = info->index, c = thread_count; int mod = h % c, div = h / c; int a = MIN(p, mod)*(div+1) + (MAX(p, mod)-mod)*div, b = p < mod ? a + div + 1 : a + div; @@ -147,21 +147,22 @@ void draw_mandelbrot(int32_t threadIndex) pixmap[4*(i*w + j) + 0] = b; } } - threads[threadIndex].complete = true; + info->complete = true; } -void *writer_thread(void *index_ptr) +void *writer_thread(void *arg) { - int32_t index = *(int32_t *)index_ptr; + struct threadInfo *info = arg; + int32_t index = info->index; while (true) { pthread_mutex_lock(&pixmapMutex); - while(!pixmapAvailable || threads[index].complete) { + while(!pixmapAvailable || info->complete) { pthread_cond_wait(&pixmapCond, &pixmapMutex); } pthread_mutex_unlock(&pixmapMutex); - threads[index].drawing = true; - draw_mandelbrot(index); - threads[index].drawing = false; + info->drawing = true; + draw_mandelbrot(info); + info->drawing = false; } } @@ -323,7 +324,7 @@ int main(int argc, char **argv) // integer, if the thread fails to start, it will simply be // overwritten by the same value in the next run of the loop pthread_t id; - if (pthread_create(&id, NULL, &writer_thread, &threads[count].index)) { + if (pthread_create(&id, NULL, &writer_thread, &threads[count])) { pthread_detach(id); fprintf(stderr, "Failed to create thread %d\n", i); continue;