<commit_before>#pragma once

#include <chrono>
#include <functional>
#include <thread>
#include <atomic>

class Tick
{
public:
	Tick(std::chrono::milliseconds timeBetweenTicks,
		 std::function<void()>&& onTick)
		: timeBetweenTicks_(timeBetweenTicks)
		, onTick_(onTick)
		, active_(true)
		, workerThread_([this] { Loop(); })	// note initialization order is very important here; 
											// thread should start last
	{}

	void Deactivate() { active_ = false; workerThread_.join(); }

	bool IsActive() const { return active_; }

private:
	void Loop()
	{
		while (active_)
		{
			for (auto start = std::chrono::steady_clock::now(), now = start;
				 now < start + timeBetweenTicks_;
				 now = std::chrono::steady_clock::now());
			onTick_();	// may take significant time!
		}
	}

	std::function<void()> onTick_;
	std::chrono::milliseconds timeBetweenTicks_;
	std::atomic<bool> active_;
	std::thread workerThread_;
};
<commit_msg>Tick/Timer - (cosmetic) accept microseconds instead of milliseconds in ctor<commit_after>#pragma once

#include <chrono>
#include <functional>
#include <thread>
#include <atomic>

class Tick
{
public:
	Tick(std::chrono::microseconds timeBetweenTicks,
		 std::function<void()>&& onTick)
		: timeBetweenTicks_(timeBetweenTicks)
		, onTick_(onTick)
		, active_(true)
		, workerThread_([this] { Loop(); })	// note initialization order is very important here; 
											// thread should start last
	{}

	void Deactivate() { active_ = false; workerThread_.join(); }

	bool IsActive() const { return active_; }

private:
	void Loop()
	{
		while (active_)
		{
			for (auto start = std::chrono::steady_clock::now(), now = start;
				 now < start + timeBetweenTicks_;
				 now = std::chrono::steady_clock::now());
			onTick_();	// may take significant time!
		}
	}

	std::function<void()> onTick_;
	std::chrono::milliseconds timeBetweenTicks_;
	std::atomic<bool> active_;
	std::thread workerThread_;
};

{'fraction_non_alphanumeric': 0.14522821576763487, 'fraction_numerical': 0.0, 'mean_word_length': 4.57514450867052, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2776876', 'n_tokens_mistral': 709, 'n_tokens_neox': 625, 'n_words': 152}