ⓟrogramming/C++

[Thread] Working with threads

뚱땡이 우주인 2018. 1. 4. 08:48
#include 
#include 
#include 
#include  

#pragma warning(disable : 4996) //_CRT_SECURE_NO_WARNINGS

inline void print_time()
{
	auto now = std::chrono::system_clock::now();
	auto stime = std::chrono::system_clock::to_time_t(now);
	auto ltime = std::localtime(&stime);

	std::cout << std::put_time(ltime, "%c") << std::endl;
}

void func4()
{
	using namespace std::chrono;
	print_time();
	std::this_thread::sleep_for(2s);
	print_time();
}

void func5()
{
	using namespace std::chrono;
	print_time();
	std::this_thread::sleep_until(
		std::chrono::system_clock::now() + 2s);
	print_time();
}


void func6(std::chrono::seconds timeout)
{
	auto now = std::chrono::system_clock::now();
	auto then = now + timeout;
	do
	{
		std::this_thread::yield();
	} while (std::chrono::system_clock::now() < then);
}

int main()
{
	//std::thread t(func4);
	//t.join();

	//std::thread t5(func5);
	//t5.join();

	std::thread t6(func6, std::chrono::seconds(2));
	t6.join();
	print_time();
	return 0;
}