Browse Source

thread pool

testing
totallyfake 2 years ago
parent
commit
fa8fc0d39d
6 changed files with 115 additions and 12 deletions
  1. 39
    0
      include/threadpool.h
  2. 2
    2
      res/config.conf
  3. 3
    7
      src/main.cpp
  4. 17
    1
      src/scenes/testscene.cpp
  5. 52
    0
      src/threadpool.cpp
  6. 2
    2
      src/world.cpp

+ 39
- 0
include/threadpool.h View File

@@ -0,0 +1,39 @@
/*

Thread pool

*/

#pragma once

#include <thread>
#include <vector>
#include <queue>
#include <functional>
#include <mutex>
#include <condition_variable>

namespace vtk {

class ThreadPool {
private:
ThreadPool();
ThreadPool(ThreadPool const& copy);
ThreadPool& operator=(ThreadPool const& copy);

public:
static ThreadPool& getInstance();

void addThreads(const int& count); // add new threads to the pool
void addJob(std::function<void()> newJob); // add a job to the pool

private:
void threadIdle();

std::vector<std::thread> mThreads;
std::queue<std::function<void()> > mQueue;
std::mutex mQueueMutex;
std::condition_variable mCondition;
};

}

+ 2
- 2
res/config.conf View File

@@ -2,8 +2,8 @@

graphics {
res {
x = 1920; horizontal resolution
y = 1080; vertical resolution
x = 800; horizontal resolution
y = 600; vertical resolution
}
fov = 90
vsync = 0

+ 3
- 7
src/main.cpp View File

@@ -4,6 +4,7 @@
#include "mathplus.h"
#include "spdlog/spdlog.h"
#include "loggersetup.h"
#include "threadpool.h"

#include <iostream>
#include <vector>
@@ -12,18 +13,13 @@

int main (int argc, char *argv[])
{
//testing
unsigned uTopModel [] = { 0,0,0,0,0,0 };
uTopModel[0] = (uTopModel[0] << 4) | unsigned(8);
uTopModel[0] = (uTopModel[0] << 4) | unsigned(6);
std::cout << "Extracted: " << unsigned((uTopModel[0] >> 4)&((1<<4)-1)) <<
", " << unsigned((uTopModel[0] >> 0)&((1<<4)-1));

vtk::LoggerSetup lSetup;
lSetup.setup();
spdlog::get("general")->info("Starting Voxeltronik Engine...");
spdlog::get("general");

vtk::ThreadPool::getInstance().addThreads(2);

auto conf = new Config;
conf->loadConfigFromFile("res/config.conf");
conf->addArgumentRule("-w", "graphics.res.x");

+ 17
- 1
src/scenes/testscene.cpp View File

@@ -51,7 +51,7 @@ void TestScene::init() {

glUseProgram(shaders);

camera.setPosition(glm::vec3(0.0f, 5.0f, 5.0f));
camera.setPosition(glm::vec3(0.0f, 64.0f, 0.0f));
camera.setAspectRatio(linkedGame->window.getAspect());

viewMatUni = glGetUniformLocation(shaders, "view");
@@ -109,6 +109,7 @@ void TestScene::init() {

std::cout << std::endl;

/*
int chunkCount = 1;
for (int i = 0; i < 32; i++) {
for (int j = 0; j < 8; j++) {
@@ -119,6 +120,7 @@ void TestScene::init() {
}
}
}
*/

//world.forceGlobalGeometryUpdate();

@@ -168,6 +170,20 @@ void TestScene::update(const float& dTime) {
}

//camera.moveRelative(camMovement * dTime);

//generate chunks with camera (doesn't work very well :|)
int radius = 2;
auto cPos = camera.getPosition();
glm::ivec3 cPosI = (cPos / 16.0f);
for (int y = -radius; y <= radius; ++y) {
for (int x = -radius; x <= radius; ++x) {
if(x*x + y*y <= radius*radius) {
for (int i = 0; i < 8; ++i)
world.generateChunk(cPosI.x + x, i, cPosI.z + y);
}
}
}
}

void TestScene::draw() {

+ 52
- 0
src/threadpool.cpp View File

@@ -0,0 +1,52 @@
#include "threadpool.h"

namespace vtk {

ThreadPool::ThreadPool() {
}

ThreadPool::ThreadPool(ThreadPool const& copy) {

}

ThreadPool& ThreadPool::operator=(ThreadPool const& copy) {
return *this;
}

ThreadPool& ThreadPool::getInstance() {
static ThreadPool instance;
return instance;
}

void ThreadPool::addThreads(const int &count) {
for (int i = 0; i < count; ++i) {
mThreads.push_back(std::thread(&ThreadPool::threadIdle, this));
}
}

void ThreadPool::threadIdle() {

// Idle until something appears in the job queue, then run it
std::function<void()> job;
while(true) {
{
std::unique_lock<std::mutex> lock(mQueueMutex);
mCondition.wait(lock, [&]{return !mQueue.empty();});

job = mQueue.front();
mQueue.pop();
}
job();
}
}

void ThreadPool::addJob(std::function<void ()> newJob) {
{
std::unique_lock<std::mutex> lock(mQueueMutex);
mQueue.push(newJob);
}
mCondition.notify_one();
}
}

+ 2
- 2
src/world.cpp View File

@@ -1,5 +1,6 @@
#include "world.h"
#include "chunk.h"
#include "threadpool.h"

#include <cmath>
#include <iostream>
@@ -154,8 +155,7 @@ void World::update() {
};

rebuildThreadActive = true;
std::thread(updatefunc).detach();

ThreadPool::getInstance().addJob(updatefunc);
}
}


Loading…
Cancel
Save