📄 gamultithreadingalgorithm.h
字号:
#ifndef __GA_MULTITHREADING_ALGORITHM_H__
#define __GA_MULTITHREADING_ALGORITHM_H__
#include "..\CallConvention.h"
#include "GaAlgorithm.h"
namespace Algorithm
{
// Prameters for multithreading genetic algorithm
class GaMultithreadingAlgorithmParams : public GaAlgorithmParams
{
private:
// Number of workers thread
int _numberOfWorkers;
public:
// Initialize parameters
DLL_EXPORT
GaMultithreadingAlgorithmParams(int numberOfWorkers);
// Initialize parameters with default values
DLL_EXPORT
GaMultithreadingAlgorithmParams();
// Virtual copy constructor
DLL_EXPORT
virtual GaParameters* GACALL Clone() const;
// Returns number of workers thread
DLL_EXPORT
int GACALL GetNumberOfWorkers() const;
// Sets number of workers thread
DLL_EXPORT
void GACALL SetNumberOfWorkers(int workers);
};// END CLASS DEFINITION GaMultithreadingAlgorithmParams
// Base class for mutlithreaded algorithms
class GaMultithreadingAlgorithm : public GaBaseAlgorithm
{
protected:
// Synchronization of worker's thread
SysSemaphoreObject _workerSync[2];
// If set to true indicates that parameter change is in progress
bool _parametersChange;
// Number of threads
int _numberOfThreads;
// ID counter for worker threads
volatile int _workerIdCounter;
// Control thread and workers
GaThread** _threads;
public:
// Initialize algorithm
DLL_EXPORT
GaMultithreadingAlgorithm(const GaMultithreadingAlgorithmParams& parameters);
// Frees resources
DLL_EXPORT
virtual ~GaMultithreadingAlgorithm();
// Sets new parameters for algorithm
DLL_EXPORT
virtual void GACALL SetAlgorithmParameters(const GaAlgorithmParams& parameters);
protected:
// Step of control flow before workers start
DLL_EXPORT
virtual void GACALL BeforeWorkers();
// One step of work flow
DLL_EXPORT
virtual void GACALL WorkStep(int workerId);
// Step of control flow after workers finish
DLL_EXPORT
virtual void GACALL AfterWorkers();
// Called when user starts the algorithm
DLL_EXPORT
virtual bool GACALL OnStart();
// Called when user stops the algorithm
DLL_EXPORT
virtual bool GACALL OnStop();
// Called when user pauses the algorithm
DLL_EXPORT
virtual bool GACALL OnPause();
// Called when user resumes the paused algorithm
DLL_EXPORT
virtual bool GACALL OnResume();
// Control flow of evolution
DLL_EXPORT
virtual void GACALL ControlFlow();
// Working flow of evolution
DLL_EXPORT
virtual void GACALL WorkFlow();
// Starts control and working threads.
// Returns TRUE if all threads are started successafully.
DLL_EXPORT
virtual bool GACALL StartThreads();
// Waits for threads to finish
DLL_EXPORT
virtual bool GACALL WaitForThreads();
private:
// Wraper method of control flow method for thread starting
static ThreadFunctionReturn GACALL ControlFlowWrapper(GaThread* thread,
void* params);
// Wraper method of work flow method for thread starting
static ThreadFunctionReturn GACALL WorkFlowWrapper(GaThread* thread,
void* params);
};// END CLASS DEFINITION GaMultithreadingAlgorithm
} // Algorithm
#endif // __GA_MULTITHREADING_ALGORITHM_H__
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -