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. // Created by ary on 29.07.2026.
// //
#include <stdatomic.h>
#include <stdio.h> #include <stdio.h>
#ifdef _WIN32 #ifdef _WIN32
#include <windows.h> #include <windows.h>
@@ -18,7 +19,8 @@
#include <stdlib.h> #include <stdlib.h>
#include "helper.h" #include "helper.h"
#include <pthread.h> #include <pthread.h>
#define WORKTHREADSIZE 32 #include <stdatomic.h>
#define WORKTHREADSIZE 16
void* renderPixel(RenderArgs *arg, int pixelId) { void* renderPixel(RenderArgs *arg, int pixelId) {
RenderArgs *args = (RenderArgs*)arg; RenderArgs *args = (RenderArgs*)arg;
@@ -57,11 +59,7 @@ void *workerLoop(void *arg) {
RenderArgs *args = (RenderArgs*)arg; RenderArgs *args = (RenderArgs*)arg;
while (1) { while (1) {
pthread_mutex_lock(&args->mutexKey); int pixelId = atomic_fetch_add(&args->nextPixelId, 1);
int pixelId = args->nextPixelId;
args->nextPixelId++;
pthread_mutex_unlock(&args->mutexKey);
if (pixelId >= args->pixelCount) break; if (pixelId >= args->pixelCount) break;
renderPixel(args, pixelId); renderPixel(args, pixelId);
@@ -73,9 +71,7 @@ void* monitorProgress(void *arg) {
MonitorArgs *args = (MonitorArgs*)arg; MonitorArgs *args = (MonitorArgs*)arg;
while (1){ while (1){
pthread_mutex_lock(args->mutexKey); int pixelId = *atomic_load(&args->nextPixelId);
int pixelId = *args->nextPixelId;
pthread_mutex_unlock(args->mutexKey);
if (pixelId >= args->pixelCount) break; if (pixelId >= args->pixelCount) break;
double progress = 100.0 * pixelId / args->pixelCount; double progress = 100.0 * pixelId / args->pixelCount;
printf("progress: %lf%%\n", progress); printf("progress: %lf%%\n", progress);
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.4 MiB

After

Width:  |  Height:  |  Size: 1.4 MiB