cpp_Thread_Mutex - 8BitsCoding/RobotMentor GitHub Wiki
๊ธฐ์กด์ OS๋ณ ๋ณ๋๋ก ๋๋ thead๋ฅผ ํ๋๋ก ํฉ์ณ๋ณด์!
๋จ, C++ํ์ค์ด๊ธฐ์ ๋ชจ๋ OS์์ ๋์๊ฐ๊ฒ ๋ง๋ค์๋ค.
๊ทธ์ ๋ฐ๋ฅธ ์ฐ๊ธฐ ๋ถํธํ ์ ์ด ๋ช ๊ฐ์ง ์กด์ฌํ๋ค..
- ๋ค๋ฅธ ์ฝ์ค์์ ์ฐ๋ ๋ฉ ํ๋ก๊ทธ๋๋ฐ์ ๋ํด ๋ฐฐ์ด๋ค.
- ๊ทธ๋์ C++์์ ์ฐ๋ ๋ฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ๋ฒ์ ์ด์ ์ ๋ง์ถค
- ํนํ
- ์ฐ๋ ๋
- ๋ฎคํ ์ค
- ์กฐ๊ฑด ๋ณ์
- ๋ ์์ผ๋, ์ฌ๊ธฐ์ ๋ฐฐ์ฐ์ง ์๋๋ค.
// Windwos
#include <Windows.h>
DWORD WINAPI PrintMessage()
{
// ...
}
int main() {
DWORD myThreadID;
HANDLE myHandle = CreateThread(0, 0, PrintMessage, NULL, 0, &myThreadID);
WaitForSingleObject(myHandle, INFINITE);
// thread๊ฐ ์ข
๋ฃ๋ ๋ ๊น์ง ๊ธฐ๋ค๋ฆฐ๋ค.
CloseHandle(myHandle);
return 0;
}// POSIX(pthread)
#include <pthread.h>
void *printMessage()
{
// ...
}
int main() {
pthread_t thread = 0;
int result_code = pthread_create(&thread, NULL, printMessage, NULL);
result_code = pthread_join(thread, NULL);
return 0;
}- C++11 ์ ๊น์ง ํ์ค ๋ฉํฐ์ฐ๋ ๋ฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์์
- OS๋ง๋ค ๋ฉํฐ์ฐ๋ ๋ฉ ๊ตฌํ์ด ๋ฌ๋์
- ๋ฆฌ๋ ์ค/์ ๋์ค : POSIX(pthread)
- ์๋์ฐ ์ฐ๋ ๋
- ์๋์ฐ์์ pthread๋ฅผ ์ฌ์ฉํ ์ ์์(ver 1003.1๋ง)
#include <iostream>
#include <string>
#include <thread>
void PrintMessage(const std::string& message)
{
std::cout << message << std::endl;
}
int main() {
std::thread thread(PrintMessage, "Message from a child thread");
PrintMessage("Message from a main thread");
return 0;
}๋จ, ์์ ์๋ Main์ด ๋๋๋ Thread๊ฐ ๊ณ์ ๋์๊ฐ๋ ์ํ์ด ์์
#include <iostream>
#include <string>
#include <thread>
void PrintMessage(const std::string& message)
{
std::cout << message << std::endl;
}
int main() {
std::thread thread(PrintMessage, "Message from a child thread");
PrintMessage("Message from a main thread");
thread.join();
return 0;
}#include <iostream>
#include <sstream>
#include <string>
#include <thread>
void PrintMessage(const std::string& message)
{
std::cout << message << std::endl;
}
int main() {
std::thread thread(PrintMessage, "Message from a child thread");
std::thread::id childThreadOD = thread.get_id();
std::stringstream ss;
ss << childThreadID;
std::string childThreadISStr = ss.str();
PrintMessage("Waiting the child thread(ID : " + childThreadIDStr + ")");
thread.join();
return 0;
}#include <iostream>
#include <string>
#include <thread>
void PrintMessage(const std::string& message)
{
std::cout << message << std::endl;
}
int main() {
std::thread thread(PrintMessage, "Message from a child thread");
PrintMessage("Waiting the child thread");
// ๋ผ์ด ๋ด๊ธฐ
thread.detach();
// ๋ค์ ๋ถ์ด๊ณ ์ถ๋ค๋ฉด?
if(thread.joinable())
{
thread.join();
}
return 0;
}#include <iostream>
#include <string>
#include <thread>
int main() {
auto PrintMessage = [](const std::string& message)
{
std::cout << message << std::endl;
};
std::thread thread(PrintMessage, "Message from a child thread");
PrintMessage("Waiting the child thread");
thread.join();
return 0;
}#include <iostream>
#include <string>
#include <thread>
#include <vector>
int main() {
std::vector<int> list(100, 1);
int result = 0;
std::thread thread([](const std::vector<int>& v, int& result)
{
for( auto item : v )
{
result += item;
}
}, list, std::ref(result));
thread.join();
std::cout << "Result: " << result << std::endl;
return 0;
}- ๋ค์์คํ์ด์ค ๊ทธ๋ฃน
- ํ์ฌ ์ฐ๋ ๋์ ์ ์ฉ๋๋ ๋์ฐ๋ฏธ ํจ์๋ค์ด ์์
- get_id()
- sleep_for()
- sleep_until()
- yield()
#include <iostream>
#include <chrono>
#include <string>
#include <thread>
void PrintCurrentTime() {/* ํ์ฌ ์๊ฐ ์ถ๋ ฅ */}
void PrintMessage(const std::string& message)
{
std::cout << "Sleep now ... ";
PrintCurrentTime();
std::this_thread::sleep_for(std::chrono::seconds(3));
// thread 3์ด ์๋ผ
std::cout << message << " ";
PrintCurrentTime();
}
int main() {
std::thread thread(PrintMessage, "Message from a child thread");
PrintMessage("Waiting the child thread");
thread.join();
return 0;
}auto end = std::chrono::high_resolution_clock::now() + delay;
while(std::chrono::high_resolution_clock_now() < end)
{
std::this_thread::yield();
}๊ณต์ ์์ ์ ๊ทธ๊ธฐ
#include <iostream>
#include <mutex>
#include <string>
#include <thread>
void PrintMessage(const std::string& message)
{
static std::mutex sMutex;
// ๋ฌผ๋ก ์ด๋ฐ์์ผ๋ก static์ ์ฐ๋๊ฑด ์ข์ง ๋ชปํ ์ต๊ด
sMutex.lock();
std::cout << message << std::endl;
sMutex.unlock();
}
int main() {
std::thread thread(PrintMessage, "Message from a child thread");
PrintMessage("Waiting the child thread");
thread.join();
return 0;
}#include <iostream>
#include <mutex>
#include <string>
#include <thread>
void PrintMessage(const std::string& message)
{
static std::mutex sMutex;
// ๋ฌผ๋ก ์ด๋ฐ์์ผ๋ก static์ ์ฐ๋๊ฑด ์ข์ง ๋ชปํ ์ต๊ด
sMutex.lock();
std::cout << message << std::endl;
// sMutex.unlock();
// unlock์ ์ํ๊ฒฝ์ฐ
}
int main() {
std::thread thread(PrintMessage, "Message from a child thread");
PrintMessage("Waiting the child thread");
thread.join();
return 0;
}๊ธฐ๋ณธ์ ์ผ๋ก unlock์ ์ํ๋๊ฒ ์ ๋ต์ด๊ณ ..
void PrintMessage(const std::string& message)
{
static std::mutex sMutex;
// ๋ฌผ๋ก ์ด๋ฐ์์ผ๋ก static์ ์ฐ๋๊ฑด ์ข์ง ๋ชปํ ์ต๊ด
std::scoped_lock<std::mutex> lock(sMutex);
std::cout << message << std::endl;
}void PrintMessage(const std::string& message)
{
static std::mutex sMutex;
// ๋ฌผ๋ก ์ด๋ฐ์์ผ๋ก static์ ์ฐ๋๊ฑด ์ข์ง ๋ชปํ ์ต๊ด
// ๋ค์๊ณผ ๊ฐ์ด ๋๋ ๊ฒฝ์ฐ scoped_lock์ ๋ณ๋๋ก ๊ฑธ ์ ์๋ค.
{
std::scoped_lock<std::mutex> lock(sMutex);
std::cout << message << std::endl;
}
{
std::scoped_lock<std::mutex> lock(sMutex);
std::cout << message << std::endl;
}
}