Automatic number of loaders selecting

This commit is contained in:
MihailRis 2022-03-06 03:24:05 +03:00 committed by GitHub
parent cbfa073550
commit 551d4cac85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 6 deletions

View File

@ -8,18 +8,20 @@
#include "../files/WorldFiles.h" #include "../files/WorldFiles.h"
#include "ChunksLoader.h" #include "ChunksLoader.h"
#include <iostream> #include <iostream>
#include <thread>
#define LOADERS_COUNT 3
ChunksController::ChunksController(Chunks* chunks, Lighting* lighting) : chunks(chunks), lighting(lighting){ ChunksController::ChunksController(Chunks* chunks, Lighting* lighting) : chunks(chunks), lighting(lighting){
loaders = new ChunksLoader*[LOADERS_COUNT]; loadersCount = std::thread::hardware_concurrency() - 1;
for (int i = 0; i < LOADERS_COUNT; i++){ loaders = new ChunksLoader*[loadersCount];
for (int i = 0; i < loadersCount; i++){
loaders[i] = new ChunksLoader(); loaders[i] = new ChunksLoader();
} }
std::cout << "created " << loadersCount << " loaders" << std::endl;
} }
ChunksController::~ChunksController(){ ChunksController::~ChunksController(){
for (int i = 0; i < LOADERS_COUNT; i++) for (int i = 0; i < loadersCount; i++)
delete loaders[i]; delete loaders[i];
delete[] loaders; delete[] loaders;
} }
@ -29,7 +31,7 @@ int ChunksController::countFreeLoaders(){
return loaders[0]->isBusy() ? 0 : 1; return loaders[0]->isBusy() ? 0 : 1;
int count = 0; int count = 0;
for (int i = 0; i < LOADERS_COUNT; i++){ for (int i = 0; i < loadersCount; i++){
if (!loaders[i]->isBusy()) if (!loaders[i]->isBusy())
count++; count++;
} }
@ -74,7 +76,7 @@ bool ChunksController::loadVisible(WorldFiles* worldFiles){
return false; return false;
ChunksLoader* freeLoader = nullptr; ChunksLoader* freeLoader = nullptr;
for (int i = 0; i < LOADERS_COUNT; i++){ for (int i = 0; i < loadersCount; i++){
ChunksLoader* loader = loaders[i]; ChunksLoader* loader = loaders[i];
if (loader->isBusy()){ if (loader->isBusy()){
continue; continue;

View File

@ -12,6 +12,7 @@ private:
Chunks* chunks; Chunks* chunks;
Lighting* lighting; Lighting* lighting;
ChunksLoader** loaders; ChunksLoader** loaders;
int loadersCount;
int _totalLoaded = 0; int _totalLoaded = 0;
public: public:
ChunksController(Chunks* chunks, Lighting* lighting); ChunksController(Chunks* chunks, Lighting* lighting);