<commit_before>#include<chrono>
#include<iostream>
#include<mutex>
#include<random>
#include<thread>
#include"../lib/CThreadPool.h"

namespace
{
	std::mutex mut;

	size_t get()
	{
		using namespace std;
		static mt19937 mu{static_cast<mt19937::result_type>(chrono::high_resolution_clock::now().time_since_epoch().count())};
		return mu()%4+1;
	}

	int add_func(int i)
	{
		using namespace std;
		const auto sec{get()};
		this_thread::sleep_for(chrono::seconds{sec});
		lock_guard<mutex> lock{mut};
		cout<<"add - thread "<<i<<" wait "<<sec<<" sec"<<endl;
		return i;
	}

	int add_and_detach_func(int i)
	{
		using namespace std;
		const auto sec{get()};
		this_thread::sleep_for(chrono::seconds{sec});
		lock_guard<mutex> lock{mut};
		cout<<"add_and_detach - thread "<<i<<" wait "<<sec<<" sec"<<endl;
		return i;
	}
}

int main()
{
	using namespace std;
	nTool::CThreadPool tp{4};

	cout<<"stage 1"<<endl;
	for(size_t i{0};i!=tp.count();++i)
		tp.add_and_detach(add_and_detach_func,i);
	tp.join_all();	//this will not block, because you use add_and_detach

	cout<<"stage 2"<<endl;
	for(size_t i{0};i!=tp.count();++i)
		tp.add(add_func,i);	//tp will block here until add_and_detach_func complete
	for(size_t i{0};i!=tp.count();++i)
		tp.join(i);	//tp will block here until the i of thread complete

	cout<<"stage 3"<<endl;
	for(size_t i{0};i!=tp.count();++i)
		tp.add_and_detach(add_and_detach_func,i);
	tp.wait_until_all_available();	//this will block until all detach threads complete add_and_detach_func

	cout<<"stage 4"<<endl;
	for(size_t i{0};i!=tp.count();++i)
		tp.add(add_func,i);	//tp will not block here, because you join all thread
	tp.join_all();	//tp will block here until add_func complete, it is same as
					//for(size_t i(0);i!=tp.count();++i)
					//	tp.join(i);

	cout<<"stage 5"<<endl;
	for(size_t i{0};i!=tp.count();++i)
		tp.add(add_func,i);
	cout<<"thread "<<tp.join_any()<<" complete"<<endl;	//join any thread without specify which one
	tp.join_all();<commit_msg>1. change include directive path 2. change return type and argument of add_func and add_and_detach_func to std::size_t 3. change argument of add_func and add_and_detach_func to const<commit_after>#include<chrono>
#include<iostream>
#include<mutex>
#include<random>
#include<thread>
#include"../header/CThreadPool.h"

namespace
{
	std::mutex mut;

	size_t get()
	{
		using namespace std;
		static mt19937 mu{static_cast<mt19937::result_type>(chrono::high_resolution_clock::now().time_since_epoch().count())};
		return mu()%4+1;
	}

	std::size_t add_func(const std::size_t i)
	{
		using namespace std;
		const auto sec{get()};
		this_thread::sleep_for(chrono::seconds{sec});
		lock_guard<mutex> lock{mut};
		cout<<"add - thread "<<i<<" wait "<<sec<<" sec"<<endl;
		return i;
	}

	std::size_t add_and_detach_func(const std::size_t i)
	{
		using namespace std;
		const auto sec{get()};
		this_thread::sleep_for(chrono::seconds{sec});
		lock_guard<mutex> lock{mut};
		cout<<"add_and_detach - thread "<<i<<" wait "<<sec<<" sec"<<endl;
		return i;
	}
}

int main()
{
	using namespace std;
	nThread::CThreadPool tp{4};

	cout<<"stage 1"<<endl;
	for(size_t i{0};i!=tp.count();++i)
		tp.add_and_detach(add_and_detach_func,i);
	tp.join_all();	//this will not block, because you use add_and_detach

	cout<<"stage 2"<<endl;
	for(size_t i{0};i!=tp.count();++i)
		tp.add(add_func,i);	//tp will block here until add_and_detach_func complete
	for(size_t i{0};i!=tp.count();++i)
		tp.join(i);	//tp will block here until the i of thread complete

	cout<<"stage 3"<<endl;
	for(size_t i{0};i!=tp.count();++i)
		tp.add_and_detach(add_and_detach_func,i);
	tp.wait_until_all_available();	//this will block until all detach threads complete add_and_detach_func

	cout<<"stage 4"<<endl;
	for(size_t i{0};i!=tp.count();++i)
		tp.add(add_func,i);	//tp will not block here, because you join all thread
	tp.join_all();	//tp will block here until add_func complete, it is same as
					//for(size_t i(0);i!=tp.count();++i)
					//	tp.join(i);

	cout<<"stage 5"<<endl;
	for(size_t i{0};i!=tp.count();++i)
		tp.add(add_func,i);
	cout<<"thread "<<tp.join_any()<<" complete"<<endl;	//join any thread without specify which one
	tp.join_all();
{'fraction_non_alphanumeric': 0.18105916030534353, 'fraction_numerical': 0.012643129770992367, 'mean_word_length': 5.382039573820395, 'pattern_counts': {'":': 0, '<': 123, '<?xml version=': 0, '>': 19, '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': '3059229', 'n_tokens_mistral': 1770, 'n_tokens_neox': 1672, 'n_words': 358}