aboutsummaryrefslogtreecommitdiff
path: root/zenhttp/workthreadpool.h
blob: 834339d50919ec5609c9ad8bfd26024b99a11baf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Copyright Epic Games, Inc. All Rights Reserved.

#pragma once

#include <zencore/zencore.h>

#include <zencore/blockingqueue.h>
#include <zencore/refcount.h>

#include <exception>
#include <functional>
#include <system_error>
#include <thread>
#include <vector>

namespace zen {

struct IWork : public RefCounted
{
	virtual void Execute() = 0;

	inline std::exception_ptr GetException() { return m_Exception; }

private:
	std::exception_ptr m_Exception;

	friend class WorkerThreadPool;
};

class WorkerThreadPool
{
public:
	WorkerThreadPool(int InThreadCount);
	~WorkerThreadPool();

	void ScheduleWork(Ref<IWork> Work);
	void ScheduleWork(std::function<void()>&& Work);

	void WorkerThreadFunction();

private:
	std::vector<std::thread>  m_WorkerThreads;
	BlockingQueue<Ref<IWork>> m_WorkQueue;
};

}  // namespace zen