Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions submission/exercise-17/nahyunkim/ex17.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include <thread>
#include <iostream>
#include <atomic>
#include <mutex>
#include <chrono>
std::atomic<int> x1(0);

int y = 0;
static long min = 999'999'999;
template <typename T>
void solution1(T a);
std::memory_order omin;

const char* memory_order_to_string(std::memory_order order) {
switch (order) {
case std::memory_order_relaxed:
return "memory_order_relaxed";
case std::memory_order_consume:
return "memory_order_consume";
case std::memory_order_acquire:
return "memory_order_acquire";
case std::memory_order_release:
return "memory_order_release";
case std::memory_order_acq_rel:
return "memory_order_acq_rel";
case std::memory_order_seq_cst:
return "memory_order_seq_cst";
default:
return "unknown_memory_order";
}
}

int main() {

//memory_order_relaxed
std::cout << "memory order relaxed\n";
solution1<std::memory_order>(std::memory_order_relaxed);

//memory_order_consum
std::cout << "memory order consume\n";
solution1<std::memory_order>(std::memory_order_consume);

//memory_order_acquire
std::cout << "memory order acquire\n";
solution1<std::memory_order>(std::memory_order_acquire);

//memory_order_release
std::cout << "memory order release\n";
solution1<std::memory_order>(std::memory_order_release);

//memory_order_acq_rel
std::cout << "memory order acq rel\n";
solution1<std::memory_order>(std::memory_order_acq_rel);

//memory_order_seq_cst
std::cout << "memory order seq cst\n";
solution1<std::memory_order>(std::memory_order_seq_cst);



std::cout << " The fastest Memory Order: " << memory_order_to_string(omin) << '\n';






}

template <typename T>
void solution1(T a) { // atomicÀ» ÅëÇÑ ÇØ°á
std::thread th1([&] {
for (int i = 0; i < 100'000'000; ++i) {
x1.fetch_add(1, a);
}
});

std::thread th2([&] {for (int i = 0; i < 100'000'000; ++i) {
x1.fetch_add(1, a);
}});

th1.join();
th2.join();

std::cout << x1 << '\n';
auto _time = std::chrono::steady_clock::now();

auto time_point = std::chrono::duration_cast<std::chrono::milliseconds>(_time.time_since_epoch());

if (min > time_point.count()) {
min = time_point.count();
omin = a;
}
std::cout << "atomic time(ms): " << time_point.count() << '\n';

x1 = 0;

}


91 changes: 91 additions & 0 deletions submission/exercise-18/nahyunkim/ex18.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <thread>
#include <iostream>
#include <atomic>
#include <mutex>
#include <chrono>

int x2 = 0;
std::mutex x2_mutex;
std::condition_variable cv; // mutex의 소유권을 가지는 변수


void solution2();


int main() {


std::cout << "Mutex: ";

solution2();

exit(0);


}



void solution2() {

std::thread t1 = std::thread([&] {
for (int i = 0; i < 100'000'000; ++i) {

{
std::lock_guard<std::mutex> lock(x2_mutex);
x2++;
}

cv.notify_all();

}
});

std::thread t2 = std::thread([&] {
for (int i = 0; i < 100'000'000; ++i) {
{
std::lock_guard<std::mutex> lock(x2_mutex);
x2++;
}
cv.notify_all();
}
});

//x2가 2억이 되기 전까지는 sleep
std::thread t3 = std::thread([&] {

//객체 생성하지만 소유권은 아직 가지지 않는다.
std::unique_lock<std::mutex> lck(x2_mutex);

//lck는 소유권 획득을 시도하면 획득할때까지 무한 대기한다.
cv.wait(lck, [&]() { //람다함수
return x2 == 200'000'000; // 리턴값이 1이 될때 notification이 생성이된다.
});


});

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

if (t1.joinable()) {
t1.join();
}

if (t2.joinable()) {
t2.join();
}
if (t3.joinable()) {
t3.join();
}
std::cout << x2 << '\n';
auto _time = std::chrono::steady_clock::now();

auto time_point = std::chrono::duration_cast<std::chrono::milliseconds>(_time.time_since_epoch());

std::cout << "mutex time(ms): " << time_point.count() << '\n';



}