Marsettler

Jaewan Park's blog

한국 카크로치디비 사용자 그룹 GitHub Tags
3 September 2018

std::shared_ptr is not thread safe

I found that std::shared_ptr is not thread-safe, and I leave that example.

main.cpp

#include <thread>

std::shared_ptr<int> g_ptr = nullptr;

int main() {
	std::thread t1([]() {
		int i = 0;
		while (true) {
			g_ptr = std::make_shared<int>(i);

			++i;
		}
	});

	std::thread t2([]() {
		while(true) {
			std::shared_ptr<int> ptr = g_ptr;
		}
	});

	t1.join();
	t2.join();

	return 0;
}

2018년 09월 3일
박재완

tags: c++ multithread