Modify argument to worker thread functions

Instead of only supplying the index, the pointer to the threadInfo
structure is passed.
This commit is contained in:
Petar Kapriš 2025-10-07 20:28:34 +01:00
parent ba685cc0ca
commit 4b150c8097

21
visor.c
View file

@ -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;