mutex mit atomic operations ausgetauscht
This commit is contained in:
+5
-9
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user