336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.
#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;
}
블로그 이미지

뚱땡이 우주인

,
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


For the example in the preceding section, we assumed that multiple threads can throw exceptions and therefore need a container to hold them all. If there is a single exception from a single thread at a time, then you do not need a shared container and a mutex to synchronize access to it. You can use a single global object of the type std::exception_ptr to hold the exception transported between threads.

The std::current_exception() is a function that is typically used in a catch clause to capture the current exception and create an instance of std::exception_ptr. This is done to hold a copy or reference (depending on the implementation) to the original exception, which remains valid as long as there is an std::exception_ptr pointer available that refers to it. If this function is called when no exception is being handled, then it creates an empty std::exception_ptr.

The std::exception_ptr pointer is a wrapper for an exception captured with std::current_exception(). If default constructed, it does not hold any exception. Two objects of this type are equal if they are both empty or point to the same exception object. The std::exception_ptr objects can be passed to other threads where they can be rethrown and caught in a try...catch block.

The std::rethrow_exception() is a function that takes std::exception_ptr as an argument and throws the exception object referred to by its argument.

쓰레드에서 exception 발생치 처리하기 위한 코드로 2개 이상의 thread에서 exception이 발생시 vector에 exception 객체를 담아서 관리할 필요가 있다.

catch 구문에서 lock_guard를 통해서 두개의 thread에서 동시에 exception 처리를 위한 mutex 설정이 필요하며 단일 thread의 경우에는 굳이 mutex 및 전역 변수에 vector로 exception 객체를 관리할 필요가 없다.


#include 
#include 
#include 
#include 
#include 

std::vector g_exceptions;
std::mutex g_mutex;

void func1()
{
	throw std::exception("exception 1");
}

void func2()
{
	throw std::exception("exception 2");
}

void thread_func1()
{
	try
	{
		func1();
	}
	catch (...)
	{
		std::lock_guard lock(g_mutex);
		g_exceptions.push_back(std::current_exception());
	}
}

void thread_func2()
{
	try
	{
		func2();
	}
	catch (...)
	{
		std::lock_guard lock(g_mutex);
		g_exceptions.push_back(std::current_exception());
	}
}

int main() {
	g_exceptions.clear();

	std::thread t1(thread_func1);
	std::thread t2(thread_func2);
	t1.join();
	t2.join();

	for (auto const & e : g_exceptions)
	{
		try
		{
			if (e != nullptr)
				std::rethrow_exception(e);
		}
		catch (std::exception const & ex)
		{
			std::cout << ex.what() << std::endl;
		}
	}

	return 0;
}


'ⓟrogramming > C++' 카테고리의 다른 글

[Thread] Synchronizing access to shared data with mutexes and locks  (0) 2018.01.04
[Thread] Working with threads  (0) 2018.01.04
Visual Studio 에서 Boost 사용법  (0) 2016.07.28
임시객체  (1) 2016.05.11
rvalue  (0) 2016.05.11
블로그 이미지

뚱땡이 우주인

,
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

https://www.tensorflow.org/


텐서플로(TensorFlow)는 구글이 오픈 소스 소프트웨어로 공개하고 있는 머신러닝(machine learning, 기계학습) 라이브러리다. 특히 딥러닝(deep learning)에 적용할 것을 고려해서 구조를 갖추고 있으며, 이분야에 관한 구글 내부의 연구 및 구글이 제공하는 서비스 개발에도 이용되고 있다.


딥러닝은 넓은 의미로 머신러닝 중 '신경망(neural network)' 이라는 모델의 일종이다.



머신러닝의 개념


머신러닝은 데이터의 속에 있는 '수학적인 구조'를 컴퓨터로 계산해서 발견해 내는 구조다,



Deep MNIST for Experts : https://www.tensorflow.org/get_started/mnist/pros

MNIST : 필기 문자(숫자) 이미지 데이터를 분류하는 데이터 세트로, 최종적으로 약 99%의 인식률을 달성하고 있음


'합성곱 신경망(CNN : Convolutional Neural Network)' : 딥러닝의 대표적인 예로 필기 문자를 인식


예제)

원서 : https://github.com/enakai00/jupyter_tfbook

번역서 : https://github.com/Jpub/TensorflowDeeplearning


텐서플로는 신경망 구성을 파이썬 코드로 표현하며, 학습용 데이터를 이용해 신경망을 최적화하는 처리를 자동적으로 수행하는 기능을 제공



환경 설정


도커 허브로 부터 이미지를 다운로드하고 컨테이너상에 주피터가 실행

도커 환경설정에서 CPU 와 메모리를 위와 같이 설정


Hongui-MacBook-Pro:~ hongkun$ mkdir $HOME/data
Hongui-MacBook-Pro:~ hongkun$ docker run -itd --name jupyter -p 8888:8888 -p 6006:6006 -v $HOME/data:/root/notebook -e PASSWORD=passw0rd enakai00/jupyter_tensorflow:0.9.0-cp27
Unable to find image 'enakai00/jupyter_tensorflow:0.9.0-cp27' locally
0.9.0-cp27: Pulling from enakai00/jupyter_tensorflow
a3ed95caeb02: Pull complete 
da71393503ec: Pull complete 
bd182e7407b8: Pull complete 
ab00e726fd5f: Pull complete 
d59566bcc7c5: Pull complete 
7dfa8a8cd0f3: Pull complete 
edc3b8fc01e0: Pull complete 
7f0730d44ae5: Pull complete 
608ebba7c0a3: Pull complete 
42d6024691cd: Pull complete 
06c005696a9c: Pull complete 
Digest: sha256:4a1f4f8af59e5a1de09d3a2f46670cc4e7e5302c49a9470fd988330c1972c8b9
Status: Downloaded newer image for enakai00/jupyter_tensorflow:0.9.0-cp27
318784070c039680132cad26809d6200f65988ca7b592a9f766017b4d1245c6b
Hongui-MacBook-Pro:~ hongkun$
Hongui-MacBook-Pro:~ hongkun$ docker ps -a
CONTAINER ID        IMAGE                                    COMMAND                  CREATED             STATUS                     PORTS                                            NAMES
318784070c03        enakai00/jupyter_tensorflow:0.9.0-cp27   "/usr/local/bin/in..."   6 minutes ago       Up 6 minutes               0.0.0.0:6006->6006/tcp, 0.0.0.0:8888->8888/tcp   jupyter
Hongui-MacBook-Pro:~ hongkun$ 

docker run -itd --name jupyter -p 8888:8888 -p 6006:6006 -v $HOME/data:/root/notebook -e PASSWORD=passw0rd enakai00/jupyter_tensorflow:0.9.0-cp27



"-e PASSWORD" 옵션에는 웹 브라우저에서 주피터에 접속할 때 사용할 인증 패스워드를 지정

컨테이너를 실행한 경우 주피터에서 생성한 노트북 파일은 사용자의 홈 디렉토리 (/User/<사용자명>) 아래에 있는 'data' 디렉터리에 저장된다.




웹브라우저에서 주피터에 접속할 때는 http://localhost:8888 로 접속 한다. 또한 텐서보드(TensorBoard)를 실행할 때는 URL http://localhost:6006으로 화면에 접속한다.




블로그 이미지

뚱땡이 우주인

,