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