[c++] CAS ๊ตฌํ˜„ ๋ฐ ABA ๋ฌธ์ œ ํ•ด๊ฒฐ :: ABA ํ•ด๊ฒฐ_4. ๊ทธ ์™ธ์˜ ๋ฐฉ๋ฒ•๋“ค
์ปดํ“จํ„ฐ๊ณผํ•™ (CS)/Operating System 2020. 7. 2. 19:45

๋ชฉ์ฐจ ๋ฌธ์ œ ์ •์˜ lock free ๊ตฌํ˜„ ABA ํ•ด๊ฒฐ intํ˜• ๊ตฌํ˜„(+ Hazard pointer) Counter ๊ทธ ์™ธ์˜ ๋ฐฉ๋ฒ•๋“ค mutex lock(spin lock)๊ณผ์˜ ๋น„๊ต ๊ทธ ์™ธ์˜ ๋ฐฉ๋ฒ•๋“ค DCAS _InterlockedCompareExchange128 ์‚ฌ์šฉ ์œ„์˜ Counter ๊ธฐ๋ฒ•๊ณผ ๋น„์Šทํ•˜๋‹ค. 64bit๋ฅผ ๋ชจ๋‘ ์ด์šฉํ•˜์—ฌ ์ฃผ์†Œ๋ฅผ ํ‘œํ˜„ํ•˜๋Š” CPU์—์„œ๋Š” ์œ„์˜ Counter ๊ธฐ๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ c++์˜ _InterlockedCompareExchange128 ์—ฐ์‚ฐ์„ ์ด์šฉํ•˜์—ฌ ์ด๋ฅผ ๋Œ€์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ์—ฐ์‚ฐ์€ 64bit ์ž๋ฃŒํ˜• 2๊ฐœ๋ฅผ ๋ฌถ์–ด์„œ 128bit๋กœ CAS ์—ฐ์‚ฐ์„ ์‹œํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด ๋•Œ 64bit ์”ฉ ๋ถ„๋ฆฌํ•˜์—ฌ ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ๋ฐ”๊พธ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค. lock ๋ณ€์ˆ˜ ์ด์šฉ ์–ด์…ˆ๋ธ”๋ฆฌ์–ด๊นŒ์ง€ ๋ณด๋ฉด lock-fr..

[c++] CAS ๊ตฌํ˜„ ๋ฐ ABA ๋ฌธ์ œ ํ•ด๊ฒฐ :: ABA ํ•ด๊ฒฐ_3. Counter ๊ตฌํ˜„
์ปดํ“จํ„ฐ๊ณผํ•™ (CS)/Operating System 2020. 7. 2. 19:43

๋ชฉ์ฐจ ๋ฌธ์ œ ์ •์˜ lock free ๊ตฌํ˜„ ABA ํ•ด๊ฒฐ intํ˜• ๊ตฌํ˜„(+ Hazard pointer) Counter ๊ทธ ์™ธ์˜ ๋ฐฉ๋ฒ•๋“ค mutex lock(spin lock)๊ณผ์˜ ๋น„๊ต Counter #include #include #include #include #include #include using namespace std; class Node { public: int value; Node* next; Node() : value(0) { next = NULL; } Node(int k_value) { next = NULL; value = k_value; } }; int node_n; //random ๊ตฌํ˜„ random_device rd; mt19937 gen(rd()); uniform_int_distribut..

[c++] CAS ๊ตฌํ˜„ ๋ฐ ABA ๋ฌธ์ œ ํ•ด๊ฒฐ :: ABA ํ•ด๊ฒฐ_2. hazard pointer
์ปดํ“จํ„ฐ๊ณผํ•™ (CS)/Operating System 2020. 7. 2. 19:41

๋ชฉ์ฐจ ๋ฌธ์ œ ์ •์˜ lock free ๊ตฌํ˜„ ABA ํ•ด๊ฒฐ intํ˜• ๊ตฌํ˜„(+ Hazard pointer) Counter ๊ทธ ์™ธ์˜ ๋ฐฉ๋ฒ•๋“ค mutex lock(spin lock)๊ณผ์˜ ๋น„๊ต Hazard Pointer https://m.blog.naver.com/PostView.nhn?blogId=jjoommnn&logNo=130127286459&proxyReferer=https:%2F%2Fwww.google.com%2F ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์ž‘์„ฑํ•˜์˜€๋‹ค. ๋ฌธ์ œ์  ๋จผ์ € delete๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์‚ญ์ œํ•˜๋ฉด, ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋˜ ์Šค๋ ˆ๋“œ์— ๋ฌธ์ œ๊ฐ€ ์ƒ๊ธธ ์ˆ˜๋„ ์žˆ๊ณ  ๊ฐ์ฒด๊ฐ€ ์‚ญ์ œ๋œ ์ฃผ์†Œ์— ๋‹ค์‹œ ๊ฐ™์€ ๊ฐ์ฒด๊ฐ€ ํ• ๋‹น๋˜์–ด ABA ๋ฌธ์ œ๊ฐ€ ์ผ์–ด๋‚  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๋‹ค. ์ƒˆ๋กœ ๊ฐ์ฒด๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์€ ์‚ฌ์šฉ์ž๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” list๊ฐ€ ์•„๋‹ˆ๋‹ค. 1๋ฒˆ ๋ฌธ์ œ๋ฅผ ..

[c++] CAS ๊ตฌํ˜„ ๋ฐ ABA ๋ฌธ์ œ ํ•ด๊ฒฐ :: ABA ํ•ด๊ฒฐ_1. intํ˜• ๊ตฌํ˜„
์ปดํ“จํ„ฐ๊ณผํ•™ (CS)/Operating System 2020. 7. 2. 19:37

๋ชฉ์ฐจ ๋ฌธ์ œ ์ •์˜ lock free ๊ตฌํ˜„ ABA ํ•ด๊ฒฐ intํ˜• ๊ตฌํ˜„(+ Hazard pointer) Counter ๊ทธ ์™ธ์˜ ๋ฐฉ๋ฒ•๋“ค mutex lock(spin lock)๊ณผ์˜ ๋น„๊ต ABA ํ•ด๊ฒฐ intํ˜• ๊ตฌํ˜„ ABA ๋ฌธ์ œ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ intํ˜•์œผ๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋ฌธ์ œ์ ์€ ์กด์žฌํ•œ๋‹ค(๋’ท๋ถ€๋ถ„์— ์–ธ๊ธ‰). ์ฝ”๋“œ #define _CRT_SECURE_NO_WARNINGS #define NULL_INT -1 // NULL ๊ฐ’ #include #include #include #include #include // #include #include using namespace std; struct Node { int data; Node* next_node; }; int node_n; atomic free_l..

[c++] CAS ๊ตฌํ˜„ ๋ฐ ABA ๋ฌธ์ œ ํ•ด๊ฒฐ :: ๋ฌธ์ œ ์ •์˜, ๊ธฐ๋ณธ CAS ๊ตฌํ˜„
์ปดํ“จํ„ฐ๊ณผํ•™ (CS)/Operating System 2020. 7. 2. 19:27

๋ชฉ์ฐจ ๋ฌธ์ œ ์ •์˜ lock free ๊ตฌํ˜„ ABA ํ•ด๊ฒฐ intํ˜• ๊ตฌํ˜„(+ Hazard pointer) Counter ๊ทธ ์™ธ์˜ ๋ฐฉ๋ฒ•๋“ค mutex lock(spin lock)๊ณผ์˜ ๋น„๊ต ๋ฌธ์ œ ์ •์˜ ๋ฌธ์ œ Linked List ์ž๋ฃŒ๊ตฌ์กฐ์—์„œ push()์™€ pop() ์—ฐ์‚ฐ์„ ๊ตฌํ˜„ํ•œ๋‹ค. Linked List๋Š” FreeList์™€ HeadList ๋‘๊ฐ€์ง€๊ฐ€ ์žˆ์œผ๋ฉฐ ์—ฌ๋Ÿฌ ์Šค๋ ˆ๋“œ๊ฐ€ ๋‘ ๊ฐœ์˜ List๋ฅผ ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉฐ push, pop ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์Šค๋ ˆ๋“œ๋“ค์ด ๊ฐ๊ฐ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” List๋“ค์— ๋Œ€ํ•œ ์ƒํ˜ธ๋ฐฐ์ œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ํ•ด์„ ์ƒํ˜ธ ๋ฐฐ์ œ์—๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ, ํฌ๊ฒŒ ๋‘๊ฐ€์ง€๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๋‹ค. lock ๋ณ€์ˆ˜์˜ ์กฐ๊ฑด์„ ์ฒดํฌํ•˜๋ฉฐ ๋ฐ˜๋ณต๋ฌธ์„ ๋Œ๊ณ  ์žˆ๋Š” spin lock๊ณผ, lock ๋ณ€์ˆ˜ ์—†์ด ๊ณ„์† ์‹œ๋„ํ•˜๋ฉด์„œ ์ ์ ˆํ•œ ๋•Œ์— ๋™์ž‘์„ ์ˆ˜ํ–‰..

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค :: ์นดํŽซ (brute force)
Algorithm ๋ฌธ์ œ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2020. 5. 28. 21:50

์นดํŽซ ์ฝ”๋“œ #include #include #include using namespace std; vector solution(int brown, int yellow) { vector answer; vector candidate; for (int i = 1; i ๋งž์œผ๋ฉด (๊ฐ€๋กœ, ์„ธ๋กœ) ๊ธธ์ด๋ฅผ ๊ฒฐ๊ณผ vector์— ๋„ฃ์–ด์„œ return brown ๊ฐฏ์ˆ˜ = 2*๊ฐ€๋กœyellow + 2*์„ธ๋กœyellow +4 4 : ์–‘์ชฝ ๋ชจ์„œ๋ฆฌ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ #include #include using namespace std; vector solution(int brown, int red) { int len = brown / 2 + 2; int w = len - 3; int h = 3; while(w >= h){ if(w * h ..

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค :: ์ˆซ์ž์•ผ๊ตฌ (brute force)
Algorithm ๋ฌธ์ œ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2020. 5. 28. 09:00

์ˆซ์ž์•ผ๊ตฌ ์ฝ”๋“œ #include #include using namespace std; bool check(int* arr, vector& bb) { int num = bb[0]; int s = bb[1]; int b = bb[2]; int n[3]; n[0] = num / 100; n[1] = (num - n[0] * 100) / 10; n[2] = (num - n[0] * 100 - n[1] * 10); //strike check int t_num = 0; for (int i = 0; i < 3; i++) { if (n[i] == arr[i]) t_num++; } if (t_num != s) return false; t_num = 0; for (int i = 0; i < 3; i++) { if (n[i]..

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค :: ์†Œ์ˆ˜ ์ฐพ๊ธฐ (brute force)
Algorithm ๋ฌธ์ œ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2020. 5. 24. 10:24

https://programmers.co.kr/learn/courses/30/lessons/42839 ์ฝ”๋“œ #include #include #include #include using namespace std; set res; void test(int num) { if (num num์ด ์ตœ๋Œ€ 9999999 (10^7) if (num%i == 0) return; } res.insert(num); return; } void makePrime(vector& memo,int num) { if (memo.size()==0) return; int size = memo.size(); for (int i = 0; i < size; i++) { // 7 int value = memo[i]; test(num * 10 + v..