CodePorting.Translator Cs2Cpp
CodePorting.Translator.Cs2Cpp.Framework
thread_pool.h
1
2#ifndef _aspose_system_threading_thread_pool_h_
3#define _aspose_system_threading_thread_pool_h_
4
5#include <queue>
6#include <mutex>
7#include <condition_variable>
8#include <system/object.h>
9#include <system/multicast_delegate.h>
10#include <system/shared_ptr.h>
11
12namespace System { namespace Threading {
13
15using WaitCallback = System::MulticastDelegate<void(System::SharedPtr<System::Object>)>;
16
17class ThreadPool;
18
22class ASPOSECPP_SHARED_CLASS ThreadPoolImpl
23{
24public:
29 ASPOSECPP_SHARED_API bool QueueUserWorkItem(WaitCallback callback, const System::SharedPtr<System::Object>& state);
30
34 ASPOSECPP_SHARED_API void GetAvailableThreads(int &wt, int &cpt);
38 ASPOSECPP_SHARED_API void GetMaxThreads(int &wt, int &cpt);
42 ASPOSECPP_SHARED_API void GetMinThreads(int &wt, int &cpt);
47 ASPOSECPP_SHARED_API bool SetMaxThreads(int wt, int cpt);
52 ASPOSECPP_SHARED_API bool SetMinThreads(int wt, int cpt);
53
55 ASPOSECPP_SHARED_API ThreadPoolImpl();
57 ASPOSECPP_SHARED_API ~ThreadPoolImpl();
58
61 static ASPOSECPP_SHARED_API bool& GetInitialized();
63 ASPOSECPP_SHARED_API void JoinAll();
64
65protected:
67 std::uint32_t m_max_threads;
69 std::uint32_t m_min_threads;
71 std::mutex m_mutex;
73 std::condition_variable m_cv;
75 typedef std::pair<WaitCallback, System::SharedPtr<System::Object>> Job;
77 std::queue<Job> m_queue;
78
80 ASPOSECPP_SHARED_API void Worker();
83 ASPOSECPP_SHARED_API void AddWorkers(uint32_t amount);
86 ASPOSECPP_SHARED_API void DelWorkers(uint32_t amount);
87};
88
89
155{
157 RTTI_INFO(System::Threading::ThreadPool, ::System::BaseTypesInfo<System::Object>);
158public:
161 static ASPOSECPP_SHARED_API ThreadPoolImpl& GetInstance();
162
164 ThreadPool(const ThreadPool&) = delete;
166 void operator = (const ThreadPool&) = delete;
167
171 static ASPOSECPP_SHARED_API bool QueueUserWorkItem(WaitCallback callback);
176 static ASPOSECPP_SHARED_API bool QueueUserWorkItem(WaitCallback callback, const System::SharedPtr<System::Object>& state);
177
181 static ASPOSECPP_SHARED_API void GetAvailableThreads(int &wt, int &cpt);
185 static ASPOSECPP_SHARED_API void GetMaxThreads(int &wt, int &cpt);
189 static ASPOSECPP_SHARED_API void GetMinThreads(int &wt, int &cpt);
190
195 static ASPOSECPP_SHARED_API bool SetMaxThreads(int wt, int cpt);
200 static ASPOSECPP_SHARED_API bool SetMinThreads(int wt, int cpt);
201
203 static ASPOSECPP_SHARED_API void JoinAllThreads();
204};
205
206}} // System::Threading
207
208#endif
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:62
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
Thread pool API allowing it pushing jobs into queue to be read by pool of worker threads....
Definition: thread_pool.h:155
static ThreadPoolImpl & GetInstance()
Implementation instance that holds list of jobs and other parameters.
static void GetAvailableThreads(int &wt, int &cpt)
Gets number of available threads.
static void JoinAllThreads()
Joins all owned threads. Waits infinitely.
static void GetMinThreads(int &wt, int &cpt)
Gets minimal number of threads being created by pool.
static bool QueueUserWorkItem(WaitCallback callback)
Puts work item into queue which is present with callback with no parameter.
ThreadPool(const ThreadPool &)=delete
No copying.
void operator=(const ThreadPool &)=delete
No copying.
static bool SetMinThreads(int wt, int cpt)
Sets minimal number of threads owned by pool.
static bool QueueUserWorkItem(WaitCallback callback, const System::SharedPtr< System::Object > &state)
Puts work item into queue which is present with callback with no parameter.
static bool SetMaxThreads(int wt, int cpt)
Sets number of threads owned by pool.
static void GetMaxThreads(int &wt, int &cpt)
Gets maximal number of concurrent threads.
Thread pool internal data. This is a singleton type with memory management done by access function(s)...
Definition: thread_pool.h:23
void GetMaxThreads(int &wt, int &cpt)
Gets maximal number of concurrent threads.
std::uint32_t m_min_threads
Minimal allowed number of threads to be created on request.
Definition: thread_pool.h:69
void GetMinThreads(int &wt, int &cpt)
Gets minimal number of threads being created by pool.
std::uint32_t m_max_threads
Maximal allowed number of threads to exist simultaneously in the pool.
Definition: thread_pool.h:67
std::pair< WaitCallback, System::SharedPtr< System::Object > > Job
Structure that keeps job callback and parameter to be executed.
Definition: thread_pool.h:75
std::mutex m_mutex
Mutex that blocks thread pool task queue.
Definition: thread_pool.h:71
std::condition_variable m_cv
Condition variable used to keep executor threads waiting.
Definition: thread_pool.h:73
static bool & GetInitialized()
Gets initialization state singleton.
void JoinAll()
Joins all owned threads. Waits infinitely.
std::queue< Job > m_queue
Jobs queue.
Definition: thread_pool.h:77
void AddWorkers(uint32_t amount)
Adds worker threads to the pool.
~ThreadPoolImpl()
Destructor. Joins all threads if they were not terminated yet.
bool SetMaxThreads(int wt, int cpt)
Sets number of threads owned by pool.
bool SetMinThreads(int wt, int cpt)
Sets minimal number of threads owned by pool.
void Worker()
Worker thread function.
void DelWorkers(uint32_t amount)
Deletes worker threads from the pool.
bool QueueUserWorkItem(WaitCallback callback, const System::SharedPtr< System::Object > &state)
Adds work item to queue.
void GetAvailableThreads(int &wt, int &cpt)
Gets number of available threads.
System::MulticastDelegate< void(System::SharedPtr< System::Object >)> WaitCallback
Callback item to be executed once there is a spot.
Definition: thread_pool.h:15
Definition: db_command.h:9