mutex mit atomic operations ausgetauscht

This commit is contained in:
any
2026-08-29 00:57:23 +02:00
parent 7052745a90
commit 09d2587ef2
2 changed files with 5 additions and 9 deletions
+5 -9
View File
@@ -1,6 +1,7 @@
//
// Created by ary on 29.07.2026.
//
#include <stdatomic.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
@@ -18,7 +19,8 @@
#include <stdlib.h>
#include "helper.h"
#include <pthread.h>
#define WORKTHREADSIZE 32
#include <stdatomic.h>
#define WORKTHREADSIZE 16
void* renderPixel(RenderArgs *arg, int pixelId) {
RenderArgs *args = (RenderArgs*)arg;
@@ -57,11 +59,7 @@ void *workerLoop(void *arg) {
RenderArgs *args = (RenderArgs*)arg;
while (1) {
pthread_mutex_lock(&args->mutexKey);
int pixelId = args->nextPixelId;
args->nextPixelId++;
pthread_mutex_unlock(&args->mutexKey);
int pixelId = atomic_fetch_add(&args->nextPixelId, 1);
if (pixelId >= args->pixelCount) break;
renderPixel(args, pixelId);
@@ -73,9 +71,7 @@ void* monitorProgress(void *arg) {
MonitorArgs *args = (MonitorArgs*)arg;
while (1){
pthread_mutex_lock(args->mutexKey);
int pixelId = *args->nextPixelId;
pthread_mutex_unlock(args->mutexKey);
int pixelId = *atomic_load(&args->nextPixelId);
if (pixelId >= args->pixelCount) break;
double progress = 100.0 * pixelId / args->pixelCount;
printf("progress: %lf%%\n", progress);