• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

19.4 [14:04] std::lock_guard, std::scoped_lock 컴파일 오류

23.08.01 11:11 작성 23.08.01 11:33 수정 조회수 390

1


#include <iostream>
#include <thread>
#include <atomic>
#include <mutex>
#include <chrono>

using namespace std;

mutex mtx;

int main()
{
	int shared_memory(0);
	//atomic<int> shared_memory(0);

	auto count_func = [&]() {
		for (int i = 0; i < 1000; ++i)
		{
			this_thread::sleep_for(chrono::milliseconds(1));
			
			//shared_memory++;
			//shared_memory.fetch_add(1);

			
			/*mtx.lock();

			shared_memory++;

			mtx.unlock();*/
			//std::lock_guard lock(mtx);
			
			//std::scoped_lock lock(mtx);
			
			shared_memory++;
		}
	};

	thread t1 = thread(count_func);
	thread t2 = thread(count_func);
	thread t3 = thread(count_func);
	thread t4 = thread(count_func);
	
	t1.join();
	t2.join();
	t3.join();
	t4.join();
	

	cout << "After" << endl;
	cout << shared_memory << endl;

	return 0;
}

 

 

 

Debug 모드로 진행시 std::lock_guard랑 scoped_lock이 정상적으로 compile되고 작동이 되는데,

Release 모드로 변경하니 해당 lock_guard는 missing template이 뜨고, scoped_lock은 member가 존재하지 않는다고 뜨는데

어떤 점이 문제일까요?..

교수님 강의에서는 Release 모드에서도 잘 작동하는 거로 보였습니다.

 

 

답변 1

답변을 작성해보세요.

1

Soobak님의 프로필

Soobak

2023.08.01

안녕하세요, 답변 도우미 Soobak 입니다.

우선, 질문해주신 코드에서 주석 처리하신 부분을 제외하면 lock_guardscoped_lock 을 사용하는 부분이 없으셔서, 어느 코드를 바탕으로 설명을 드려할지 어렵네요. 🥲

하지만, Debug 모드에서는 잘 작동하시는 코드가 Release 모드에서는 안되신다고 하시는 것과, 말씀하신 에러 메시지로 미루어보아 설정 문제로 보입니다.
따라서, Release 모드에서도 컴파일러 설정이 C++17 이상으로 설정되어 있는지 확인해보시길 바랍니다.

첨부해주신 두 번째 사진에서 Debug 란을 Release 로 바꾸시고 C++ Lanaguage Standard 란이 어떻게 설정되어 있는지 한 번 확인해보시면 좋을 것 같습니다.

image

오준원님의 프로필

오준원

질문자

2023.08.01

감사합니다!. 덕분에 해결되었습니다. 질문하는 법을 익혀야될거 같네요 ㅎㅎ.. 노력하겠습니다.