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
51 changes: 44 additions & 7 deletions submission/exercise-15/shintaewon/exercise-15.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,62 @@
#include <iostream>
#include <thread>
#include <atomic>
#include <mutex>
#include <chrono>

int x = 0;
using namespace std;
using namespace chrono;

int main() {
std::thread th1([]() {
atomic<int> x1 = 0;
mutex mtx;
int x2 = 0;

// atomic ���
auto start1 = steady_clock::now();
thread th1([&]() {
for (int i = 0; i < 100'000'000; ++i) {
++x;
++x1;
}
});

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

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

std::cout << x << '\n';
auto end1 = steady_clock::now();
duration<double> elapsed1 = end1 - start1;

cout << "x1: " << x1 << '\n';
cout << "Atomic �ɸ� �ð�: " << elapsed1.count() << "��\n";

//mutex ���
auto start2 = steady_clock::now();
thread th3([&]() {
for (int i = 0; i < 100'000'000; ++i) {
lock_guard<mutex> lock(mtx);
++x2;
}
});
thread th4([&]() {
for (int i = 0; i < 100'000'000; ++i) {
lock_guard<mutex> lock(mtx);
++x2;
}
});

th3.join();
th4.join();

auto end2 = steady_clock::now();
duration<double> elapsed2 = end2 - start2;

cout << "x2: " << x2 << '\n';
cout << "Mutex �ɸ� �ð�: " << elapsed2.count() << "��\n";

return 0;
}
59 changes: 59 additions & 0 deletions submission/exercise-16/shintaewon/exercise-16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>
#include <thread>
#include <atomic>
#include <mutex>
#include <chrono>

int main() {
std::atomic<int> x1(0);
std::mutex mtx;
int x2 = 0;

// atomic ���
auto start1 = std::chrono::steady_clock::now();
std::thread th1([&]() {
for (int i = 0; i < 100'000'000; ++i) {
++x1;
}
});
std::thread th2([&]() {
for (int i = 0; i < 100'000'000; ++i) {
++x1;
}
});

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

auto end1 = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed1 = end1 - start1;

std::cout << "x1: " << x1 << '\n';
std::cout << "�ɸ� �ð�: " << elapsed1.count() << "\n";

//mutex ���
auto start2 = std::chrono::steady_clock::now();
std::thread th3([&]() {
for (int i = 0; i < 100'000'000; ++i) {
std::lock_guard<std::mutex> lock(mtx);
++x2;
}
});
std::thread th4([&]() {
for (int i = 0; i < 100'000'000; ++i) {
std::lock_guard<std::mutex> lock(mtx);
++x2;
}
});

th3.join();
th4.join();

auto end2 = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed2 = end2 - start2;

std::cout << "x2: " << x2 << '\n';
std::cout << "�ɸ� �ð�: " << elapsed2.count() << "\n";

return 0;
}