Можно ли сделать лучше?
class Block
{
public:
Block(unsigned int delay)
{
_Delay = delay;
_NextTick = std::time(nullptr);
}
void Run(std::shared_ptr<std::mutex> &mutex)
{
_Thread = std::thread([=] { RunTime(std::ref(*mutex.get())); });
}
void RunTime(std::mutex &mutex)
{
while (true)
{
if (std::time(nullptr) >= _NextTick)
{
mutex.lock();
Echo();
mutex.unlock();
_NextTick = std::time(nullptr) + _Delay;
}
}
}
std::thread &GetThread() { return _Thread; }
// return code
virtual unsigned int Echo() = 0;
private:
std::time_t _NextTick;
std::thread _Thread;
unsigned int _Delay;
};
class Logger : public Block
{
public:
Logger(unsigned int delay)
: Block(delay)
{
}
unsigned int Echo()
{
std::cout << "Tick Logger" << std::endl;
return 0;
}
};
class Core
{
public:
Core()
{
for (int a = 0; a < 500; a++)
{
_Blocks.push_back(new Logger(0));
}
}
void RunAllBlock()
{
auto mutex = std::make_shared<std::mutex>();
for (auto block : _Blocks)
{
block->Run(mutex);
}
}
~Core()
{
for (auto block : _Blocks)
{
block->GetThread().join();
}
}
private:
std::vector<Block*> _Blocks;
};
void main()
{
Core c;
c.RunAllBlock();
}
Продвижение своими сайтами как стратегия роста и независимости