├── latch ├── main.cpp ├── Latch.h └── latch.vcxproj.filters ├── HashTableX ├── main.cpp ├── hash_table_x.h ├── HashTableX.vcxproj.filters └── hash_table_x.cpp ├── hashmap ├── parallel_hashtable.cpp ├── hashmap.vcxproj.filters ├── parallel_hashtable.h └── Source.cpp ├── sort ├── main.cpp ├── sort.vcxproj.filters └── parallel_quick_sort.h ├── s2_thread_safe_stack_implementation ├── main.cpp ├── trivial_stack.h ├── thread_safe_stack.h └── s2_thread_safe_stack_implementation.vcxproj.filters ├── s2_thread_safe_stack_implementation_race_conditions ├── main.cpp ├── thread_safe_stack.h └── s2_thread_safe_stack_implementation_race_conditions.vcxproj.filters ├── common ├── utils.cpp ├── utils.h ├── common.vcxproj.filters ├── common_objs.h └── common_thread_safe_queue.h ├── s8_simple_threadpool ├── main.cpp ├── s8_simple_threadpool.vcxproj.filters └── simple_thread_pool.h ├── s1_parallel_accumulate ├── main.cpp ├── s1_parallel_accumulate.vcxproj.filters └── parallel_accumulate.h ├── thread_local_demo ├── main.cpp └── thread_local_demo.vcxproj.filters ├── s3_introduction_to_futures_and_async_tasks ├── main.cpp └── s3_introduction_to_futures_and_async_tasks.vcxproj.filters ├── s1_join_and_detach_functions ├── main.cpp └── s1_join_and_detach_functions.vcxproj.filters ├── s8_simple_threadpool_with_work_stealing ├── main.cpp ├── s8_simple_threadpool_with_work_stealing.vcxproj.filters └── simple_threadpool_with_work_stealing.h ├── s3_thread_safe_queue_implementation ├── main.cpp ├── s3_thread_safe_queue_implementation.vcxproj.filters └── thread_safe_queue.h ├── s1_joinability_of_thread ├── main.cpp └── s1_joinability_of_thread.vcxproj.filters ├── s8_simple_threadpool_waiting ├── main.cpp ├── s8_simple_threadpool_waiting.vcxproj.filters └── simple_thread_pool_with_waiting.h ├── s8_simple_threadpool_with_local_work_queue ├── main.cpp ├── s8_simple_threadpool_with_local_work_queue.vcxproj.filters └── simple_threadpool_with_local_work_queue.h ├── s8_simple_threadpool_waiting_for_others ├── main.cpp ├── s8_simple_threadpool_waiting_for_others.vcxproj.filters └── simple_threadpool_waiting_for_other_tasks.h ├── s1_problamatic_situation_arise_when_passing_parameters_to_a_thread ├── main.cpp └── s1_problamatic_situation_arise_when_passing_parameters_to_a_thread.vcxproj.filters ├── s3_communication_between_threads_using_promises ├── main.cpp └── s3_communication_between_threads_using_promises.vcxproj.filters ├── s1_transferring_ownership_of_a_thread ├── main.cpp └── s1_transferring_ownership_of_a_thread.vcxproj.filters ├── s1_how_to_launch_a_thread ├── main.cpp └── s1_how_to_launch_a_thread.vcxproj.filters ├── s4_thread_safe_queue ├── main.cpp └── s4_thread_safe_queue.vcxproj.filters ├── reverse ├── reverse.vcxproj.filters └── reverse.vcxproj ├── quick_sort ├── quick_sort.vcxproj.filters └── quick_sort.vcxproj ├── thread_pool └── thread_pool.vcxproj.filters ├── lock_free_stack └── lock_free_stack.vcxproj.filters ├── s2_mutex ├── main.cpp └── s2_mutex.vcxproj.filters ├── thread_safe_queue ├── thread_safe_queue.vcxproj.filters └── thread_safe_queue.vcxproj ├── thread_safe_stack └── thread_safe_stack.vcxproj.filters ├── LockFreeLinearSearch └── LockFreeLinearSearch.vcxproj.filters ├── lockfree_linkedlist └── lockfree_linkedlist.vcxproj.filters ├── prime_number_generator ├── main.cpp └── prime_number_generator.vcxproj.filters ├── s3_parallel_accumulate_algorithm ├── main.cpp └── s3_parallel_accumulate_algorithm.vcxproj.filters ├── s3_introduction_to_package_tasks ├── main.cpp └── s3_introduction_to_package_tasks.vcxproj.filters ├── coroutines ├── coroutines.vcxproj.filters └── main.cpp ├── debug_test ├── debug_test.vcxproj.filters └── debug.cpp ├── interruptible_thread ├── jthread.h ├── interruptible_thread.vcxproj.filters └── main.cpp ├── semaphore ├── semaphore.vcxproj.filters └── semaphore.h ├── parallel_stl_intro ├── parallel_stl_intro.vcxproj.filters └── main.cpp ├── s2_dead_locks ├── s2_dead_locks.vcxproj.filters └── main.cpp ├── s2_unique_locks ├── s2_unique_locks.vcxproj.filters └── main.cpp ├── s3_shared_futures ├── s3_shared_futures.vcxproj.filters └── main.cpp ├── s1_how_to_pass_parameters_to_a_thread ├── s1_how_to_pass_parameters_to_a_thread.vcxproj.filters └── main.cpp ├── s1_some_useful_operations_on_threads ├── s1_some_useful_operations_on_threads.vcxproj.filters └── main.cpp ├── s3_async_task_detail_discussion ├── s3_async_task_detail_discussion.vcxproj.filters └── main.cpp ├── s1_how_to_handle_join_in_exception_scenarios ├── s1_how_to_handle_join_in_exception_scenarios.vcxproj.filters └── main.cpp ├── s3_details_about_condition_variables ├── s3_details_about_condition_variables.vcxproj.filters └── main.cpp ├── s2_things_to_remember_when_using_mutex ├── s2_things_to_remember_when_using_mutex.vcxproj.filters └── main.cpp ├── s3_introduction_to_condition_variables ├── s3_introduction_to_condition_variables.vcxproj.filters └── main.cpp ├── s3_retrieving_exception_using_futures ├── s3_retrieving_exception_using_futures.vcxproj.filters └── main.cpp ├── barrier ├── main.cpp ├── barrier.vcxproj.filters └── barrier.h ├── parallel_stl ├── parallel_stl.vcxproj.filters ├── main.cpp └── matrix_multiplication.cpp ├── matrix ├── matrix.vcxproj.filters ├── main.cpp └── matrix.h ├── find ├── find.vcxproj.filters ├── main.cpp └── parallel_find.h ├── scan ├── scan.vcxproj.filters ├── main.cpp └── partial_sum.h ├── for_each ├── for_each.vcxproj.filters ├── main.cpp └── parallel_for_each.h ├── .gitattributes └── .gitignore /latch/main.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /HashTableX/main.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /hashmap/parallel_hashtable.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /sort/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | 5 | std::cout << "Sorting project" << std::endl; 6 | } 7 | -------------------------------------------------------------------------------- /s2_thread_safe_stack_implementation/main.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include "trivial_stack.h" 5 | 6 | int main() 7 | { 8 | return 0; 9 | } -------------------------------------------------------------------------------- /s2_thread_safe_stack_implementation_race_conditions/main.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #include "thread_safe_stack.h" 5 | 6 | int main() 7 | { 8 | return 0; 9 | } -------------------------------------------------------------------------------- /common/utils.cpp: -------------------------------------------------------------------------------- 1 | #include "utils.h" 2 | #include "common_objs.h" 3 | 4 | void print_results(const char* const tag, 5 | high_resolution_clock::time_point startTime, 6 | high_resolution_clock::time_point endTime) { 7 | printf("%s: Time: %fms\n", tag, 8 | duration_cast>(endTime - startTime).count()); 9 | } -------------------------------------------------------------------------------- /s8_simple_threadpool/main.cpp: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "simple_thread_pool.h" 4 | 5 | int main() 6 | { 7 | thread_pool pool; 8 | std::cout << "Testing thread pool" << std::endl; 9 | 10 | for (int i = 0; i < 100; i++) 11 | { 12 | pool.submit([=] { 13 | printf(" %d printed by thread - %d \n", i, std::this_thread::get_id()); 14 | }); 15 | } 16 | 17 | system("pause"); 18 | } -------------------------------------------------------------------------------- /s1_parallel_accumulate/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "parallel_accumulate.h" 6 | 7 | int main() 8 | { 9 | int result = 0; 10 | std::vector vec(10000); 11 | for (int i = 0; i < 10000; i++) 12 | vec[i] = 2; 13 | 14 | parallel_accumulate(vec.begin(), vec.end(), result); 15 | 16 | std::cout << "final result = " << result << std::endl; 17 | } -------------------------------------------------------------------------------- /thread_local_demo/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | std::atomic i = 0; 7 | 8 | void foo() { 9 | ++i; 10 | std::cout << i; 11 | } 12 | 13 | int main() { 14 | std::thread t1(foo); 15 | std::thread t2(foo); 16 | std::thread t3(foo); 17 | 18 | t1.join(); 19 | t2.join(); 20 | t3.join(); 21 | 22 | std::cout << std::endl; 23 | } -------------------------------------------------------------------------------- /s3_introduction_to_futures_and_async_tasks/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int find_answer_how_old_universe_is() 5 | { 6 | //this is not the ture value 7 | return 5000; 8 | } 9 | 10 | void do_other_calculations() 11 | { 12 | std::cout << "Doing other stuff " << std::endl; 13 | } 14 | 15 | int main() 16 | { 17 | std::future the_answer_future = std::async(find_answer_how_old_universe_is); 18 | do_other_calculations(); 19 | std::cout << "The answer is " << the_answer_future.get() << std::endl; 20 | } -------------------------------------------------------------------------------- /s1_join_and_detach_functions/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void func_1() 6 | { 7 | std::this_thread::sleep_for(std::chrono::milliseconds(5000)); 8 | std::cout << "hello from func_1 \n"; 9 | } 10 | 11 | 12 | int main() 13 | { 14 | std::thread thread_1(func_1); 15 | thread_1.join(); // first example code. Comment this for second example 16 | // thread_1.detach(); // second example code. Comment this for first example 17 | 18 | std::cout << "hello from main thread \n"; 19 | } -------------------------------------------------------------------------------- /s8_simple_threadpool_with_work_stealing/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "simple_threadpool_with_work_stealing.h" 6 | 7 | int main() 8 | { 9 | const int size = 800; 10 | std::list my_array; 11 | 12 | srand(0); 13 | 14 | for (size_t i = 0; i < size; i++) 15 | { 16 | my_array.push_back(rand()); 17 | } 18 | 19 | my_array = parallel_quick_sort(my_array); 20 | 21 | for (size_t i = 0; i < size; i++) 22 | { 23 | std::cout << my_array.front() << std::endl; 24 | my_array.pop_front(); 25 | } 26 | } -------------------------------------------------------------------------------- /common/utils.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using std::chrono::duration; 10 | using std::chrono::duration_cast; 11 | using std::chrono::high_resolution_clock; 12 | using std::milli; 13 | using std::random_device; 14 | using std::sort; 15 | using std::vector; 16 | 17 | 18 | void print_results(const char* const tag, high_resolution_clock::time_point startTime, high_resolution_clock::time_point endTime); 19 | 20 | unsigned long get_optimal_num_threads(); -------------------------------------------------------------------------------- /s3_thread_safe_queue_implementation/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "thread_safe_queue.h" 5 | 6 | thread_safe_queue queue; 7 | 8 | void func_1() 9 | { 10 | int value; 11 | queue.wait_pop(value); 12 | std::cout << value << std::endl; 13 | 14 | } 15 | 16 | void func_2() 17 | { 18 | int x = 10; 19 | std::this_thread::sleep_for(std::chrono::milliseconds(2000)); 20 | queue.push(x); 21 | } 22 | 23 | int main() 24 | { 25 | std::thread thread_1(func_1); 26 | std::thread thread_2(func_2); 27 | 28 | thread_1.join(); 29 | thread_2.join(); 30 | } -------------------------------------------------------------------------------- /s1_joinability_of_thread/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void func_1() 5 | { 6 | std::cout << "hello from method \n"; 7 | } 8 | 9 | 10 | int main() 11 | { 12 | std::thread thread_1(func_1); 13 | 14 | if (thread_1.joinable()) 15 | std::cout << "this is joinable thread \n"; 16 | 17 | //thread_1.join(); 18 | 19 | if (thread_1.joinable()) 20 | { 21 | std::cout << "this is joinable thread \n"; 22 | } 23 | else 24 | { 25 | std::cout << "after calling join, thread_1 is not a joinable thread \n"; 26 | } 27 | 28 | std::cout << "hello from main thread \n"; 29 | } -------------------------------------------------------------------------------- /latch/Latch.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | class Latch { 8 | 9 | unsigned const count; 10 | std::atomic spaces; 11 | std::atomic generation; 12 | 13 | public: 14 | explicit Latch(unsigned count_) : 15 | count(count_), spaces(count), generation(0) 16 | {} 17 | void wait() 18 | { 19 | unsigned const my_generation = generation; 20 | 21 | if (--spaces) 22 | { 23 | while (generation == my_generation) 24 | std::this_thread::yield(); 25 | } 26 | else { 27 | generation++; 28 | } 29 | } 30 | }; -------------------------------------------------------------------------------- /s8_simple_threadpool_waiting/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "simple_thread_pool_with_waiting.h" 6 | 7 | int main() 8 | { 9 | std::cout << "Simple thread pool with waiting \n"; 10 | const int size = 1000; 11 | int* my_array = new int[size]; 12 | 13 | srand(0); 14 | 15 | for (size_t i = 0; i < size; i++) 16 | { 17 | //my_array[i] = rand() % 10; 18 | my_array[i] = 1; 19 | } 20 | 21 | long result = parallel_accumulate(my_array, my_array + size, 0); 22 | std::cout << "final sum is - " << result << std::endl; 23 | return 0; 24 | } -------------------------------------------------------------------------------- /s8_simple_threadpool_with_local_work_queue/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "simple_threadpool_with_local_work_queue.h" 6 | 7 | int main() 8 | { 9 | std::cout << "Simple thread pool with local work queue \n"; 10 | const int size = 800; 11 | std::list my_array; 12 | 13 | srand(0); 14 | 15 | for (size_t i = 0; i < size; i++) 16 | { 17 | my_array.push_back(rand()); 18 | } 19 | 20 | my_array = parallel_quick_sort(my_array); 21 | 22 | for (size_t i = 0; i < size; i++) 23 | { 24 | std::cout << my_array.front() << std::endl; 25 | my_array.pop_front(); 26 | } 27 | } -------------------------------------------------------------------------------- /s8_simple_threadpool_waiting_for_others/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "simple_threadpool_waiting_for_other_tasks.h" 6 | 7 | int main() 8 | { 9 | std::cout << "Simple thread pool with waiting for other tasks \n"; 10 | const int size = 800; 11 | std::list my_array; 12 | 13 | srand(0); 14 | 15 | for (size_t i = 0; i < size; i++) 16 | { 17 | my_array.push_back(rand()); 18 | } 19 | 20 | my_array = parallel_quick_sort(my_array); 21 | 22 | for (size_t i = 0; i < size; i++) 23 | { 24 | std::cout << my_array.front() << std::endl; 25 | my_array.pop_front(); 26 | } 27 | } -------------------------------------------------------------------------------- /s1_problamatic_situation_arise_when_passing_parameters_to_a_thread/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void func_2(int& x) 6 | { 7 | while (true) 8 | { 9 | std::cout << x << std::endl; 10 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 11 | } 12 | } 13 | 14 | void func_1() 15 | { 16 | int x = 5; 17 | std::thread thread_2(func_2, std::ref(x)); 18 | thread_2.detach(); 19 | std::this_thread::sleep_for(std::chrono::milliseconds(5000)); 20 | std::cout << "thread_1 finished execution \n"; 21 | } 22 | 23 | 24 | int main() 25 | { 26 | std::thread thread_1(func_1); 27 | thread_1.join(); 28 | } -------------------------------------------------------------------------------- /s3_communication_between_threads_using_promises/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void print_int(std::future& fut) { 8 | std::cout << "waiting for value from print thread \n"; 9 | std::cout << "value: " << fut.get() << '\n'; 10 | } 11 | 12 | int main() 13 | { 14 | std::promise prom; 15 | std::future fut = prom.get_future(); 16 | 17 | std::thread print_thread(print_int, std::ref(fut)); 18 | 19 | std::this_thread::sleep_for(std::chrono::milliseconds(5000)); 20 | std::cout << "setting the value in main thread \n"; 21 | prom.set_value(10); 22 | 23 | print_thread.join(); 24 | } -------------------------------------------------------------------------------- /s1_transferring_ownership_of_a_thread/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void func_1() 5 | { 6 | std::cout << "This is a function 1"; 7 | } 8 | 9 | void func_2() 10 | { 11 | std::cout << "This is a function 2"; 12 | } 13 | 14 | int main() 15 | { 16 | std::thread thread_1(func_1); 17 | 18 | //try to assigne one thread to another 19 | //std::thread thread_2 = thread_1; 20 | 21 | //move one thread form another 22 | std::thread thread_2 = std::move(thread_1); 23 | 24 | //implicit call to move constructor 25 | thread_1 = std::thread(func_2); 26 | 27 | std::thread thread_3 = std::move(thread_2); 28 | 29 | //this is wrong 30 | thread_1 = std::move(thread_3); 31 | 32 | thread_1.join(); 33 | thread_3.join(); 34 | 35 | } -------------------------------------------------------------------------------- /s1_how_to_launch_a_thread/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | void func1() 5 | { 6 | std::cout << "Hello from function \n"; 7 | } 8 | 9 | class my_class { 10 | 11 | public: 12 | void operator()() 13 | { 14 | std::cout << "hello from the class with function call operator \n"; 15 | } 16 | }; 17 | 18 | int main() 19 | { 20 | //create thread using fuction 21 | std::thread thread1(func1); 22 | 23 | //create thread using class with function call operator 24 | my_class mc; 25 | std::thread thread2(mc); 26 | 27 | //create thread using lambda expression 28 | std::thread thread3([] { 29 | std::cout << "hello from the lambda \n"; 30 | }); 31 | 32 | thread1.join(); 33 | thread2.join(); 34 | thread3.join(); 35 | 36 | std::cout << "This is main thread \n"; 37 | } -------------------------------------------------------------------------------- /s2_thread_safe_stack_implementation/trivial_stack.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | template 8 | class trivial_thread_safe_stack { 9 | std::stack stk; 10 | std::mutex m; 11 | 12 | public: 13 | void push(T element) 14 | { 15 | std::lock_guard lg(m); 16 | stk.push(element); 17 | } 18 | 19 | void pop() 20 | { 21 | std::lock_guard lg(m); 22 | stk.pop(); 23 | } 24 | 25 | T& top() 26 | { 27 | std::lock_guard lg(m); 28 | return stk.top(); 29 | } 30 | 31 | bool empty() 32 | { 33 | std::lock_guard lg(m); 34 | return stk.empty(); 35 | } 36 | 37 | size_t size() 38 | { 39 | std::lock_guard lg(m); 40 | return stk.size(); 41 | } 42 | }; -------------------------------------------------------------------------------- /s4_thread_safe_queue/main.cpp: -------------------------------------------------------------------------------- 1 | #include "thread_safe_queue.h" 2 | 3 | int main() 4 | { 5 | sequential_queue5 queueInteger; 6 | queueInteger.push(5645); 7 | queueInteger.push(87456); 8 | queueInteger.push(94564); 9 | queueInteger.push(2347); 10 | queueInteger.push(634); 11 | 12 | queueInteger.printData(); 13 | 14 | std::cout << "Removing: " << *(queueInteger.pop().get()) << std::endl; 15 | std::cout << "Removing: " << *(queueInteger.pop().get()) << std::endl; 16 | std::cout << "Removing by wait_pop(): " << *(queueInteger.wait_pop().get()) << std::endl; 17 | queueInteger.printData(); 18 | 19 | std::cout << "Removing by wait_pop(): " << *(queueInteger.wait_pop().get()) << std::endl; 20 | queueInteger.printData(); 21 | 22 | std::cout << "Removing: " << *(queueInteger.pop().get()) << std::endl; 23 | queueInteger.printData(); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /reverse/reverse.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | -------------------------------------------------------------------------------- /quick_sort/quick_sort.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | -------------------------------------------------------------------------------- /thread_pool/thread_pool.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | -------------------------------------------------------------------------------- /lock_free_stack/lock_free_stack.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | -------------------------------------------------------------------------------- /s2_mutex/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | std::list my_list; 7 | std::mutex m; 8 | 9 | void add_to_list1(int const& x) 10 | { 11 | m.lock(); 12 | my_list.push_front(x); 13 | m.unlock(); 14 | } 15 | 16 | 17 | void size1() 18 | { 19 | m.lock(); 20 | int size = my_list.size(); 21 | m.unlock(); 22 | std::cout << "size of the list is : " << size << std::endl; 23 | } 24 | 25 | 26 | void add_to_list2(int const& x) 27 | { 28 | std::lock_guard lg(m); 29 | my_list.push_back(x); 30 | } 31 | 32 | void size2() 33 | { 34 | std::lock_guard lg(m); 35 | int size = my_list.size(); 36 | std::cout << "size of the list is : " << size << std::endl; 37 | } 38 | 39 | int main() 40 | { 41 | std::thread thread_1(add_to_list2, 4); 42 | std::thread thread_2(add_to_list2, 11); 43 | 44 | thread_1.join(); 45 | thread_2.join(); 46 | } -------------------------------------------------------------------------------- /thread_safe_queue/thread_safe_queue.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | -------------------------------------------------------------------------------- /thread_safe_stack/thread_safe_stack.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | -------------------------------------------------------------------------------- /LockFreeLinearSearch/LockFreeLinearSearch.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | -------------------------------------------------------------------------------- /lockfree_linkedlist/lockfree_linkedlist.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | -------------------------------------------------------------------------------- /prime_number_generator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | using namespace std; 8 | 9 | void SieveOfEratosthenes(const unsigned int num) 10 | { 11 | const unsigned int array_size = num + 1; 12 | bool* pno = new bool[array_size]; 13 | 14 | memset((void*)pno, true, sizeof(pno)); 15 | 16 | for (int i = 2; i * i <= num; i++) 17 | { 18 | if ( pno[i] == true ) 19 | { 20 | for (int j = i * 2; j <= num; j += i) 21 | { 22 | pno[j] = false; 23 | } 24 | } 25 | } 26 | 27 | for (int i = 2; i <= num; i++) 28 | { 29 | if (pno[i]) 30 | cout << i << " "; 31 | } 32 | } 33 | 34 | 35 | int main() 36 | { 37 | int num = 15; 38 | cout << "The prime numbers smaller or equal to " << num << " are: "; 39 | SieveOfEratosthenes(num); 40 | return 0; 41 | } -------------------------------------------------------------------------------- /s3_parallel_accumulate_algorithm/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int MIN_ELEMENT_COUNT = 1000; 6 | 7 | template 8 | int parallal_accumulate(iterator begin, iterator end) 9 | { 10 | long length = std::distance(begin, end); 11 | 12 | //atleast runs 1000 element 13 | if (length <= MIN_ELEMENT_COUNT) 14 | { 15 | std::cout << std::this_thread::get_id() << std::endl; 16 | return std::accumulate(begin, end, 0); 17 | } 18 | 19 | iterator mid = begin; 20 | std::advance(mid, (length + 1) / 2); 21 | 22 | //recursive all to parallel_accumulate 23 | std::future f1 = std::async(std::launch::deferred | std::launch::async, 24 | parallal_accumulate, mid, end); 25 | auto sum = parallal_accumulate(begin, mid); 26 | return sum + f1.get(); 27 | } 28 | 29 | int main() 30 | { 31 | std::vector v(10000, 1); 32 | std::cout << "The sum is " << parallal_accumulate(v.begin(), v.end()) << '\n'; 33 | } -------------------------------------------------------------------------------- /s3_introduction_to_package_tasks/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int add(int x, int y) 8 | { 9 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); 10 | std::cout << "add function runs in : " << std::this_thread::get_id() << std::endl; 11 | return x + y; 12 | } 13 | 14 | void task_thread() 15 | { 16 | std::packaged_task task_1(add); 17 | std::future future_1 = task_1.get_future(); 18 | 19 | std::thread thread_1(std::move(task_1), 5, 6); 20 | thread_1.detach(); 21 | 22 | std::cout << "task thread - " << future_1.get() << "\n"; 23 | } 24 | 25 | void task_normal() 26 | { 27 | std::packaged_task task_1(add); 28 | std::future future_1 = task_1.get_future(); 29 | task_1(7, 8); 30 | std::cout << "task normal - " << future_1.get() << "\n"; 31 | } 32 | int main() 33 | { 34 | task_thread(); 35 | task_normal(); 36 | std::cout << "main thread id : " << std::this_thread::get_id() << std::endl; 37 | } -------------------------------------------------------------------------------- /coroutines/coroutines.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /debug_test/debug_test.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /interruptible_thread/jthread.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | //_MSC_VER 7 | 8 | class interrupt_flag 9 | { 10 | bool local_flag; 11 | public: 12 | void set() 13 | { 14 | local_flag = true; 15 | } 16 | bool is_set() 17 | { 18 | return local_flag; 19 | } 20 | }; 21 | 22 | thread_local interrupt_flag this_thread_flag; 23 | 24 | class jthread_local { 25 | 26 | std::thread _thread; 27 | interrupt_flag* flag; 28 | 29 | public: 30 | template 31 | jthread_local(FunctionType f) 32 | { 33 | std::promise p; 34 | _thread = std::thread([f, &p] { 35 | p.set_value(&this_thread_flag); 36 | f(); 37 | }); 38 | flag = p.get_future().get(); 39 | } 40 | 41 | 42 | void interrupt() 43 | { 44 | flag->set(); 45 | } 46 | 47 | ~jthread_local() 48 | { 49 | if (_thread.joinable()) 50 | _thread.join(); 51 | } 52 | }; 53 | 54 | bool interrupt_point() 55 | { 56 | if (this_thread_flag.is_set()) 57 | return true; 58 | return false; 59 | } -------------------------------------------------------------------------------- /semaphore/semaphore.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s2_mutex/s2_mutex.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /parallel_stl_intro/parallel_stl_intro.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /prime_number_generator/prime_number_generator.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s2_dead_locks/s2_dead_locks.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s1_joinability_of_thread/s1_joinability_of_thread.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s2_unique_locks/s2_unique_locks.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s1_how_to_launch_a_thread/s1_how_to_launch_a_thread.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s3_shared_futures/s3_shared_futures.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /thread_local_demo/thread_local_demo.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s1_join_and_detach_functions/s1_join_and_detach_functions.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /semaphore/semaphore.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | /** General semaphore with N permissions **/ 6 | class Semaphore { 7 | const size_t num_permissions; 8 | size_t avail; 9 | std::mutex m; 10 | std::condition_variable cv; 11 | public: 12 | /** Default constructor. Default semaphore is a binary semaphore **/ 13 | explicit Semaphore(const size_t& num_permissions = 1) : num_permissions(num_permissions), avail(num_permissions) { } 14 | 15 | /** Copy constructor. Does not copy state of mutex or condition variable, 16 | only the number of permissions and number of available permissions **/ 17 | Semaphore(const Semaphore& s) : num_permissions(s.num_permissions), avail(s.avail) { } 18 | 19 | void acquire() { 20 | std::unique_lock lk(m); 21 | cv.wait(lk, [this] { return avail > 0; }); 22 | avail--; 23 | } 24 | 25 | void release() { 26 | //TODO : here also use unique_locks 27 | m.lock(); 28 | avail++; 29 | m.unlock(); 30 | cv.notify_one(); 31 | } 32 | 33 | size_t available() const { 34 | return avail; 35 | } 36 | }; -------------------------------------------------------------------------------- /s1_how_to_pass_parameters_to_a_thread/s1_how_to_pass_parameters_to_a_thread.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s1_some_useful_operations_on_threads/s1_some_useful_operations_on_threads.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s1_transferring_ownership_of_a_thread/s1_transferring_ownership_of_a_thread.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s3_async_task_detail_discussion/s3_async_task_detail_discussion.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s3_introduction_to_package_tasks/s3_introduction_to_package_tasks.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s3_parallel_accumulate_algorithm/s3_parallel_accumulate_algorithm.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s1_how_to_handle_join_in_exception_scenarios/s1_how_to_handle_join_in_exception_scenarios.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s1_how_to_pass_parameters_to_a_thread/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | ////////////////////////////// for first example 6 | void func_1(int x, int y) 7 | { 8 | std::cout << " X + Y = " << x + y << std::endl; 9 | } 10 | 11 | void run_code1() 12 | { 13 | int p = 9; 14 | int q = 8; 15 | 16 | std::thread thread_1(func_1, p, q); 17 | 18 | thread_1.join(); 19 | } 20 | 21 | 22 | ////////////////////////////// for second example 23 | void func_2(int& x) 24 | { 25 | while (true) 26 | { 27 | std::cout << "Thread_1 x value : " << x << std::endl; 28 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 29 | } 30 | } 31 | 32 | void run_code2() 33 | { 34 | int x = 9; 35 | std::cout << "Main thread current value of X is : " << x << std::endl; 36 | std::thread thread_1(func_2, std::ref(x)); 37 | std::this_thread::sleep_for(std::chrono::milliseconds(5000)); 38 | 39 | x = 15; 40 | std::cout << "Main thread X value changed to : " << x << std::endl; 41 | thread_1.join(); 42 | } 43 | 44 | 45 | int main() 46 | { 47 | run_code1(); 48 | //run_code2(); 49 | } -------------------------------------------------------------------------------- /s3_details_about_condition_variables/s3_details_about_condition_variables.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s2_things_to_remember_when_using_mutex/s2_things_to_remember_when_using_mutex.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s3_introduction_to_condition_variables/s3_introduction_to_condition_variables.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s3_retrieving_exception_using_futures/s3_retrieving_exception_using_futures.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s3_async_task_detail_discussion/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void printing() 6 | { 7 | std::cout << "printing runs on-" << std::this_thread::get_id() << std::endl; 8 | } 9 | 10 | int addition(int x, int y) 11 | { 12 | std::cout << "addition runs on-" << std::this_thread::get_id() << std::endl; 13 | return x + y; 14 | } 15 | 16 | int substract(int x, int y) 17 | { 18 | std::cout << "substract runs on-" << std::this_thread::get_id() << std::endl; 19 | return x - y; 20 | } 21 | 22 | int main() 23 | { 24 | std::cout << "main thread id -" << std::this_thread::get_id() << std::endl; 25 | 26 | int x = 100; 27 | int y = 50; 28 | 29 | std::future f1 = std::async(std::launch::async, printing); 30 | std::future f2 = std::async(std::launch::deferred, addition, x, y); 31 | std::future f3 = std::async(std::launch::deferred | std::launch::async, 32 | substract, x, y); 33 | 34 | f1.get(); 35 | std::cout << "value recieved using f2 future -" << f2.get() << std::endl; 36 | std::cout << "value recieved using f2 future -" << f3.get() << std::endl; 37 | 38 | } -------------------------------------------------------------------------------- /s3_introduction_to_futures_and_async_tasks/s3_introduction_to_futures_and_async_tasks.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /s3_details_about_condition_variables/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | bool have_i_arrived = false; 10 | int total_distance = 5; 11 | int distance_coverd = 0; 12 | std::condition_variable cv; 13 | std::mutex m; 14 | 15 | void keep_moving() 16 | { 17 | while (true) 18 | { 19 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 20 | distance_coverd++; 21 | 22 | //notify the waiting threads if the event occurss 23 | if (distance_coverd == total_distance) 24 | cv.notify_one(); 25 | } 26 | 27 | } 28 | 29 | void ask_driver_to_wake_u_up_at_right_time() 30 | { 31 | std::unique_lock ul(m); 32 | cv.wait(ul, [] {return distance_coverd == total_distance; }); 33 | std::cout << "finally i am there, distance_coverd = " << distance_coverd << std::endl;; 34 | } 35 | 36 | int main() 37 | { 38 | std::thread driver_thread(keep_moving); 39 | std::thread passener_thread(ask_driver_to_wake_u_up_at_right_time); 40 | passener_thread.join(); 41 | driver_thread.join(); 42 | } -------------------------------------------------------------------------------- /s3_communication_between_threads_using_promises/s3_communication_between_threads_using_promises.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /barrier/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | void func(boost::barrier& cur_barier, boost::atomic& counter) 8 | { 9 | ++counter; 10 | //std::cout << counter << "\n"; 11 | cur_barier.wait(); 12 | std::cout << "Current value after barrier :" << counter << "\n"; 13 | } 14 | 15 | int main() 16 | { 17 | boost::barrier bar(3); 18 | boost::atomic current(0); 19 | boost::thread thr1(boost::bind(&func, boost::ref(bar), boost::ref(current))); 20 | boost::thread thr2(boost::bind(&func, boost::ref(bar), boost::ref(current))); 21 | boost::thread thr3(boost::bind(&func, boost::ref(bar), boost::ref(current))); 22 | boost::thread thr4(boost::bind(&func, boost::ref(bar), boost::ref(current))); 23 | boost::thread thr5(boost::bind(&func, boost::ref(bar), boost::ref(current))); 24 | boost::thread thr6(boost::bind(&func, boost::ref(bar), boost::ref(current))); 25 | thr1.join(); 26 | thr2.join(); 27 | thr3.join(); 28 | thr4.join(); 29 | thr5.join(); 30 | thr6.join(); 31 | } -------------------------------------------------------------------------------- /s1_problamatic_situation_arise_when_passing_parameters_to_a_thread/s1_problamatic_situation_arise_when_passing_parameters_to_a_thread.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | -------------------------------------------------------------------------------- /parallel_stl/parallel_stl.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | -------------------------------------------------------------------------------- /s1_some_useful_operations_on_threads/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | void run_code1() 6 | { 7 | std::cout << "Allowed max number of parallel threads : " 8 | << std::thread::hardware_concurrency() << std::endl; 9 | } 10 | 11 | 12 | void func_1() 13 | { 14 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 15 | std::cout << "new thread id : " << std::this_thread::get_id() << std::endl; 16 | } 17 | 18 | void run_code2() 19 | { 20 | std::thread thread_1(func_1); 21 | 22 | std::cout << "thread_1 id before joining : " << thread_1.get_id() << std::endl; 23 | thread_1.join(); 24 | 25 | std::thread thread_2; 26 | 27 | std::cout << "default consturcted thread id : " << thread_2.get_id() << std::endl; 28 | std::cout << "thread_1 id after joining : " << thread_1.get_id() << std::endl; 29 | std::cout << "Main thread id : " << std::this_thread::get_id() << std::endl; 30 | 31 | std::cout << "\n\nAllowed max number of parallel threads : " 32 | << std::thread::hardware_concurrency() << std::endl; 33 | } 34 | 35 | int main() 36 | { 37 | run_code1(); // example 1 38 | //run_code2(); // example 2 39 | } -------------------------------------------------------------------------------- /latch/latch.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /matrix/matrix.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /s2_thread_safe_stack_implementation/thread_safe_stack.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | template 10 | class thread_safe_stack { 11 | std::stack> stk; 12 | std::mutex m; 13 | 14 | public: 15 | void push(T element) 16 | { 17 | std::lock_guard lg(m); 18 | stk.push(std::make_shared(element)); 19 | } 20 | 21 | std::shared_ptr pop() 22 | { 23 | std::lock_guard lg(m); 24 | if (stk.empty()) 25 | { 26 | throw std::runtime_error("stack is empty"); 27 | } 28 | 29 | std::shared_ptr res(stk.top()); 30 | stk.pop(); 31 | return res; 32 | } 33 | 34 | void pop(T& value) 35 | { 36 | std::lock_guard lg(m); 37 | if (stk.empty()) throw std::runtime_error("stack is empty"); 38 | value = *(stk.top().get()); 39 | stk.pop(); 40 | } 41 | 42 | 43 | bool empty() 44 | { 45 | std::lock_guard lg(m); 46 | return stk.empty(); 47 | } 48 | 49 | size_t size() 50 | { 51 | std::lock_guard lg(m); 52 | return stk.size(); 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /barrier/barrier.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /find/find.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /scan/scan.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /sort/sort.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /for_each/for_each.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /s2_thread_safe_stack_implementation_race_conditions/thread_safe_stack.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | template 10 | class thread_safe_stack { 11 | std::stack> stk; 12 | std::mutex m; 13 | 14 | public: 15 | void push(T element) 16 | { 17 | std::lock_guard lg(m); 18 | stk.push(std::make_shared(element)); 19 | } 20 | 21 | std::shared_ptr pop() 22 | { 23 | std::lock_guard lg(m); 24 | if (stk.empty()) 25 | { 26 | throw std::runtime_error("stack is empty"); 27 | } 28 | 29 | std::shared_ptr res(stk.top()); 30 | stk.pop(); 31 | return res; 32 | } 33 | 34 | void pop(T& value) 35 | { 36 | std::lock_guard lg(m); 37 | if (stk.empty()) throw std::runtime_error("stack is empty"); 38 | value = *(stk.top().get()); 39 | stk.pop(); 40 | } 41 | 42 | 43 | bool empty() 44 | { 45 | std::lock_guard lg(m); 46 | return stk.empty(); 47 | } 48 | 49 | size_t size() 50 | { 51 | std::lock_guard lg(m); 52 | return stk.size(); 53 | } 54 | }; 55 | -------------------------------------------------------------------------------- /hashmap/hashmap.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /interruptible_thread/interruptible_thread.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /debug_test/debug.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | const size_t testSize = 1000; 9 | 10 | using std::chrono::duration; 11 | using std::chrono::duration_cast; 12 | using std::chrono::high_resolution_clock; 13 | using std::milli; 14 | 15 | void function3() 16 | { 17 | throw; 18 | } 19 | 20 | 21 | std::string function1(int i) 22 | { 23 | function3(); 24 | 25 | if (i < 100) 26 | { 27 | return std::string("Hello"); 28 | } 29 | 30 | return std::string("Hi"); 31 | } 32 | 33 | 34 | 35 | double function2( int i) 36 | { 37 | std::string str = function1(i); 38 | 39 | if (i < 200) 40 | { 41 | return 3.98; 42 | } 43 | 44 | return 9.45; 45 | } 46 | 47 | int init_number(int i) 48 | { 49 | function2(i); 50 | 51 | if (i < 1000) 52 | { 53 | i = i; 54 | } 55 | else 56 | { 57 | i = i * 2; 58 | } 59 | 60 | return i; 61 | } 62 | 63 | int main() 64 | { 65 | 66 | std::vector ints(testSize); 67 | for (size_t i = 0; i < testSize; i++) 68 | { 69 | ints[i] = init_number(i); 70 | std::cout << init_number(i) << " " << function1(i) << " " << function2(i); 71 | } 72 | 73 | return 0; 74 | } -------------------------------------------------------------------------------- /s1_parallel_accumulate/s1_parallel_accumulate.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /s3_retrieving_exception_using_futures/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | void throw_exception() 7 | { 8 | throw std::invalid_argument("input cannot be negative"); 9 | } 10 | 11 | void calculate_squre_root(std::promise& prom) 12 | { 13 | int x = 1; 14 | std::cout << "Please, enter an integer value: "; 15 | try 16 | { 17 | std::cin >> x; 18 | if (x < 0) 19 | { 20 | throw_exception(); 21 | } 22 | prom.set_value(std::sqrt(x)); 23 | } 24 | catch (std::exception&) 25 | { 26 | prom.set_exception(std::current_exception()); 27 | } 28 | } 29 | 30 | void print_result(std::future& fut) { 31 | try 32 | { 33 | int x = fut.get(); 34 | std::cout << "value: " << x << '\n'; 35 | } 36 | catch (std::exception& e) { 37 | std::cout << "[exception caught: " << e.what() << "]\n"; 38 | } 39 | } 40 | 41 | int main() 42 | { 43 | std::promise prom; 44 | std::future fut = prom.get_future(); 45 | 46 | std::thread printing_thread(print_result, std::ref(fut)); 47 | std::thread calculation_thread(calculate_squre_root, std::ref(prom)); 48 | 49 | printing_thread.join(); 50 | calculation_thread.join(); 51 | } -------------------------------------------------------------------------------- /s4_thread_safe_queue/s4_thread_safe_queue.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /s8_simple_threadpool/s8_simple_threadpool.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /HashTableX/hash_table_x.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | //#include 5 | 6 | class parallel_hash_table 7 | { 8 | public: 9 | struct entry 10 | { 11 | std::atomic key; 12 | std::atomic value; 13 | }; 14 | 15 | private: 16 | entry* m_entries; 17 | int m_arraySize; 18 | 19 | public: 20 | parallel_hash_table(unsigned int arraySize); 21 | ~parallel_hash_table(); 22 | 23 | // Basic operations 24 | void SetItem(unsigned int key, unsigned int value); 25 | unsigned int GetItem(unsigned int key); 26 | unsigned int GetItemCount(); 27 | void Clear(); 28 | }; 29 | 30 | class simple_hash_table 31 | { 32 | struct entry { 33 | int key; 34 | int value; 35 | }; 36 | 37 | 38 | private: 39 | entry* m_entries; 40 | int m_arraySize; 41 | 42 | int hashCode(int key) { 43 | return key % m_arraySize; 44 | } 45 | 46 | public: 47 | simple_hash_table(unsigned int arraySize); 48 | ~simple_hash_table(); 49 | 50 | // Basic operations 51 | void SetItem(unsigned int key, unsigned int value); 52 | unsigned int GetItem(unsigned int key); 53 | unsigned int GetItemCount(); 54 | void Clear(); 55 | 56 | }; -------------------------------------------------------------------------------- /s2_thread_safe_stack_implementation/s2_thread_safe_stack_implementation.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /s3_thread_safe_queue_implementation/s3_thread_safe_queue_implementation.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /s8_simple_threadpool_waiting/s8_simple_threadpool_waiting.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /s8_simple_threadpool_with_work_stealing/s8_simple_threadpool_with_work_stealing.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /s2_thread_safe_stack_implementation_race_conditions/s2_thread_safe_stack_implementation_race_conditions.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /s8_simple_threadpool_waiting_for_others/s8_simple_threadpool_waiting_for_others.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /HashTableX/HashTableX.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Header Files 25 | 26 | 27 | Source Files 28 | 29 | 30 | -------------------------------------------------------------------------------- /s8_simple_threadpool_with_local_work_queue/s8_simple_threadpool_with_local_work_queue.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | 23 | 24 | Source Files 25 | 26 | 27 | -------------------------------------------------------------------------------- /s8_simple_threadpool/simple_thread_pool.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include "common_thread_safe_queue.h" 9 | #include "common_objs.h" 10 | 11 | #include "utils.h" 12 | 13 | class thread_pool 14 | { 15 | std::atomic_bool done; 16 | threadsafe_queue> work_queue; 17 | std::vector threads; 18 | join_threads joiner; 19 | 20 | void worker_thread() 21 | { 22 | while (!done) 23 | { 24 | std::function task; 25 | if (work_queue.try_pop(task)) 26 | { 27 | task(); 28 | } 29 | else 30 | { 31 | std::this_thread::yield(); 32 | } 33 | } 34 | } 35 | 36 | public: 37 | thread_pool() :done(false), joiner(threads) 38 | { 39 | int const thread_count = std::thread::hardware_concurrency(); 40 | 41 | try 42 | { 43 | for (int i = 0; i < thread_count; i++) 44 | { 45 | threads.push_back( 46 | std::thread(&thread_pool::worker_thread, this)); 47 | } 48 | } 49 | catch (...) 50 | { 51 | done = true; 52 | throw; 53 | } 54 | } 55 | 56 | ~thread_pool() 57 | { 58 | done = true; 59 | } 60 | 61 | template 62 | void submit(Function_type f) 63 | { 64 | work_queue.push(std::function(f)); 65 | } 66 | }; -------------------------------------------------------------------------------- /common/common.vcxproj.filters: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Header Files 20 | 21 | 22 | Header Files 23 | 24 | 25 | Header Files 26 | 27 | 28 | 29 | 30 | Source Files 31 | 32 | 33 | -------------------------------------------------------------------------------- /hashmap/parallel_hashtable.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | //---------------------------------------------- 6 | // The World's Simplest Lock-Free HashTable 7 | // 8 | // Maps 32-bit integers to 32-bit integers. 9 | // Uses open addressing with linear probing. 10 | // You can call SetItem and GetItem from several threads simultaneously. 11 | // GetItem is wait-free. 12 | // SetItem is lock-free. 13 | // You can't assign any value to key = 0. 14 | // In the m_cells array, key = 0 is reserved to indicate an unused cell. 15 | // You can't assign value = 0 to any key. 16 | // value = 0 means the key is unused. 17 | // The hash table never grows in size. 18 | // You can't delete individual items from the hash table. 19 | // You can Clear the hash table, but only at a time when there are no other calls being made from other threads. 20 | //---------------------------------------------- 21 | class HashTable1 22 | { 23 | public: 24 | struct Entry 25 | { 26 | std::atomic key; 27 | std::atomic value; 28 | }; 29 | 30 | private: 31 | Entry* m_entries; 32 | uint32_t m_arraySize; 33 | 34 | public: 35 | HashTable1(uint32_t arraySize); 36 | ~HashTable1(); 37 | 38 | // Basic operations 39 | void SetItem(uint32_t key, uint32_t value); 40 | uint32_t GetItem(uint32_t key); 41 | uint32_t GetItemCount(); 42 | void Clear(); 43 | }; -------------------------------------------------------------------------------- /barrier/barrier.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | class barrier_spin { 8 | 9 | unsigned const count; 10 | std::atomic spaces; 11 | std::atomic generation; 12 | 13 | public: 14 | explicit barrier_spin(unsigned count_) : 15 | count(count_), spaces(count), generation(0) 16 | {} 17 | void wait() 18 | { 19 | unsigned const my_generation = generation; 20 | 21 | if (!--spaces) 22 | { 23 | spaces = count; 24 | ++generation; 25 | } 26 | else { 27 | while (generation == my_generation) 28 | std::this_thread::yield(); 29 | } 30 | } 31 | }; 32 | 33 | class barrier_cond { 34 | public: 35 | explicit barrier_cond(std::size_t iCount) : 36 | mThreshold(iCount), 37 | mCount(iCount), 38 | mGeneration(0) { 39 | } 40 | 41 | void Wait() { 42 | std::unique_lock lLock{ mMutex }; 43 | auto lGen = mGeneration; 44 | if (!--mCount) { 45 | mGeneration++; 46 | mCount = mThreshold; 47 | mCond.notify_all(); 48 | } 49 | else { 50 | mCond.wait(lLock, [this, lGen] { return lGen != mGeneration; }); 51 | } 52 | } 53 | 54 | private: 55 | std::mutex mMutex; 56 | std::condition_variable mCond; 57 | std::size_t mThreshold; 58 | std::size_t mCount; 59 | std::size_t mGeneration; 60 | }; -------------------------------------------------------------------------------- /s1_how_to_handle_join_in_exception_scenarios/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "common_objs.h" 6 | 7 | void func_1() 8 | { 9 | std::this_thread::sleep_for(std::chrono::milliseconds(500)); 10 | std::cout << "hello from method \n"; 11 | } 12 | 13 | void other_operations() 14 | { 15 | std::cout << "This is other operation \n"; 16 | throw std::runtime_error("this is a runtime error"); 17 | } 18 | 19 | /////////////////////////////// for first example 20 | void run_code1() 21 | { 22 | std::thread thread_1(func_1); 23 | 24 | try { 25 | //do other operations 26 | other_operations(); 27 | thread_1.join(); 28 | } 29 | catch (...) 30 | { 31 | } 32 | std::cout << "This is main thread \n"; 33 | } 34 | 35 | /////////////////////////////// for second example 36 | void run_code2() 37 | { 38 | std::thread thread_1(func_1); 39 | 40 | //do other operations 41 | try 42 | { 43 | other_operations(); 44 | thread_1.join(); 45 | } 46 | catch (...) 47 | { 48 | thread_1.join(); 49 | } 50 | } 51 | 52 | /////////////////////////////// for third example 53 | void run_code3() 54 | { 55 | std::thread thread_1(func_1); 56 | thread_guard tg(thread_1); 57 | 58 | //do other operations 59 | try 60 | { 61 | other_operations(); 62 | } 63 | catch (...) 64 | { 65 | } 66 | } 67 | 68 | 69 | int main() 70 | { 71 | run_code1(); 72 | //run_code2(); 73 | //run_code3(); 74 | } -------------------------------------------------------------------------------- /s2_things_to_remember_when_using_mutex/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | /*********************** example 1 *********************/ 7 | class list_wrapper { 8 | std::list my_list; 9 | std::mutex m; 10 | 11 | public: 12 | void add_to_list(int const& x) 13 | { 14 | std::lock_guard lg(m); 15 | my_list.push_front(x); 16 | } 17 | 18 | void size() 19 | { 20 | std::lock_guard lg(m); 21 | int size = my_list.size(); 22 | std::cout << "size of the list is : " << size << std::endl; 23 | } 24 | 25 | std::list* get_data() 26 | { 27 | return &my_list; 28 | } 29 | }; 30 | 31 | /*********************** example 2 *********************/ 32 | class data_object { 33 | 34 | public: 35 | void some_operation() 36 | { 37 | std::cout << "this is some operation \n"; 38 | } 39 | }; 40 | 41 | class data_wrapper { 42 | 43 | data_object protected_data; 44 | std::mutex m; 45 | 46 | public: 47 | template 48 | void do_some_work(function f) 49 | { 50 | std::lock_guard lg(m); 51 | f(protected_data); 52 | } 53 | }; 54 | 55 | data_object* unprotected_data; 56 | 57 | void malisious_function(data_object& data) 58 | { 59 | unprotected_data = &data; 60 | } 61 | 62 | void run_code() 63 | { 64 | data_wrapper wrapper; 65 | wrapper.do_some_work(malisious_function); 66 | } 67 | 68 | 69 | int main() 70 | { 71 | run_code(); 72 | } -------------------------------------------------------------------------------- /s3_shared_futures/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | /******************************* Example 1 ******************************/ 9 | void print_result1(std::future& fut) 10 | { 11 | //std::cout << fut.get() << "\n"; 12 | if (fut.valid()) 13 | { 14 | std::cout << "this is valid future\n"; 15 | std::cout << fut.get() << "\n"; 16 | } 17 | else 18 | { 19 | std::cout << "this is invalid future\n"; 20 | } 21 | } 22 | 23 | void run_code1() 24 | { 25 | std::promise prom; 26 | std::future fut(prom.get_future()); 27 | 28 | std::thread th1(print_result1, std::ref(fut)); 29 | std::thread th2(print_result1, std::ref(fut)); 30 | 31 | prom.set_value(5); 32 | 33 | th1.join(); 34 | th2.join(); 35 | } 36 | 37 | /************************************* Example 2 **************************************/ 38 | 39 | void print_result2(std::shared_future& fut) 40 | { 41 | std::cout << fut.get() << " - valid future \n"; 42 | } 43 | 44 | void run_code2() 45 | { 46 | std::promise prom; 47 | std::shared_future fut(prom.get_future()); 48 | 49 | std::thread th1(print_result2, std::ref(fut)); 50 | std::thread th2(print_result2, std::ref(fut)); 51 | 52 | prom.set_value(5); 53 | 54 | th1.join(); 55 | th2.join(); 56 | } 57 | 58 | int main() 59 | { 60 | run_code1(); 61 | run_code2(); 62 | 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /scan/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "partial_sum.h" 11 | #include "utils.h" 12 | 13 | const size_t testSize = 1'000; 14 | 15 | using std::chrono::duration; 16 | using std::chrono::duration_cast; 17 | using std::chrono::high_resolution_clock; 18 | using std::milli; 19 | 20 | int main() 21 | { 22 | std::vector ints(testSize); 23 | std::vector outs(testSize); 24 | for (auto& i : ints) { 25 | i = 1; 26 | } 27 | 28 | //sequential implementation 29 | auto startTime = high_resolution_clock::now(); 30 | std::inclusive_scan(ints.cbegin(), ints.cend(), outs.begin()); 31 | auto endTime = high_resolution_clock::now(); 32 | print_results("sequential scan", startTime, endTime); 33 | 34 | // C++17 parallel implementation 35 | startTime = high_resolution_clock::now(); 36 | std::inclusive_scan(std::execution::par,ints.cbegin(), ints.cend(), outs.begin()); 37 | endTime = high_resolution_clock::now(); 38 | print_results("parallel scan", startTime, endTime); 39 | 40 | // Our parallel implementation 41 | startTime = high_resolution_clock::now(); 42 | parallel_partial_sum(ints.begin(), ints.end()); 43 | endTime = high_resolution_clock::now(); 44 | print_results("parallel scan manual", startTime, endTime); 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /s3_introduction_to_condition_variables/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | bool have_i_arrived = false; 9 | int distance_my_destination = 10; 10 | int distance_coverd = 0; 11 | 12 | bool keep_driving() 13 | { 14 | while (true) 15 | { 16 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 17 | distance_coverd++; 18 | } 19 | 20 | return false; 21 | } 22 | 23 | void keep_awake_all_night() 24 | { 25 | while (distance_coverd < distance_my_destination) 26 | { 27 | std::cout << "keep check, whether i am there \n"; 28 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 29 | } 30 | std::cout << "finally i am there, distance_coverd = " << distance_coverd << std::endl;; 31 | } 32 | 33 | void set_the_alarm_and_take_a_nap() 34 | { 35 | if (distance_coverd < distance_my_destination) 36 | { 37 | std::cout << "let me take a nap \n"; 38 | std::this_thread::sleep_for(std::chrono::milliseconds(10000)); 39 | } 40 | std::cout << "finally i am there, distance_coverd = " << distance_coverd << std::endl;; 41 | } 42 | 43 | int main() 44 | { 45 | std::thread driver_thread(keep_driving); 46 | std::thread keep_awake_all_night_thread(keep_awake_all_night); 47 | std::thread set_the_alarm_and_take_a_nap_thread(set_the_alarm_and_take_a_nap); 48 | 49 | keep_awake_all_night_thread.join(); 50 | set_the_alarm_and_take_a_nap_thread.join(); 51 | driver_thread.join(); 52 | } 53 | -------------------------------------------------------------------------------- /find/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "parallel_find.h" 9 | #include "common_objs.h" 10 | #include "utils.h" 11 | 12 | const size_t testSize = 100000000; 13 | 14 | using std::chrono::duration; 15 | using std::chrono::duration_cast; 16 | using std::chrono::high_resolution_clock; 17 | using std::milli; 18 | 19 | int main() 20 | { 21 | 22 | std::vector ints(testSize); 23 | for (size_t i = 0; i < testSize; i++) 24 | { 25 | ints[i] = i; 26 | } 27 | 28 | int looking_for = 50000000; 29 | 30 | auto startTime = high_resolution_clock::now(); 31 | auto value = parallel_find_pt(ints.begin(), ints.end(), looking_for); 32 | auto endTime = high_resolution_clock::now(); 33 | print_results("Parallel-package_task_impl :", startTime, endTime); 34 | 35 | startTime = high_resolution_clock::now(); 36 | std::find(ints.begin(), ints.end(), looking_for); 37 | endTime = high_resolution_clock::now(); 38 | print_results("STL sequntial :", startTime, endTime); 39 | 40 | startTime = high_resolution_clock::now(); 41 | std::find(std::execution::par,ints.begin(), ints.end(), looking_for); 42 | endTime = high_resolution_clock::now(); 43 | print_results("STL parallel-par :", startTime, endTime); 44 | 45 | startTime = high_resolution_clock::now(); 46 | std::find(std::execution::seq, ints.begin(), ints.end(), looking_for); 47 | endTime = high_resolution_clock::now(); 48 | print_results("STL parallel-seq :", startTime, endTime); 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /common/common_objs.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #pragma once 3 | 4 | #include 5 | #include 6 | 7 | class thread_guard { 8 | 9 | std::thread& t; 10 | 11 | public: 12 | explicit thread_guard(std::thread& _t) : t(_t) 13 | {} 14 | 15 | ~thread_guard() 16 | { 17 | if (t.joinable()) 18 | { 19 | t.join(); 20 | } 21 | } 22 | 23 | thread_guard(thread_guard& const) = delete; 24 | thread_guard& operator= (thread_guard& const) = delete; 25 | }; 26 | 27 | class join_threads { 28 | std::vector& threads; 29 | 30 | public: 31 | explicit join_threads(std::vector& _threads) : 32 | threads(_threads) 33 | {} 34 | 35 | ~join_threads() 36 | { 37 | for (long i = 0; i < threads.size(); i++) 38 | { 39 | if (threads[i].joinable()) 40 | threads[i].join(); 41 | } 42 | } 43 | 44 | }; 45 | 46 | class function_wrapper { 47 | struct impl_base { 48 | virtual void call() = 0; 49 | virtual ~impl_base() {} 50 | }; 51 | 52 | template 53 | struct impl_type : impl_base 54 | { 55 | F f; 56 | impl_type(F&& f_) : f(std::move(f_)) {} 57 | void call() { f(); } 58 | }; 59 | 60 | std::unique_ptr impl; 61 | 62 | public: 63 | template 64 | function_wrapper(F&& f) : 65 | impl(new impl_type(std::move(f))) 66 | {} 67 | 68 | void operator()() { impl->call(); } 69 | 70 | function_wrapper() 71 | {} 72 | 73 | function_wrapper(function_wrapper&& other) : 74 | impl(std::move(other.impl)) 75 | {} 76 | 77 | function_wrapper& operator=(function_wrapper&& other) 78 | { 79 | impl = std::move(other.impl); 80 | return *this; 81 | } 82 | 83 | function_wrapper(const function_wrapper&) = delete; 84 | function_wrapper(function_wrapper&) = delete; 85 | }; -------------------------------------------------------------------------------- /common/common_thread_safe_queue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | template 10 | class threadsafe_queue 11 | { 12 | private: 13 | mutable std::mutex mut; 14 | std::queue > data_queue; 15 | std::condition_variable data_cond; 16 | public: 17 | threadsafe_queue() 18 | {} 19 | 20 | void wait_and_pop(T& value) 21 | { 22 | std::unique_lock lk(mut); 23 | data_cond.wait(lk, [this] {return !data_queue.empty(); }); 24 | value = std::move(*data_queue.front()); 25 | data_queue.pop(); 26 | } 27 | 28 | bool try_pop(T& value) 29 | { 30 | std::lock_guard lk(mut); 31 | if (data_queue.empty()) 32 | return false; 33 | value = std::move(*data_queue.front()); 34 | data_queue.pop(); 35 | return true; 36 | } 37 | 38 | std::shared_ptr wait_and_pop() 39 | { 40 | std::unique_lock lk(mut); 41 | data_cond.wait(lk, [this] {return !data_queue.empty(); }); 42 | std::shared_ptr res = data_queue.front(); 43 | data_queue.pop(); 44 | return res; 45 | } 46 | 47 | std::shared_ptr try_pop() 48 | { 49 | std::lock_guard lk(mut); 50 | if (data_queue.empty()) 51 | return std::shared_ptr(); 52 | std::shared_ptr res = data_queue.front(); 53 | data_queue.pop(); 54 | return res; 55 | } 56 | 57 | bool empty() const 58 | { 59 | std::lock_guard lk(mut); 60 | return data_queue.empty(); 61 | } 62 | 63 | void push(T new_value) 64 | { 65 | std::shared_ptr data( 66 | std::make_shared(std::move(new_value))); 67 | std::lock_guard lk(mut); 68 | data_queue.push(data); 69 | data_cond.notify_one(); 70 | } 71 | 72 | }; -------------------------------------------------------------------------------- /s1_parallel_accumulate/parallel_accumulate.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | template 10 | void accumulate(iterator first, iterator last, T& val) 11 | { 12 | val = std::accumulate(first, last, val); 13 | } 14 | 15 | template 16 | void parallel_accumulate(iterator start, iterator end, T& ref) 17 | { 18 | unsigned MIN_BLOCK_SIZE = 1000; 19 | 20 | unsigned distance = std::distance(start, end); 21 | unsigned allowed_threads_by_elements = (distance + 1) / MIN_BLOCK_SIZE; 22 | unsigned allowed_threads_by_hardware = std::thread::hardware_concurrency(); 23 | 24 | // this is due to the fact that some operating system may 25 | // return 0 to indicate that this has not implemented 26 | if (allowed_threads_by_hardware < 1) 27 | allowed_threads_by_hardware = 2; 28 | 29 | unsigned allowed_threads = std::min(allowed_threads_by_elements, 30 | allowed_threads_by_hardware); 31 | 32 | //caculating the block size 33 | unsigned block_size = (distance + 1) / allowed_threads; 34 | 35 | std::vector results(allowed_threads); 36 | std::vector threads(allowed_threads - 1); 37 | 38 | //iterate and craeting new threads to calculate sum for each blocks 39 | iterator last; 40 | for (unsigned i = 0; i < allowed_threads - 1; i++) 41 | { 42 | last = start; 43 | std::advance(last, block_size); 44 | threads[i] = std::thread(accumulate, start, last, 45 | std::ref(results[i])); 46 | start = last; 47 | } 48 | 49 | //final block will be calculated from this thread 50 | results[allowed_threads - 1] = 51 | std::accumulate(start, end, results[allowed_threads - 1]); 52 | 53 | //calling join on the newly craeted threads 54 | for_each(threads.begin(), threads.end(), std::mem_fn(&std::thread::join)); 55 | ref = std::accumulate(results.begin(), results.end(), ref); 56 | } -------------------------------------------------------------------------------- /s3_thread_safe_queue_implementation/thread_safe_queue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | template 10 | class thread_safe_queue { 11 | std::mutex m; 12 | std::condition_variable cv; 13 | std::queue> queue; 14 | 15 | public: 16 | thread_safe_queue() 17 | {} 18 | 19 | thread_safe_queue(thread_safe_queue const& other_queue) 20 | { 21 | std::lock_guard lg(other_queue.m); 22 | queue = other_queue.queue; 23 | } 24 | 25 | void push(T& value) 26 | { 27 | std::lock_guard lg(m); 28 | queue.push(std::make_shared(value)); 29 | cv.notify_one(); 30 | } 31 | 32 | std::shared_ptr pop() 33 | { 34 | std::lock_guard lg(m); 35 | if (queue.empty()) 36 | { 37 | return std::shared_ptr(); 38 | } 39 | else 40 | { 41 | std::shared_ptr ref(queue.front()); 42 | queue.pop(); 43 | return ref; 44 | } 45 | } 46 | 47 | bool empty() 48 | { 49 | std::lock_guard lg(m); 50 | return queue.empty(); 51 | } 52 | 53 | std::shared_ptr wait_pop() 54 | { 55 | std::unique_lock lg(m); 56 | cv.wait(lg, [this] { 57 | return !queue.empty(); 58 | }); 59 | std::shared_ptr ref = queue.front(); 60 | queue.pop(); 61 | return ref; 62 | } 63 | 64 | size_t size() 65 | { 66 | std::lock_guard lg(m); 67 | return queue.size(); 68 | } 69 | 70 | bool wait_pop(T& ref) 71 | { 72 | std::unique_lock lg(m); 73 | cv.wait(lg, [this] { 74 | return !queue.empty(); 75 | }); 76 | 77 | ref = *(queue.front().get()); 78 | queue.pop(); 79 | return true; 80 | } 81 | 82 | bool pop(T& ref) 83 | { 84 | std::lock_guard lg(m); 85 | if (queue.empty()) 86 | { 87 | return false; 88 | } 89 | else 90 | { 91 | ref = queue.front(); 92 | queue.pop(); 93 | return true; 94 | } 95 | } 96 | }; -------------------------------------------------------------------------------- /matrix/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #include "utils.h" 11 | #include "matrix.h" 12 | 13 | const size_t testSize = 1'000; 14 | 15 | using std::chrono::duration; 16 | using std::chrono::duration_cast; 17 | using std::chrono::high_resolution_clock; 18 | using std::milli; 19 | 20 | void performance_test_multiply() 21 | { 22 | const int matrix_size = 1000; 23 | 24 | Matrix A(matrix_size, matrix_size); 25 | Matrix B(matrix_size, matrix_size); 26 | Matrix C(matrix_size, matrix_size); 27 | Matrix D(matrix_size, matrix_size); 28 | 29 | 30 | A.set_all(1); 31 | B.set_all(1); 32 | 33 | auto startTime = high_resolution_clock::now(); 34 | Matrix::multiply(&A, &B, &C); 35 | auto endTime = high_resolution_clock::now(); 36 | print_results("sequential matrix multiplication", startTime, endTime); 37 | 38 | startTime = high_resolution_clock::now(); 39 | Matrix::parallel_multiply(&A, &B, &D); 40 | endTime = high_resolution_clock::now(); 41 | print_results("parallel matrix multiplication", startTime, endTime); 42 | 43 | } 44 | 45 | void performance_test_transpose() 46 | { 47 | //Matrix A(5, 8); 48 | //A.set_all(1); 49 | //A.set_value(3, 4, 5); 50 | //A.print(); 51 | 52 | //Matrix B(8, 5); 53 | //Matrix::transpose(&A, &B); 54 | //B.print(); 55 | 56 | const int matrix_size = 10000; 57 | 58 | Matrix A(matrix_size, matrix_size); 59 | Matrix B(matrix_size, matrix_size); 60 | 61 | A.set_all(1); 62 | 63 | auto startTime = high_resolution_clock::now(); 64 | Matrix::transpose(&A, &B); 65 | auto endTime = high_resolution_clock::now(); 66 | print_results("sequential matrix transpose", startTime, endTime); 67 | 68 | startTime = high_resolution_clock::now(); 69 | Matrix::parallel_transpose(&A, &B); 70 | endTime = high_resolution_clock::now(); 71 | print_results("parallel matrix transpose", startTime, endTime); 72 | } 73 | 74 | int main() 75 | { 76 | performance_test_transpose(); 77 | return 0; 78 | } -------------------------------------------------------------------------------- /for_each/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "parallel_for_each.h" 6 | #include "utils.h" 7 | 8 | const size_t testSize = 1000; 9 | 10 | using std::chrono::duration; 11 | using std::chrono::duration_cast; 12 | using std::chrono::high_resolution_clock; 13 | using std::milli; 14 | 15 | int main() 16 | { 17 | std::vector ints(testSize); 18 | for (auto& i : ints) { 19 | i = 1; 20 | } 21 | 22 | auto long_function = []( const int& n ) 23 | { 24 | int sum = 0; 25 | for (auto i = 0; i < 100000; i++) 26 | { 27 | sum += 1*(i-499); 28 | } 29 | }; 30 | 31 | auto startTime = high_resolution_clock::now(); 32 | std::for_each(ints.cbegin(), ints.cend(), long_function); 33 | auto endTime = high_resolution_clock::now(); 34 | print_results("STL ", startTime, endTime); 35 | 36 | startTime = high_resolution_clock::now(); 37 | for_each(std::execution::seq, ints.cbegin(), ints.cend(), long_function); 38 | endTime = high_resolution_clock::now(); 39 | print_results("STL-seq ", startTime, endTime); 40 | 41 | startTime = high_resolution_clock::now(); 42 | std::for_each(std::execution::par, ints.cbegin(), ints.cend(), long_function); 43 | endTime = high_resolution_clock::now(); 44 | print_results("STL-par ", startTime, endTime); 45 | 46 | startTime = high_resolution_clock::now(); 47 | parallel_for_each_pt(ints.cbegin(), ints.cend(), long_function); 48 | endTime = high_resolution_clock::now(); 49 | print_results("Parallel-package_task ", startTime, endTime); 50 | 51 | startTime = high_resolution_clock::now(); 52 | parallel_for_each_async(ints.cbegin(), ints.cend(), long_function); 53 | endTime = high_resolution_clock::now(); 54 | print_results("Parallel-async ", startTime, endTime); 55 | 56 | std::cout << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl << std::endl; 57 | 58 | return 0; 59 | } -------------------------------------------------------------------------------- /parallel_stl/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | 11 | using std::chrono::duration; 12 | using std::chrono::duration_cast; 13 | using std::chrono::high_resolution_clock; 14 | using std::milli; 15 | using std::random_device; 16 | using std::sort; 17 | using std::vector; 18 | 19 | const size_t testSize = 1'000'000; 20 | const int iterationCount = 5; 21 | 22 | void print_results(const char* const tag, const vector& sorted, 23 | high_resolution_clock::time_point startTime, 24 | high_resolution_clock::time_point endTime) { 25 | printf("%s: Lowest: %g Highest: %g Time: %fms\n", tag, sorted.front(), 26 | sorted.back(), 27 | duration_cast>(endTime - startTime).count()); 28 | } 29 | 30 | int main() { 31 | random_device rd; 32 | 33 | // generate some random doubles: 34 | printf("Testing with %zu doubles...\n", testSize); 35 | vector doubles(testSize); 36 | for (auto& d : doubles) { 37 | d = static_cast(rd()); 38 | } 39 | 40 | // time how long it takes to sort them: 41 | for (int i = 0; i < iterationCount; ++i) 42 | { 43 | vector sorted(doubles); 44 | const auto startTime = high_resolution_clock::now(); 45 | sort(sorted.begin(), sorted.end()); 46 | const auto endTime = high_resolution_clock::now(); 47 | print_results("Serial", sorted, startTime, endTime); 48 | } 49 | 50 | for (int i = 0; i < iterationCount; ++i) 51 | { 52 | vector sorted(doubles); 53 | const auto startTime = high_resolution_clock::now(); 54 | // same sort call as above, but with par_unseq: 55 | std::sort(std::execution::seq, sorted.begin(), sorted.end()); 56 | const auto endTime = high_resolution_clock::now(); 57 | // in our output, note that these are the parallel results: 58 | print_results("Parallel", sorted, startTime, endTime); 59 | } 60 | } -------------------------------------------------------------------------------- /parallel_stl_intro/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include 9 | 10 | using std::chrono::duration; 11 | using std::chrono::duration_cast; 12 | using std::chrono::high_resolution_clock; 13 | using std::milli; 14 | using std::random_device; 15 | using std::sort; 16 | using std::vector; 17 | 18 | const size_t testSize = 1'000'000; 19 | const int iterationCount = 5; 20 | 21 | void print_results(const char* const tag, const vector& sorted, 22 | high_resolution_clock::time_point startTime, 23 | high_resolution_clock::time_point endTime) { 24 | printf("%s: Lowest: %g Highest: %g Time: %fms\n", tag, sorted.front(), 25 | sorted.back(), 26 | duration_cast>(endTime - startTime).count()); 27 | } 28 | 29 | int main() { 30 | random_device rd; 31 | 32 | // generate some random doubles: 33 | printf("Testing with %zu doubles...\n", testSize); 34 | vector doubles(testSize); 35 | for (auto& d : doubles) { 36 | d = static_cast(rd()); 37 | } 38 | 39 | // time how long it takes to sort them: 40 | for (int i = 0; i < iterationCount; ++i) 41 | { 42 | vector sorted(doubles); 43 | const auto startTime = high_resolution_clock::now(); 44 | sort(sorted.begin(), sorted.end()); 45 | const auto endTime = high_resolution_clock::now(); 46 | print_results("Serial STL ", sorted, startTime, endTime); 47 | } 48 | 49 | for (int i = 0; i < iterationCount; ++i) 50 | { 51 | vector sorted(doubles); 52 | const auto startTime = high_resolution_clock::now(); 53 | // same sort call as above, but with par_unseq: 54 | std::sort(std::execution::par, sorted.begin(), sorted.end()); 55 | const auto endTime = high_resolution_clock::now(); 56 | // in our output, note that these are the parallel results: 57 | print_results("Parallel STL", sorted, startTime, endTime); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /interruptible_thread/main.cpp: -------------------------------------------------------------------------------- 1 | // Type your code here, or load an example. 2 | #include 3 | 4 | #include 5 | #include 6 | #include "jthread.h" 7 | // 8 | using namespace::std::literals; 9 | // 10 | //void do_something(std::stop_token token) 11 | //{ 12 | // int counter{ 0 }; 13 | // while (counter < 10) 14 | // { 15 | // if (token.stop_requested()) 16 | // { 17 | // return; 18 | // } 19 | // std::this_thread::sleep_for(0.2s); 20 | // std::cerr << "This is interruptible thread : " << counter << std::endl; 21 | // ++counter; 22 | // } 23 | //} 24 | // 25 | //void do_something_else() 26 | //{ 27 | // int counter{ 0 }; 28 | // while (counter < 10) 29 | // { 30 | // std::this_thread::sleep_for(0.2s); 31 | // std::cerr << "This is non-interruptible thread : " << counter << std::endl; 32 | // ++counter; 33 | // } 34 | //} 35 | // 36 | //int main() { 37 | // 38 | // std::cout << std::endl; 39 | // std::jthread nonInterruptable(do_something_else); 40 | // std::jthread interruptible(do_something); 41 | // 42 | // std::this_thread::sleep_for(1.0s); 43 | // interruptible.request_stop(); 44 | // nonInterruptable.request_stop(); 45 | // 46 | // std::cout << std::endl; 47 | // 48 | //} 49 | 50 | void do_something() 51 | { 52 | int counter{ 0 }; 53 | while (counter < 10) 54 | { 55 | if (interrupt_point()) 56 | { 57 | return; 58 | } 59 | std::this_thread::sleep_for(0.2s); 60 | std::cerr << "This is interruptible thread : " << counter << std::endl; 61 | ++counter; 62 | } 63 | } 64 | 65 | void do_something_else() 66 | { 67 | int counter{ 0 }; 68 | while (counter < 10) 69 | { 70 | std::this_thread::sleep_for(0.2s); 71 | std::cerr << "This is non-interruptible thread : " << counter << std::endl; 72 | ++counter; 73 | } 74 | } 75 | 76 | int main() 77 | { 78 | 79 | std::cout << std::endl; 80 | jthread_local nonInterruptable(do_something_else); 81 | jthread_local interruptible(do_something); 82 | 83 | std::this_thread::sleep_for(1.0s); 84 | interruptible.interrupt(); 85 | nonInterruptable.interrupt(); 86 | 87 | //std::cout << std::endl; 88 | return 0; 89 | } -------------------------------------------------------------------------------- /s2_unique_locks/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | 7 | /***************************************************Example 1 ******************************************/ 8 | class bank_account { 9 | double balance; 10 | std::string name; 11 | std::mutex m; 12 | 13 | public: 14 | bank_account() {}; 15 | 16 | bank_account(double _balance, std::string _name) :balance(_balance), name(_name) {} 17 | 18 | bank_account(bank_account& const) = delete; 19 | bank_account& operator=(bank_account& const) = delete; 20 | 21 | void withdraw(double amount) 22 | { 23 | std::lock_guard lg(m); 24 | balance += amount; 25 | } 26 | 27 | void deposite(double amount) 28 | { 29 | std::lock_guard lg(m); 30 | balance += amount; 31 | } 32 | 33 | void transfer(bank_account& from, bank_account& to, double amount) 34 | { 35 | 36 | std::cout << std::this_thread::get_id() << " hold the lock for both mutex \n"; 37 | 38 | std::unique_lock ul_1(from.m, std::defer_lock); 39 | std::unique_lock ul_2(to.m, std::defer_lock); 40 | std::lock(ul_1, ul_2); 41 | 42 | from.balance -= amount; 43 | to.balance += amount; 44 | std::cout << "transfer to - " << to.name << " from - " << from.name << " end \n"; 45 | } 46 | }; 47 | 48 | void run_code1() 49 | { 50 | 51 | bank_account account; 52 | 53 | bank_account account_1(1000, "james"); 54 | bank_account account_2(2000, "Mathew"); 55 | 56 | std::thread thread_1(&bank_account::transfer, &account, std::ref(account_1), std::ref(account_2), 500); 57 | std::thread thread_2(&bank_account::transfer, &account, std::ref(account_2), std::ref(account_1), 200); 58 | 59 | thread_1.join(); 60 | thread_2.join(); 61 | } 62 | 63 | 64 | 65 | /***************************************************Example 2 ******************************************/ 66 | 67 | void x_operations() 68 | { 69 | std::cout << "this is some operations \n"; 70 | } 71 | 72 | void y_operations() 73 | { 74 | std::cout << "this is some other operations \n"; 75 | } 76 | 77 | std::unique_lock get_lock() 78 | { 79 | std::mutex m; 80 | std::unique_lock lk(m); 81 | x_operations(); 82 | return lk; 83 | } 84 | 85 | void run_code2() 86 | { 87 | std::unique_lock lk(get_lock()); 88 | y_operations(); 89 | } 90 | 91 | int main() 92 | { 93 | run_code1(); 94 | run_code2(); 95 | 96 | return 0; 97 | } -------------------------------------------------------------------------------- /sort/parallel_quick_sort.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | template 9 | std::list parallel_quick_sort(std::list input) 10 | { 11 | //std::cout << std::this_thread::get_id() << std::endl; 12 | if (input.size() < 2) 13 | { 14 | return input; 15 | } 16 | 17 | //move frist element in the list to result list and take it as pivot value 18 | std::list result; 19 | result.splice(result.begin(), input, input.begin()); 20 | T pivot = *result.begin(); 21 | 22 | //partition the input array 23 | auto divide_point = std::partition(input.begin(), input.end(), [&](T const& t) 24 | { 25 | return t < pivot; 26 | }); 27 | 28 | //move lower part of the list to separate list so that we can make recursive call 29 | std::list lower_list; 30 | lower_list.splice(lower_list.end(), input, input.begin(), divide_point); 31 | 32 | auto new_lower(parallel_quick_sort(std::move(lower_list))); 33 | //auto new_upper(sequential_quick_sort(std::move(input))); 34 | std::future> new_upper_future 35 | (std::async(¶llel_quick_sort, std::move(input))); 36 | 37 | result.splice(result.begin(), new_lower); 38 | result.splice(result.end(), new_upper_future.get()); 39 | return result; 40 | } 41 | 42 | 43 | template 44 | std::list sequential_quick_sort(std::list input) 45 | { 46 | //recursive condition 47 | if ( input.size() < 2 ) 48 | { 49 | return input; 50 | } 51 | 52 | //move frist element in the list to result list and take it as pivot value 53 | std::list result; 54 | result.splice(result.begin(), input, input.begin()); 55 | T pivot = *result.begin(); 56 | 57 | //partition the input array so that t< pivot in lower part and t> pivot in upper part of input list 58 | auto divide_point = std::partition(input.begin(), input.end(), 59 | [&](T const& t) 60 | { 61 | return t < pivot; 62 | }); 63 | 64 | //move lower part of the list to separate list so that we can make recursive call 65 | std::list lower_list; 66 | lower_list.splice(lower_list.end(), input, input.begin(), divide_point); 67 | 68 | //call the sequenctial_quick_sort recursively 69 | auto new_lower(sequential_quick_sort(std::move(lower_list))); 70 | auto new_upper(sequential_quick_sort(std::move(input))); 71 | 72 | //transfer all elements in to result list 73 | result.splice(result.begin(), new_lower); 74 | result.splice(result.end(), new_upper); 75 | 76 | return result; 77 | } -------------------------------------------------------------------------------- /for_each/parallel_for_each.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | #include "common_objs.h" 8 | 9 | 10 | /* This is the parallel version of for_each function implmentation with package tasks and futures */ 11 | template 12 | void parallel_for_each_pt( Iterator first, Iterator last, Func f ) 13 | { 14 | unsigned long const length = std::distance(first, last); 15 | 16 | if (!length) 17 | return; 18 | 19 | /* Calculate the optimized number of threads to run the algorithm */ 20 | 21 | unsigned long const min_per_thread = 25; 22 | unsigned long const max_threads = (length + min_per_thread - 1) / min_per_thread; 23 | 24 | unsigned long const hardware_threads = std::thread::hardware_concurrency(); 25 | unsigned long const num_threads = std::min(hardware_threads != 0 ? hardware_threads : 2, max_threads); 26 | unsigned long const block_size = length / num_threads; 27 | 28 | /* Declare the needed data structures */ 29 | 30 | std::vector> futures(num_threads - 1); 31 | std::vector threads(num_threads - 1); 32 | join_threads joiner(threads); 33 | 34 | /* Partition of data between threads */ 35 | 36 | Iterator block_start = first; 37 | for ( unsigned long i =0; i <= ( num_threads -2 ); i++ ) 38 | { 39 | Iterator block_end = block_start; 40 | std::advance(block_end, block_size); 41 | 42 | std::packaged_task task( 43 | [=]() 44 | { 45 | std::for_each(block_start, block_end, f); 46 | } 47 | ); 48 | 49 | futures[i] = task.get_future(); 50 | threads[i] = std::thread(std::move(task)); 51 | 52 | block_start = block_end; 53 | } 54 | 55 | // call the function for last block from this thread 56 | std::for_each(block_start, last, f); 57 | 58 | /* wait until futures are ready */ 59 | for (unsigned long i = 0; i < (num_threads - 1); ++i) 60 | futures[i].get(); 61 | 62 | } 63 | 64 | /* This is the parallel version of for_each function implmentation with std::async */ 65 | template 66 | void parallel_for_each_async(Iterator first, Iterator last, Func f) 67 | { 68 | unsigned long const length = std::distance(first,last); 69 | 70 | if (!length) 71 | return; 72 | 73 | unsigned long const min_per_thread = 25; 74 | 75 | if (length < 2 * min_per_thread) 76 | { 77 | std::for_each(first, last, f); 78 | } 79 | else 80 | { 81 | Iterator const mid_point = first + length / 2; 82 | std::future first_half = 83 | std::async(¶llel_for_each_async, first, mid_point, f); 84 | 85 | parallel_for_each_async(mid_point, last, f); 86 | first_half.get(); 87 | } 88 | 89 | } -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /parallel_stl/matrix_multiplication.cpp: -------------------------------------------------------------------------------- 1 | //#include 2 | //#include 3 | //#include 4 | // 5 | //using namespace std::experimental::D4087; 6 | // 7 | //template 8 | //void measure_time(F&& f, const char* name) 9 | //{ 10 | // using namespace std::chrono; 11 | // 12 | // auto begin = high_resolution_clock::now(); 13 | // f(); 14 | // auto end = high_resolution_clock::now(); 15 | // 16 | // printf("%s %llu.%03llu seconds\n", name, 17 | // duration_cast(end - begin).count(), 18 | // duration_cast(end - begin).count() % 1000); 19 | //} 20 | // 21 | //void multiply_element(const index<2>& idx, const std::vector& vA, const std::vector& vB, std::vector& vResult, int N) 22 | //{ 23 | // auto row = idx[0]; 24 | // auto col = idx[1]; 25 | // 26 | // double sum = 0; 27 | // 28 | // for (auto i = 0; i < N; i++) 29 | // sum += vA[row * N + i] * vB[i * N + col]; 30 | // 31 | // vResult[row * N + col] = sum; 32 | //} 33 | // 34 | //void test_matrix_multiplication(int N) 35 | //{ 36 | // // Create a random number generator. 37 | // std::random_device rd; 38 | // std::mt19937 generator(rd()); 39 | // 40 | // printf("\nTesting matrix multiplication of %d X %d doubles:\n", N, N); 41 | // 42 | // std::vector vA(N * N); 43 | // std::vector vB(N * N); 44 | // 45 | // std::uniform_real_distribution dist(-100, 100); 46 | // 47 | // // Generate random numbers 48 | // std::generate(std::begin(vA), std::end(vA), [&generator, &dist]() { return dist(generator); }); //double distribution 49 | // std::generate(std::begin(vB), std::end(vB), [&generator, &dist]() { return dist(generator); }); 50 | // 51 | // measure_time([&]() mutable 52 | // { 53 | // bounds<2> bnd{ N, N }; 54 | // std::vector vResult(bnd.size()); 55 | // 56 | // // Using serial implementation 57 | // std::for_each(std::begin(bnd), std::end(bnd), [&](index<2> idx) { 58 | // multiply_element(idx, vA, vB, vResult, N); 59 | // }); 60 | // }, "serial: "); 61 | // 62 | // measure_time([&]() mutable 63 | // { 64 | // bounds<2> bnd{ N, N }; 65 | // std::vector vResult(bnd.size()); 66 | // 67 | // // Using Parallel STL implementation 68 | // std::experimental::parallel::for_each(std::experimental::parallel::par, std::begin(bnd), std::end(bnd), [&](index<2> idx) { 69 | // multiply_element(idx, vA, vB, vResult, N); 70 | // }); 71 | // }, "parallel STL: "); 72 | //} 73 | // 74 | //int _tmain(int /* argc */, _TCHAR* /* argv */[]) 75 | //{ 76 | // test_matrix_multiplication(100); 77 | // test_matrix_multiplication(200); 78 | // test_matrix_multiplication(500); 79 | // test_matrix_multiplication(750); 80 | // test_matrix_multiplication(1000); 81 | // return 0; 82 | //} 83 | // 84 | -------------------------------------------------------------------------------- /HashTableX/hash_table_x.cpp: -------------------------------------------------------------------------------- 1 | #include "hash_table_x.h" 2 | #include 3 | #include 4 | 5 | 6 | #define USE_FAST_SETITEM 0 7 | 8 | 9 | //---------------------------------------------- 10 | // from code.google.com/p/smhasher/wiki/MurmurHash3 11 | inline static int integerHash(unsigned int h) 12 | { 13 | h ^= h >> 16; 14 | h *= 0x85ebca6b; 15 | h ^= h >> 13; 16 | h *= 0xc2b2ae35; 17 | h ^= h >> 16; 18 | return h; 19 | } 20 | 21 | 22 | //---------------------------------------------- 23 | parallel_hash_table::parallel_hash_table(unsigned int arraySize) 24 | { 25 | // Initialize cells 26 | assert((arraySize & (arraySize - 1)) == 0); // Must be a power of 2 27 | m_arraySize = arraySize; 28 | m_entries = new entry[arraySize]; 29 | Clear(); 30 | } 31 | 32 | 33 | //---------------------------------------------- 34 | parallel_hash_table::~parallel_hash_table() 35 | { 36 | // Delete cells 37 | delete[] m_entries; 38 | } 39 | 40 | void parallel_hash_table::SetItem(unsigned int key, unsigned int value) 41 | { 42 | assert(key != 0); 43 | assert(value != 0); 44 | 45 | for (uint32_t idx = integerHash(key);; idx++) 46 | { 47 | idx &= m_arraySize - 1; 48 | unsigned int prevKey = 0; 49 | 50 | //int prevKey = std::atomic_compare_exchange_strong(&m_entries[idx].key, 0, key); 51 | m_entries[idx].key.compare_exchange_strong(prevKey, key, std::memory_order_relaxed); 52 | 53 | if ((prevKey == 0) || (prevKey == key)) 54 | { 55 | m_entries[idx].value.store(value, std::memory_order_relaxed); 56 | return; 57 | } 58 | } 59 | } 60 | 61 | //---------------------------------------------- 62 | unsigned int parallel_hash_table::GetItem(unsigned int key) 63 | { 64 | assert(key != 0); 65 | 66 | for (uint32_t idx = integerHash(key);; idx++) 67 | { 68 | idx &= m_arraySize - 1; 69 | 70 | //uint32_t probedKey = mint_load_32_relaxed(&m_entries[idx].key); 71 | int probedKey = m_entries[idx].key.load(std::memory_order_relaxed); 72 | if (probedKey == key) 73 | //return mint_load_32_relaxed(&m_entries[idx].value); 74 | return m_entries[idx].value.load(std::memory_order_relaxed); 75 | if (probedKey == 0) 76 | return 0; 77 | } 78 | } 79 | 80 | 81 | //---------------------------------------------- 82 | unsigned int parallel_hash_table::GetItemCount() 83 | { 84 | uint32_t itemCount = 0; 85 | for (uint32_t idx = 0; idx < m_arraySize; idx++) 86 | { 87 | if ((m_entries[idx].key.load(std::memory_order_relaxed) != 0) 88 | && (m_entries[idx].value.load(std::memory_order_relaxed) != 0)) 89 | itemCount++; 90 | } 91 | return itemCount; 92 | } 93 | 94 | 95 | //---------------------------------------------- 96 | void parallel_hash_table::Clear() 97 | { 98 | memset(m_entries, 0, sizeof(entry) * m_arraySize); 99 | } -------------------------------------------------------------------------------- /s2_dead_locks/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | /*********************************************** example 1 ***********************************/ 8 | class bank_account { 9 | double balance; 10 | std::string name; 11 | std::mutex m; 12 | 13 | 14 | public: 15 | bank_account() {}; 16 | 17 | bank_account(double _balance, std::string _name) :balance(_balance), name(_name) {} 18 | 19 | bank_account(bank_account& const) = delete; 20 | bank_account& operator=(bank_account& const) = delete; 21 | 22 | void withdraw(double amount) 23 | { 24 | std::lock_guard lg(m); 25 | balance -= amount; 26 | } 27 | 28 | void deposite(double amount) 29 | { 30 | std::lock_guard lg(m); 31 | balance += amount; 32 | } 33 | 34 | void transfer(bank_account& from, bank_account& to, double amount) 35 | { 36 | std::lock_guard lg_1(from.m); 37 | std::cout << "lock for " << from.name << " account acquire by " << std::this_thread::get_id() << std::endl; 38 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 39 | 40 | std::cout << "waiting to acquire lock for " << to.name << " account by " << std::this_thread::get_id() << std::endl; 41 | std::lock_guard lg_2(to.m); 42 | 43 | from.balance -= amount; 44 | to.balance += amount; 45 | std::cout << "transfer to - " << to.name << " from - " << from.name << " end \n"; 46 | } 47 | 48 | }; 49 | 50 | void run_code1() 51 | { 52 | bank_account account; 53 | 54 | bank_account account_1(1000, "james"); 55 | bank_account account_2(2000, "Mathew"); 56 | 57 | std::thread thread_1(&bank_account::transfer, &account, std::ref(account_1), std::ref(account_2), 500); 58 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); 59 | std::thread thread_2(&bank_account::transfer, &account, std::ref(account_2), std::ref(account_1), 200); 60 | 61 | thread_1.join(); 62 | thread_2.join(); 63 | } 64 | 65 | 66 | 67 | /*********************************************** example 2 ***********************************/ 68 | std::mutex m1; 69 | std::mutex m2; 70 | 71 | 72 | void m1_frist_m2_second() 73 | { 74 | std::lock_guard lg1(m1); 75 | std::this_thread::sleep_for(std::chrono::milliseconds(1000)); 76 | std::cout << "thread " << std::this_thread::get_id() << " has acquired lock for m1 mutex, its wait for m2 \n"; 77 | std::lock_guardlg2(m2); 78 | std::cout << "thread " << std::this_thread::get_id() << " has acquired lock for m2 mutex \n"; 79 | } 80 | 81 | 82 | void m2_frist_m1_second() 83 | { 84 | std::lock_guard lg1(m2); 85 | std::this_thread::sleep_for(std::chrono::milliseconds(1500)); 86 | std::cout << "thread " << std::this_thread::get_id() << " has acquired lock for m2 mutex, its wait for m1 \n"; 87 | std::lock_guardlg2(m1); 88 | std::cout << "thread " << std::this_thread::get_id() << " has acquired lock for m1 mutex \n"; 89 | } 90 | 91 | void run_code2() 92 | { 93 | std::thread thread_1(m1_frist_m2_second); 94 | std::thread thread_2(m2_frist_m1_second); 95 | 96 | thread_1.join(); 97 | thread_2.join(); 98 | } 99 | 100 | 101 | int main() 102 | { 103 | run_code1(); 104 | run_code2(); 105 | 106 | return 0; 107 | } 108 | -------------------------------------------------------------------------------- /s8_simple_threadpool_waiting/simple_thread_pool_with_waiting.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "common_thread_safe_queue.h" 10 | #include "common_objs.h" 11 | 12 | #include "utils.h" 13 | 14 | class thread_pool_waiting { 15 | 16 | std::atomic_bool done; 17 | threadsafe_queue< function_wrapper> work_queue; 18 | std::vector threads; 19 | join_threads joiner; 20 | 21 | void worker_thread() 22 | { 23 | while (!done) 24 | { 25 | function_wrapper task; 26 | if (work_queue.try_pop(task)) 27 | { 28 | task(); 29 | } 30 | else 31 | { 32 | std::this_thread::yield(); 33 | } 34 | } 35 | } 36 | 37 | public: 38 | thread_pool_waiting() :done(false), joiner(threads) 39 | { 40 | int const thread_count = std::thread::hardware_concurrency(); 41 | 42 | try 43 | { 44 | for (int i = 0; i < thread_count; ++i) 45 | { 46 | threads.push_back( 47 | std::thread(&thread_pool_waiting::worker_thread, this)); 48 | } 49 | } 50 | catch (...) 51 | { 52 | done = true; 53 | throw; 54 | } 55 | } 56 | 57 | ~thread_pool_waiting() 58 | { 59 | done = true; 60 | } 61 | 62 | template 63 | std::future::type> 64 | submit(FunctionType f) 65 | { 66 | typedef typename std::result_of::type result_type; 67 | std::packaged_task task(std::move(f)); 68 | std::future res(task.get_future()); 69 | work_queue.push(std::move(task)); 70 | return res; 71 | } 72 | }; 73 | 74 | template 75 | struct accumulate_block 76 | { 77 | T operator()(Iterator first, Iterator last) 78 | { 79 | T value = std::accumulate(first, last, T()); 80 | printf(" %d - %d \n", std::this_thread::get_id(), value); 81 | return value; 82 | } 83 | }; 84 | 85 | template 86 | T parallel_accumulate(Iterator first, Iterator last, T init) 87 | { 88 | unsigned long const length = std::distance(first, last); 89 | thread_pool_waiting pool; 90 | 91 | if (!length) 92 | return init; 93 | 94 | unsigned long const min_per_thread = 25; 95 | unsigned long const max_threads = 96 | (length + min_per_thread - 1) / min_per_thread; 97 | 98 | unsigned long const hardware_threads = 99 | std::thread::hardware_concurrency(); 100 | 101 | unsigned long const num_threads = 102 | std::min(hardware_threads != 0 ? hardware_threads : 2, max_threads); 103 | 104 | unsigned long const block_size = length / num_threads; 105 | 106 | std::vector > futures(num_threads - 1); 107 | 108 | Iterator block_start = first; 109 | for (unsigned long i = 0; i < (num_threads - 1); ++i) 110 | { 111 | Iterator block_end = block_start; 112 | std::advance(block_end, block_size); 113 | futures[i] = pool.submit(std::bind(accumulate_block(), block_start, block_end)); 114 | block_start = block_end; 115 | } 116 | T last_result = accumulate_block()(block_start, last); 117 | 118 | T result = init; 119 | for (unsigned long i = 0; i < (num_threads - 1); ++i) 120 | { 121 | result += futures[i].get(); 122 | } 123 | result += last_result; 124 | return result; 125 | } 126 | 127 | 128 | -------------------------------------------------------------------------------- /s8_simple_threadpool_with_local_work_queue/simple_threadpool_with_local_work_queue.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "common_thread_safe_queue.h" 10 | #include "common_objs.h" 11 | 12 | #include "utils.h" 13 | 14 | 15 | class thread_pool_waiting_other_tasks { 16 | 17 | std::atomic_bool done; 18 | threadsafe_queue< function_wrapper> work_queue; 19 | std::vector threads; 20 | join_threads joiner; 21 | 22 | void worker_thread() 23 | { 24 | while (!done) 25 | { 26 | function_wrapper task; 27 | if (work_queue.try_pop(task)) 28 | { 29 | task(); 30 | } 31 | else 32 | { 33 | std::this_thread::yield(); 34 | } 35 | } 36 | } 37 | 38 | public: 39 | thread_pool_waiting_other_tasks() :done(false), joiner(threads) 40 | { 41 | int const thread_count = std::thread::hardware_concurrency(); 42 | 43 | try 44 | { 45 | for (int i = 0; i < thread_count; ++i) 46 | { 47 | threads.push_back( 48 | std::thread(&thread_pool_waiting_other_tasks::worker_thread, this)); 49 | } 50 | } 51 | catch (...) 52 | { 53 | done = true; 54 | throw; 55 | } 56 | } 57 | 58 | ~thread_pool_waiting_other_tasks() 59 | { 60 | done = true; 61 | } 62 | 63 | template 64 | std::future::type> 65 | submit(FunctionType f) 66 | { 67 | typedef typename std::result_of::type result_type; 68 | 69 | std::packaged_task task(std::move(f)); 70 | std::future res(task.get_future()); 71 | work_queue.push(std::move(task)); 72 | return res; 73 | } 74 | 75 | void run_pending_task() 76 | { 77 | function_wrapper task; 78 | if (work_queue.try_pop(task)) 79 | { 80 | task(); 81 | } 82 | else 83 | { 84 | std::this_thread::yield(); 85 | } 86 | } 87 | 88 | }; 89 | 90 | template 91 | struct sorter { 92 | 93 | thread_pool_waiting_other_tasks pool; 94 | 95 | std::list do_sort(std::list& chunk_data) 96 | { 97 | if (chunk_data.size() < 2) 98 | return chunk_data; 99 | 100 | std::list result; 101 | result.splice(result.begin(), chunk_data, chunk_data.begin()); 102 | T const& partition_val = *result.begin(); 103 | 104 | typename std::list::iterator divide_point = std::partition(chunk_data.begin(), 105 | chunk_data.end(), [&](T const& val) 106 | { 107 | return val < partition_val; 108 | }); 109 | 110 | std::list new_lower_chunk; 111 | new_lower_chunk.splice(new_lower_chunk.end(), chunk_data, 112 | chunk_data.begin(), divide_point); 113 | 114 | std::future> new_lower = 115 | pool.submit(std::bind(&sorter::do_sort, this, std::move(new_lower_chunk))); 116 | 117 | std::list new_higher(do_sort(chunk_data)); 118 | 119 | result.splice(result.end(), new_higher); 120 | 121 | //while(new_lower.wait_for(std::chrono::seconds(0))== std::future_status::timeout) 122 | while (!new_lower._Is_ready()) 123 | { 124 | pool.run_pending_task(); 125 | } 126 | 127 | result.splice(result.begin(), new_lower.get()); 128 | 129 | return result; 130 | 131 | } 132 | 133 | }; 134 | 135 | template 136 | std::list parallel_quick_sort(std::list input) 137 | { 138 | if (input.empty()) 139 | { 140 | return input; 141 | } 142 | 143 | sorter s; 144 | return s.do_sort(input); 145 | } -------------------------------------------------------------------------------- /s8_simple_threadpool_waiting_for_others/simple_threadpool_waiting_for_other_tasks.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include "common_thread_safe_queue.h" 11 | #include "common_objs.h" 12 | 13 | #include "utils.h" 14 | 15 | 16 | class thread_pool_waiting_other_tasks { 17 | 18 | std::atomic_bool done; 19 | threadsafe_queue< function_wrapper> work_queue; 20 | std::vector threads; 21 | join_threads joiner; 22 | 23 | void worker_thread() 24 | { 25 | while (!done) 26 | { 27 | function_wrapper task; 28 | if (work_queue.try_pop(task)) 29 | { 30 | task(); 31 | } 32 | else 33 | { 34 | std::this_thread::yield(); 35 | } 36 | } 37 | } 38 | 39 | public: 40 | thread_pool_waiting_other_tasks() :done(false), joiner(threads) 41 | { 42 | int const thread_count = std::thread::hardware_concurrency(); 43 | 44 | try 45 | { 46 | for (int i = 0; i < thread_count; ++i) 47 | { 48 | threads.push_back( 49 | std::thread(&thread_pool_waiting_other_tasks::worker_thread, this)); 50 | } 51 | } 52 | catch (...) 53 | { 54 | done = true; 55 | throw; 56 | } 57 | } 58 | 59 | ~thread_pool_waiting_other_tasks() 60 | { 61 | done = true; 62 | } 63 | 64 | template 65 | std::future::type> 66 | submit(FunctionType f) 67 | { 68 | typedef typename std::result_of::type result_type; 69 | 70 | std::packaged_task task(std::move(f)); 71 | std::future res(task.get_future()); 72 | work_queue.push(std::move(task)); 73 | return res; 74 | } 75 | 76 | void run_pending_task() 77 | { 78 | function_wrapper task; 79 | if (work_queue.try_pop(task)) 80 | { 81 | task(); 82 | } 83 | else 84 | { 85 | std::this_thread::yield(); 86 | } 87 | } 88 | 89 | }; 90 | 91 | template 92 | struct sorter { 93 | 94 | thread_pool_waiting_other_tasks pool; 95 | 96 | std::list do_sort(std::list& chunk_data) 97 | { 98 | if (chunk_data.size() < 2) 99 | return chunk_data; 100 | 101 | std::list result; 102 | result.splice(result.begin(), chunk_data, chunk_data.begin()); 103 | T const& partition_val = *result.begin(); 104 | 105 | typename std::list::iterator divide_point = std::partition(chunk_data.begin(), 106 | chunk_data.end(), [&](T const& val) 107 | { 108 | return val < partition_val; 109 | }); 110 | 111 | std::list new_lower_chunk; 112 | new_lower_chunk.splice(new_lower_chunk.end(), chunk_data, 113 | chunk_data.begin(), divide_point); 114 | 115 | std::future> new_lower = 116 | pool.submit(std::bind(&sorter::do_sort, this, std::move(new_lower_chunk))); 117 | 118 | std::list new_higher(do_sort(chunk_data)); 119 | 120 | result.splice(result.end(), new_higher); 121 | 122 | //while(new_lower.wait_for(std::chrono::seconds(0))== std::future_status::timeout) 123 | while (!new_lower._Is_ready()) 124 | { 125 | pool.run_pending_task(); 126 | } 127 | 128 | result.splice(result.begin(), new_lower.get()); 129 | 130 | return result; 131 | 132 | } 133 | 134 | }; 135 | 136 | template 137 | std::list parallel_quick_sort(std::list input) 138 | { 139 | if (input.empty()) 140 | { 141 | return input; 142 | } 143 | 144 | sorter s; 145 | return s.do_sort(input); 146 | } 147 | 148 | 149 | 150 | 151 | 152 | 153 | -------------------------------------------------------------------------------- /find/parallel_find.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "common_objs.h" 9 | 10 | 11 | template 12 | Iterator parallel_find_pt(Iterator first, Iterator last, MatchType match) 13 | { 14 | struct find_element 15 | { 16 | void operator()(Iterator begin, Iterator end, 17 | MatchType match, 18 | std::promise* result, 19 | std::atomic* done_flag) 20 | { 21 | try 22 | { 23 | for( ; (begin != end) && !std::atomic_load(done_flag); ++begin) 24 | { 25 | if (*begin == match) 26 | { 27 | result->set_value(begin); 28 | //done_flag.store(true); 29 | std::atomic_store(done_flag, true); 30 | return; 31 | } 32 | } 33 | } 34 | catch (...) 35 | { 36 | result->set_exception(std::current_exception()); 37 | done_flag->store(true); 38 | } 39 | } 40 | }; 41 | 42 | unsigned long const length = std::distance(first, last); 43 | 44 | if (!length) 45 | return last; 46 | 47 | /* Calculate the optimized number of threads to run the algorithm */ 48 | 49 | unsigned long const min_per_thread = 25; 50 | unsigned long const max_threads = (length + min_per_thread - 1) / min_per_thread; 51 | 52 | unsigned long const hardware_threads = std::thread::hardware_concurrency(); 53 | unsigned long const num_threads = std::min(hardware_threads != 0 ? hardware_threads : 2, max_threads); 54 | unsigned long const block_size = length / num_threads; 55 | 56 | /* Declare the needed data structures */ 57 | std::promise result; 58 | std::atomic done_flag(false); 59 | 60 | std::vector threads(num_threads - 1); 61 | 62 | { 63 | join_threads joiner(threads); 64 | 65 | // task dividing loop 66 | Iterator block_start = first; 67 | for (unsigned long i = 0; i < (num_threads - 1); i++) 68 | { 69 | Iterator block_end = block_start; 70 | std::advance(block_end, block_size); 71 | 72 | threads[i] = std::thread(find_element(), block_start, block_end, match, &result, &done_flag); 73 | 74 | block_start = block_end; 75 | } 76 | 77 | // perform the find operation for final block in this thread. 78 | find_element()(block_start, last, match, &result, &done_flag); 79 | } 80 | 81 | if (!done_flag.load()) 82 | { 83 | return last; 84 | } 85 | 86 | return result.get_future().get(); 87 | } 88 | 89 | template 90 | Iterator parallel_find_async(Iterator first, Iterator last, MatchType match, std::atomic* done_flag) 91 | { 92 | try 93 | { 94 | unsigned long const length = std::distance(first, last); 95 | unsigned long const min_per_thread = 25; 96 | 97 | if (length < 2 * min_per_thread) 98 | { 99 | for ( ; (first != last ) && done_flag; ++first) 100 | { 101 | if (*first == match) 102 | { 103 | *done_flag = true; 104 | return first; 105 | } 106 | } 107 | return last; 108 | } 109 | else 110 | { 111 | Iterator const mid_point = first + length / 2; 112 | std::future async_result = 113 | std::async(¶llel_find_async,mid_point, last, match, std::ref(done_flag)); 114 | 115 | Iterator const direct_result = 116 | parallel_find_async(first, mid_point, match, done_flag); 117 | 118 | return (direct_result == mid_point) ? async_result.get() : direct_result; 119 | } 120 | } 121 | catch (const std::exception&) 122 | { 123 | *done_flag = true; 124 | throw; 125 | } 126 | } -------------------------------------------------------------------------------- /hashmap/Source.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "parallel_hashtable.h" 5 | 6 | 7 | #define USE_FAST_SETITEM 1 8 | 9 | 10 | //---------------------------------------------- 11 | // from code.google.com/p/smhasher/wiki/MurmurHash3 12 | inline static uint32_t integerHash(uint32_t h) 13 | { 14 | h ^= h >> 16; 15 | h *= 0x85ebca6b; 16 | h ^= h >> 13; 17 | h *= 0xc2b2ae35; 18 | h ^= h >> 16; 19 | return h; 20 | } 21 | 22 | 23 | //---------------------------------------------- 24 | HashTable1::HashTable1(uint32_t arraySize) 25 | { 26 | // Initialize cells 27 | assert((arraySize & (arraySize - 1)) == 0); // Must be a power of 2 28 | m_arraySize = arraySize; 29 | m_entries = new Entry[arraySize]; 30 | Clear(); 31 | } 32 | 33 | 34 | //---------------------------------------------- 35 | HashTable1::~HashTable1() 36 | { 37 | // Delete cells 38 | delete[] m_entries; 39 | } 40 | 41 | 42 | //---------------------------------------------- 43 | #if USE_FAST_SETITEM 44 | void HashTable1::SetItem(uint32_t key, uint32_t value) 45 | { 46 | assert(key != 0); 47 | assert(value != 0); 48 | 49 | for (uint32_t idx = integerHash(key);; idx++) 50 | { 51 | idx &= m_arraySize - 1; 52 | 53 | // Load the key that was there. 54 | uint32_t probedKey = std::atomic_load(&m_entries[idx].key); 55 | if (probedKey != key) 56 | { 57 | // The entry was either free, or contains another key. 58 | if (probedKey != 0) 59 | continue; // Usually, it contains another key. Keep probing. 60 | 61 | // The entry was free. Now let's try to take it using a CAS. 62 | uint32_t prevKey = std::atomic_compare_exchange_strong(&m_entries[idx].key, 0, key); 63 | if ((prevKey != 0) && (prevKey != key)) 64 | continue; // Another thread just stole it from underneath us. 65 | 66 | // Either we just added the key, or another thread did. 67 | } 68 | 69 | // Store the value in this array entry. 70 | std::atomic_store(&m_entries[idx].value, value); 71 | return; 72 | } 73 | } 74 | #else 75 | void HashTable1::SetItem(uint32_t key, uint32_t value) 76 | { 77 | assert(key != 0); 78 | assert(value != 0); 79 | 80 | for (uint32_t idx = integerHash(key);; idx++) 81 | { 82 | idx &= m_arraySize - 1; 83 | 84 | uint32_t prevKey = mint_compare_exchange_strong_32_relaxed(&m_entries[idx].key, 0, key); 85 | if ((prevKey == 0) || (prevKey == key)) 86 | { 87 | mint_store_32_relaxed(&m_entries[idx].value, value); 88 | return; 89 | } 90 | } 91 | } 92 | #endif 93 | 94 | //---------------------------------------------- 95 | uint32_t HashTable1::GetItem(uint32_t key) 96 | { 97 | assert(key != 0); 98 | 99 | for (uint32_t idx = integerHash(key);; idx++) 100 | { 101 | idx &= m_arraySize - 1; 102 | 103 | uint32_t probedKey = std::atomic_load(&m_entries[idx].key); 104 | if (probedKey == key) 105 | return atomic_load(&m_entries[idx].value); 106 | if (probedKey == 0) 107 | return 0; 108 | } 109 | } 110 | 111 | 112 | //---------------------------------------------- 113 | uint32_t HashTable1::GetItemCount() 114 | { 115 | uint32_t itemCount = 0; 116 | for (uint32_t idx = 0; idx < m_arraySize; idx++) 117 | { 118 | if ((atomic_load(&m_entries[idx].key) != 0) 119 | && (atomic_load(&m_entries[idx].value) != 0)) 120 | itemCount++; 121 | } 122 | return itemCount; 123 | } 124 | 125 | 126 | //---------------------------------------------- 127 | void HashTable1::Clear() 128 | { 129 | memset(m_entries, 0, sizeof(Entry) * m_arraySize); 130 | } -------------------------------------------------------------------------------- /coroutines/main.cpp: -------------------------------------------------------------------------------- 1 | //#include 2 | // 3 | //#include 4 | //#include 5 | // 6 | // 7 | //class my_resumable { 8 | //public: 9 | // struct promise_type; 10 | // using coro_handle = std::experimental::coroutine_handle; 11 | // my_resumable(coro_handle handle) : handle_(handle) { assert(handle); } 12 | // my_resumable(my_resumable&) = delete; 13 | // my_resumable(my_resumable&&) = delete; 14 | // bool resume() { 15 | // if (not handle_.done()) 16 | // handle_.resume(); 17 | // return not handle_.done(); 18 | // } 19 | // ~my_resumable() { handle_.destroy(); } 20 | //private: 21 | // coro_handle handle_; 22 | //}; 23 | // 24 | //struct my_resumable::promise_type { 25 | // using coro_handle = std::experimental::coroutine_handle; 26 | // auto get_return_object() noexcept { 27 | // return coro_handle::from_promise(*this); 28 | // } 29 | // auto initial_suspend() noexcept { return std::experimental::suspend_always(); } 30 | // auto final_suspend() noexcept{ return std::experimental::suspend_always(); } 31 | // void return_void() noexcept {} 32 | // void unhandled_exception() noexcept { 33 | // std::terminate(); 34 | // } 35 | //}; 36 | // 37 | //my_resumable foo() { 38 | // std::cout << "Hello" << std::endl; 39 | // co_await std::experimental::suspend_always(); 40 | // std::cout << "World" << std::endl; 41 | //} 42 | // 43 | //void alpha() 44 | //{ 45 | // std::cout << "A" << std::endl; 46 | // std::cout << "B" << std::endl; 47 | // std::cout << "C" << std::endl; 48 | //} 49 | // 50 | //void numeric() 51 | //{ 52 | // std::cout << "1" << std::endl; 53 | // std::cout << "2" << std::endl; 54 | // std::cout << "3" << std::endl; 55 | //} 56 | // 57 | //int main() 58 | //{ 59 | // alpha(); 60 | // numeric(); 61 | // // resumable res = foo(); 62 | // // foo().resume(); 63 | // // while (res.resume()); 64 | //} 65 | // 66 | // 67 | // 68 | 69 | 70 | #include 71 | #include 72 | #include 73 | 74 | template 75 | struct Generator { 76 | 77 | struct promise_type; 78 | using handle_type = experimental::coroutine_handle; 79 | 80 | Generator(handle_type h) : coro(h) {} // (3) 81 | handle_type coro; 82 | 83 | ~Generator() { 84 | if (coro) coro.destroy(); 85 | } 86 | Generator(const Generator&) = delete; 87 | Generator& operator = (const Generator&) = delete; 88 | Generator(Generator&& oth) noexcept : coro(oth.coro) { 89 | oth.coro = nullptr; 90 | } 91 | Generator& operator = (Generator&& oth) noexcept { 92 | coro = oth.coro; 93 | oth.coro = nullptr; 94 | return *this; 95 | } 96 | T getValue() { 97 | return coro.promise().current_value; 98 | } 99 | bool next() { // (5) 100 | coro.resume(); 101 | return not coro.done(); 102 | } 103 | struct promise_type { 104 | promise_type() = default; // (1) 105 | 106 | ~promise_type() = default; 107 | 108 | auto initial_suspend() { // (4) 109 | return experimental::suspend_always(); 110 | } 111 | auto final_suspend() noexcept { 112 | return experimental::suspend_always(); 113 | } 114 | auto get_return_object() { // (2) 115 | return Generator{ experimental::handle_type::from_promise(*this) }; 116 | } 117 | void return_void() { 118 | experimental::suspend_never(); 119 | } 120 | 121 | auto yield_value(const T value) { // (6) 122 | current_value = value; 123 | return experimental::suspend_always{}; 124 | } 125 | void unhandled_exception() { 126 | std::exit(1); 127 | } 128 | T current_value; 129 | }; 130 | 131 | }; 132 | 133 | 134 | Generator getNext(int start = 0, int step = 1) noexcept { 135 | auto value = start; 136 | for (int i = 0;; ++i) { 137 | co_yield value; 138 | value += step; 139 | } 140 | } 141 | 142 | int main() { 143 | 144 | std::cout << std::endl; 145 | 146 | std::cout << "getNext():"; 147 | auto gen = getNext(); 148 | for (int i = 0; i <= 10; ++i) { 149 | gen.next(); 150 | std::cout << " " << gen.getValue(); // (7) 151 | } 152 | 153 | std::cout << "\n\n"; 154 | 155 | std::cout << "getNext(100, -10):"; 156 | auto gen2 = getNext(100, -10); 157 | for (int i = 0; i <= 20; ++i) { 158 | gen2.next(); 159 | std::cout << " " << gen2.getValue(); 160 | } 161 | 162 | std::cout << std::endl; 163 | 164 | } 165 | -------------------------------------------------------------------------------- /s8_simple_threadpool_with_work_stealing/simple_threadpool_with_work_stealing.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include "common_thread_safe_queue.h" 10 | #include "common_objs.h" 11 | 12 | #include "utils.h" 13 | class work_stealing_queue 14 | { 15 | private: 16 | typedef function_wrapper data_type; 17 | std::deque the_queue; 18 | mutable std::mutex the_mutex; 19 | 20 | public: 21 | work_stealing_queue() 22 | {} 23 | 24 | work_stealing_queue(const work_stealing_queue& other) = delete; 25 | work_stealing_queue& operator=(const work_stealing_queue& other) = delete; 26 | 27 | void push(data_type data) 28 | { 29 | std::lock_guard lock(the_mutex); 30 | the_queue.push_front(std::move(data)); 31 | } 32 | 33 | bool empty() const 34 | { 35 | std::lock_guard lock(the_mutex); 36 | return the_queue.empty(); 37 | } 38 | 39 | bool try_pop(data_type& res) 40 | { 41 | std::lock_guard lock(the_mutex); 42 | if (the_queue.empty()) 43 | { 44 | return false; 45 | } 46 | 47 | res = std::move(the_queue.front()); 48 | the_queue.pop_front(); 49 | return true; 50 | } 51 | 52 | bool try_steal(data_type& res) 53 | { 54 | std::lock_guard lock(the_mutex); 55 | if (the_queue.empty()) 56 | { 57 | return false; 58 | } 59 | 60 | res = std::move(the_queue.back()); 61 | the_queue.pop_back(); 62 | return true; 63 | } 64 | }; 65 | 66 | class thread_pool_with_work_steal { 67 | 68 | typedef function_wrapper task_type; 69 | 70 | std::atomic_bool done; 71 | threadsafe_queue global_work_queue; 72 | std::vector > queues; 73 | std::vector threads; 74 | join_threads joiner; 75 | 76 | static thread_local work_stealing_queue* local_work_queue; 77 | static thread_local unsigned my_index; 78 | 79 | void worker_thread(unsigned my_index_) 80 | { 81 | my_index = my_index_; 82 | local_work_queue = queues[my_index].get(); 83 | while (!done) 84 | { 85 | run_pending_task(); 86 | } 87 | } 88 | 89 | bool pop_task_from_local_queue(task_type& task) 90 | { 91 | return local_work_queue && local_work_queue->try_pop(task); 92 | } 93 | 94 | bool pop_task_from_pool_queue(task_type& task) 95 | { 96 | return global_work_queue.try_pop(task); 97 | } 98 | 99 | bool pop_task_from_other_thread_queue(task_type& task) 100 | { 101 | for (unsigned i = 0; i < queues.size(); ++i) 102 | { 103 | unsigned const index = (my_index + i + 1) % queues.size(); 104 | if (queues[index]->try_steal(task)) 105 | { 106 | return true; 107 | } 108 | } 109 | 110 | return false; 111 | } 112 | 113 | public: 114 | thread_pool_with_work_steal() :joiner(threads), done(false) 115 | { 116 | unsigned const thread_count = std::thread::hardware_concurrency(); 117 | 118 | try 119 | { 120 | for (unsigned i = 0; i < thread_count; ++i) 121 | { 122 | queues.push_back(std::unique_ptr(new work_stealing_queue)); 123 | threads.push_back(std::thread(&thread_pool_with_work_steal::worker_thread, 124 | this, i)); 125 | } 126 | } 127 | catch (...) 128 | { 129 | done = true; 130 | throw; 131 | } 132 | } 133 | 134 | ~thread_pool_with_work_steal() 135 | { 136 | done = true; 137 | } 138 | 139 | template 140 | std::future::type> submit(FunctionType f) 141 | { 142 | typedef typename std::result_of::type result_type; 143 | 144 | std::packaged_task task(std::move(f)); 145 | std::future res(task.get_future()); 146 | 147 | if (local_work_queue) 148 | { 149 | local_work_queue->push(std::move(task)); 150 | } 151 | else 152 | { 153 | global_work_queue.push(std::move(task)); 154 | } 155 | return res; 156 | } 157 | 158 | void run_pending_task() 159 | { 160 | task_type task; 161 | if (pop_task_from_local_queue(task) || 162 | pop_task_from_pool_queue(task) || 163 | pop_task_from_other_thread_queue(task)) 164 | { 165 | task(); 166 | } 167 | else 168 | { 169 | std::this_thread::yield(); 170 | } 171 | } 172 | }; 173 | 174 | thread_local work_stealing_queue* thread_pool_with_work_steal::local_work_queue; 175 | thread_local unsigned thread_pool_with_work_steal::my_index; 176 | 177 | 178 | template 179 | struct sorter { 180 | 181 | thread_pool_with_work_steal pool; 182 | 183 | std::list do_sort(std::list& chunk_data) 184 | { 185 | if (chunk_data.size() < 2) 186 | return chunk_data; 187 | 188 | std::list result; 189 | result.splice(result.begin(), chunk_data, chunk_data.begin()); 190 | T const& partition_val = *result.begin(); 191 | 192 | typename std::list::iterator divide_point = std::partition(chunk_data.begin(), 193 | chunk_data.end(), [&](T const& val) 194 | { 195 | return val < partition_val; 196 | }); 197 | 198 | std::list new_lower_chunk; 199 | new_lower_chunk.splice(new_lower_chunk.end(), chunk_data, 200 | chunk_data.begin(), divide_point); 201 | 202 | std::future> new_lower = 203 | pool.submit(std::bind(&sorter::do_sort, this, std::move(new_lower_chunk))); 204 | 205 | std::list new_higher(do_sort(chunk_data)); 206 | 207 | result.splice(result.end(), new_higher); 208 | 209 | //while(new_lower.wait_for(std::chrono::seconds(0))== std::future_status::timeout) 210 | while (!new_lower._Is_ready()) 211 | { 212 | pool.run_pending_task(); 213 | } 214 | 215 | result.splice(result.begin(), new_lower.get()); 216 | 217 | return result; 218 | 219 | } 220 | 221 | }; 222 | 223 | template 224 | std::list parallel_quick_sort(std::list input) 225 | { 226 | if (input.empty()) 227 | { 228 | return input; 229 | } 230 | 231 | sorter s; 232 | return s.do_sort(input); 233 | } -------------------------------------------------------------------------------- /matrix/matrix.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "utils.h" 8 | #include "common_objs.h" 9 | 10 | class Matrix { 11 | 12 | int* data; 13 | int rows; // row count 14 | int columns; // column count 15 | 16 | public: 17 | 18 | Matrix(int _n, int _m) : rows(_n), columns(_m) 19 | { 20 | data = new int[ rows * columns ]; 21 | //set the array to 0 22 | std::fill(data, data + rows*columns, 0); 23 | } 24 | 25 | // i -> 0 to n-1 26 | // j -> 0 to m-1 27 | void set_value(int i, int j, int value) 28 | { 29 | data[i * columns + j] = value; 30 | } 31 | 32 | void set_all( int value) 33 | { 34 | std::fill(data, data + rows * columns, value); 35 | } 36 | 37 | static void multiply(Matrix* x, Matrix* y, Matrix* results) 38 | { 39 | //check the matrix sizes are correct to multiply 40 | if (!(x->columns == y->rows) || !((x->rows == results->rows) && (y->columns == results->columns))) 41 | { 42 | std::cout << " ERROR : Invalid matrix sizes for multiplication \n"; 43 | return; 44 | } 45 | 46 | // r = result_size 47 | int r = results->rows * results->columns; 48 | 49 | for (size_t i = 0; i < r; i++) 50 | { 51 | for (size_t j = 0; j < x->columns; j++) 52 | { 53 | results->data[i] += x->data[ (i / results->columns) * x->columns + j ] 54 | * y->data[ i % results->rows + j*y->columns ]; 55 | } 56 | } 57 | } 58 | 59 | static void parallel_multiply(Matrix* x, Matrix* y, Matrix* results) 60 | { 61 | struct process_data_chunk 62 | { 63 | void operator()(Matrix* results, Matrix* x, Matrix* y, int start_index, int end_index) 64 | { 65 | for (size_t i = start_index; i < end_index; i++) 66 | { 67 | for (size_t j = 0; j < x->columns; j++) 68 | { 69 | results->data[i] += x->data[(i / results->columns) * x->columns + j] 70 | * y->data[i % results->rows + j * y->columns]; 71 | } 72 | } 73 | } 74 | 75 | }; 76 | 77 | //check the matrix sizes are correct to multiply 78 | if (!((x->rows == results->rows) && (y->columns == results->columns)) || !(x->columns == y->rows)) 79 | { 80 | std::cout << " ERROR : Invalid matrix sizes for multiplication \n"; 81 | } 82 | 83 | // r = result_size 84 | int length = results->rows * results->columns; 85 | 86 | if (!length) 87 | return; 88 | 89 | int min_per_thread = 10000; 90 | int max_threads = (length + min_per_thread - 1) / min_per_thread; 91 | int hardware_threads = std::thread::hardware_concurrency(); 92 | int num_threads = std::min(hardware_threads != 0 ? hardware_threads : 2, max_threads); 93 | int block_size = length / num_threads; 94 | 95 | std::vector threads(num_threads - 1); 96 | int block_start = 0; 97 | int block_end = 0; 98 | { 99 | join_threads joiner(threads); 100 | 101 | for (unsigned long i = 0; i < (num_threads - 1); i++) 102 | { 103 | block_start = i * block_size; 104 | block_end = block_start + block_size; 105 | threads[i] = std::thread(process_data_chunk(), results, x, y, block_start, block_end); 106 | } 107 | 108 | // perform the find operation for final block in this thread. 109 | process_data_chunk()(results, x, y, block_end, length); 110 | } 111 | } 112 | 113 | static void transpose(Matrix* x, Matrix* results) 114 | { 115 | //check the matrix sizes are correct to multiply 116 | if ( !((x->columns == results->rows) && (x->rows == results->columns)) ) 117 | { 118 | std::cout << " ERROR : Invalid matrix sizes for transpose \n"; 119 | return; 120 | } 121 | 122 | // r = result_size 123 | int r = results->rows * results->columns; 124 | 125 | int result_column = 0; 126 | int result_row = 0; 127 | 128 | int input_column = 0; 129 | int input_row = 0; 130 | 131 | for (size_t i = 0; i < r; i++) 132 | { 133 | //get the current row and column count 134 | result_row = i / results->columns; 135 | result_column = i % results->columns; 136 | 137 | //flipped the columns and row for input 138 | input_row = result_column; 139 | input_column = result_row; 140 | 141 | //store the corresponding element from input to the results 142 | results->data[i] = x->data[input_row * x->columns + input_column]; 143 | } 144 | } 145 | 146 | static void parallel_transpose(Matrix* x, Matrix* results) 147 | { 148 | struct process_data_chunk 149 | { 150 | void operator()(Matrix* results, Matrix* x, int start_index, int end_index) 151 | { 152 | int result_column = 0; 153 | int result_row = 0; 154 | 155 | int input_column = 0; 156 | int input_row = 0; 157 | 158 | for (size_t i = start_index; i < end_index; i++) 159 | { 160 | result_row = i / results->columns; 161 | result_column = i % results->columns; 162 | 163 | input_row = result_column; 164 | input_column = result_row; 165 | 166 | results->data[i] = x->data[input_row * x->columns + input_column]; 167 | } 168 | } 169 | 170 | }; 171 | 172 | //check the matrix sizes are correct to multiply 173 | if (!((x->columns == results->rows) && (x->rows == results->columns))) 174 | { 175 | std::cout << " ERROR : Invalid matrix sizes for transpose \n"; 176 | return; 177 | } 178 | 179 | // r = result_size 180 | int length = results->rows * results->columns; 181 | 182 | if (!length) 183 | return; 184 | 185 | int min_per_thread = 10000; 186 | int max_threads = (length + min_per_thread - 1) / min_per_thread; 187 | int hardware_threads = std::thread::hardware_concurrency(); 188 | int num_threads = std::min(hardware_threads != 0 ? hardware_threads : 2, max_threads); 189 | int block_size = length / num_threads; 190 | 191 | std::vector threads(num_threads - 1); 192 | int block_start = 0; 193 | int block_end = 0; 194 | { 195 | join_threads joiner(threads); 196 | 197 | for (unsigned long i = 0; i < (num_threads - 1); i++) 198 | { 199 | block_start = i * block_size; 200 | block_end = block_start + block_size; 201 | threads[i] = std::thread(process_data_chunk(), results, x, block_start, block_end); 202 | } 203 | 204 | // perform the find operation for final block in this thread. 205 | process_data_chunk()(results, x, block_end, length); 206 | } 207 | } 208 | 209 | void print() 210 | { 211 | if ( rows < 50 && columns < 50 ) 212 | { 213 | for (size_t i = 0; i < rows; i++) 214 | { 215 | for (size_t j = 0; j < columns; j++) 216 | { 217 | std::cout << data[j + i * columns] << " "; 218 | } 219 | 220 | std::cout << "\n"; 221 | } 222 | std::cout << std::endl; 223 | } 224 | } 225 | 226 | ~Matrix() 227 | { 228 | delete data; 229 | } 230 | }; -------------------------------------------------------------------------------- /scan/partial_sum.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | #include "common_objs.h" 6 | 7 | 8 | template 9 | void parallel_partial_sum(Iterator first, Iterator last) 10 | { 11 | typedef typename Iterator::value_type value_type; 12 | 13 | struct process_chunk 14 | { 15 | void operator()(Iterator begin, Iterator last, 16 | std::future* previous_end_value, 17 | std::promise* end_value) 18 | { 19 | try 20 | { 21 | Iterator end = last; 22 | ++end; 23 | std::partial_sum(begin, end, begin); 24 | if (previous_end_value) 25 | { 26 | //this is not the first thread 27 | auto addend = previous_end_value->get(); 28 | *last += addend; 29 | if (end_value) 30 | { 31 | //not the last block 32 | end_value->set_value(*last); 33 | } 34 | std::for_each(begin, last, [addend](value_type& item) 35 | { 36 | item += addend; 37 | }); 38 | } 39 | else if (end_value) 40 | { 41 | //this is the first thread 42 | end_value->set_value(*last); 43 | } 44 | } 45 | catch (...) 46 | { 47 | if (end_value) 48 | { 49 | end_value->set_exception(std::current_exception()); 50 | } 51 | else 52 | { 53 | //final block - main therad is the one process the final block 54 | throw; 55 | } 56 | } 57 | } 58 | }; 59 | unsigned long const length = std::distance(first, last); 60 | 61 | if (!length) 62 | return; 63 | 64 | unsigned long const min_per_thread = 25; 65 | unsigned long const max_threads = (length + min_per_thread - 1) / min_per_thread; 66 | unsigned long const hardware_threads = std::thread::hardware_concurrency(); 67 | unsigned long const num_threads = std::min(hardware_threads != 0 ? hardware_threads : 2, max_threads); 68 | unsigned long const block_size = length / num_threads; 69 | 70 | 71 | //typedef typename Iterator::value_type value_type; 72 | 73 | std::vector threads(num_threads - 1); 74 | std::vector > end_values(num_threads - 1); 75 | std::vector > previous_end_values; 76 | previous_end_values.reserve(num_threads - 1); 77 | join_threads joiner(threads); 78 | 79 | Iterator block_start = first; 80 | for (unsigned long i = 0; i < (num_threads - 1); ++i) 81 | { 82 | Iterator block_last = block_start; 83 | std::advance(block_last, block_size - 1); 84 | 85 | threads[i] = std::thread(process_chunk(), 86 | block_start, block_last,(i != 0) ? &previous_end_values[i - 1] : 0, &end_values[i]); 87 | 88 | block_start = block_last; 89 | ++block_start; 90 | previous_end_values.push_back(end_values[i].get_future()); 91 | } 92 | 93 | Iterator final_element = block_start; 94 | std::advance(final_element, std::distance(block_start, last) - 1); 95 | process_chunk()(block_start, final_element,(num_threads > 1) ? &previous_end_values.back() : 0, 0); 96 | } 97 | 98 | struct barrier 99 | { 100 | std::atomic count; 101 | std::atomic spaces; 102 | std::atomic generation; 103 | barrier(unsigned count_) : 104 | count(count_), spaces(count_), generation(0) 105 | {} 106 | void wait() 107 | { 108 | unsigned const gen = generation.load(); 109 | if (!--spaces) 110 | { 111 | spaces = count.load(); 112 | ++generation; 113 | } 114 | else 115 | { 116 | while (generation.load() == gen) 117 | { 118 | std::this_thread::yield(); 119 | } 120 | } 121 | } 122 | void done_waiting() 123 | { 124 | --count; 125 | if (!--spaces) 126 | { 127 | spaces = count.load(); 128 | ++generation; 129 | } 130 | } 131 | }; 132 | template 133 | void parallel_partial_sum_barrier(Iterator first, Iterator last) 134 | { 135 | typedef typename Iterator::value_type value_type; 136 | struct process_element 137 | { 138 | void operator()(Iterator first, Iterator last, 139 | std::vector& buffer, 140 | unsigned i, barrier& b) 141 | { 142 | value_type& ith_element = *(first + i); 143 | bool update_source = false; 144 | 145 | for (unsigned step = 0, stride = 1; stride <= i; ++step, stride *= 2) 146 | { 147 | value_type const& source = (step % 2) ? buffer[i] : ith_element; 148 | value_type& dest = (step % 2) ? ith_element : buffer[i]; 149 | value_type const& addend = (step % 2) ? buffer[i - stride] : *(first + i - stride); 150 | dest = source + addend; 151 | update_source = !(step % 2); 152 | b.wait(); 153 | } 154 | if (update_source) 155 | { 156 | ith_element = buffer[i]; 157 | } 158 | b.done_waiting(); 159 | } 160 | }; 161 | unsigned long const length = std::distance(first, last); 162 | 163 | if (length <= 1) 164 | return; 165 | 166 | std::vector buffer(length); 167 | barrier b(length); 168 | std::vector threads(length - 1); 169 | join_threads joiner(threads); 170 | 171 | Iterator block_start = first; 172 | for (unsigned long i = 0; i < (length - 1); ++i) 173 | { 174 | threads[i] = std::thread(process_element(), first, last, 175 | std::ref(buffer), i, std::ref(b)); 176 | } 177 | process_element()(first, last, buffer, length - 1, b); 178 | } 179 | 180 | 181 | 182 | template 183 | void sequentail_partial_sum(Iterator first, Iterator last, OutIterator y) 184 | { 185 | unsigned long const length = std::distance(first, last); 186 | 187 | y[0] = first[0]; 188 | 189 | for (size_t i = 1; i < length; i++) 190 | { 191 | y[i] = first[i] + y[i - 1]; 192 | } 193 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Build results 17 | [Dd]ebug/ 18 | [Dd]ebugPublic/ 19 | [Rr]elease/ 20 | [Rr]eleases/ 21 | x64/ 22 | x86/ 23 | [Aa][Rr][Mm]/ 24 | [Aa][Rr][Mm]64/ 25 | bld/ 26 | [Bb]in/ 27 | [Oo]bj/ 28 | [Ll]og/ 29 | 30 | # Visual Studio 2015/2017 cache/options directory 31 | .vs/ 32 | # Uncomment if you have tasks that create the project's static files in wwwroot 33 | #wwwroot/ 34 | 35 | # Visual Studio 2017 auto generated files 36 | Generated\ Files/ 37 | 38 | # MSTest test Results 39 | [Tt]est[Rr]esult*/ 40 | [Bb]uild[Ll]og.* 41 | 42 | # NUNIT 43 | *.VisualState.xml 44 | TestResult.xml 45 | 46 | # Build Results of an ATL Project 47 | [Dd]ebugPS/ 48 | [Rr]eleasePS/ 49 | dlldata.c 50 | 51 | # Benchmark Results 52 | BenchmarkDotNet.Artifacts/ 53 | 54 | # .NET Core 55 | project.lock.json 56 | project.fragment.lock.json 57 | artifacts/ 58 | 59 | # StyleCop 60 | StyleCopReport.xml 61 | 62 | # Files built by Visual Studio 63 | *_i.c 64 | *_p.c 65 | *_h.h 66 | *.ilk 67 | *.meta 68 | *.obj 69 | *.iobj 70 | *.pch 71 | *.pdb 72 | *.ipdb 73 | *.pgc 74 | *.pgd 75 | *.rsp 76 | *.sbr 77 | *.tlb 78 | *.tli 79 | *.tlh 80 | *.tmp 81 | *.tmp_proj 82 | *_wpftmp.csproj 83 | *.log 84 | *.vspscc 85 | *.vssscc 86 | .builds 87 | *.pidb 88 | *.svclog 89 | *.scc 90 | 91 | # Chutzpah Test files 92 | _Chutzpah* 93 | 94 | # Visual C++ cache files 95 | ipch/ 96 | *.aps 97 | *.ncb 98 | *.opendb 99 | *.opensdf 100 | *.sdf 101 | *.cachefile 102 | *.VC.db 103 | *.VC.VC.opendb 104 | 105 | # Visual Studio profiler 106 | *.psess 107 | *.vsp 108 | *.vspx 109 | *.sap 110 | 111 | # Visual Studio Trace Files 112 | *.e2e 113 | 114 | # TFS 2012 Local Workspace 115 | $tf/ 116 | 117 | # Guidance Automation Toolkit 118 | *.gpState 119 | 120 | # ReSharper is a .NET coding add-in 121 | _ReSharper*/ 122 | *.[Rr]e[Ss]harper 123 | *.DotSettings.user 124 | 125 | # JustCode is a .NET coding add-in 126 | .JustCode 127 | 128 | # TeamCity is a build add-in 129 | _TeamCity* 130 | 131 | # DotCover is a Code Coverage Tool 132 | *.dotCover 133 | 134 | # AxoCover is a Code Coverage Tool 135 | .axoCover/* 136 | !.axoCover/settings.json 137 | 138 | # Visual Studio code coverage results 139 | *.coverage 140 | *.coveragexml 141 | 142 | # NCrunch 143 | _NCrunch_* 144 | .*crunch*.local.xml 145 | nCrunchTemp_* 146 | 147 | # MightyMoose 148 | *.mm.* 149 | AutoTest.Net/ 150 | 151 | # Web workbench (sass) 152 | .sass-cache/ 153 | 154 | # Installshield output folder 155 | [Ee]xpress/ 156 | 157 | # DocProject is a documentation generator add-in 158 | DocProject/buildhelp/ 159 | DocProject/Help/*.HxT 160 | DocProject/Help/*.HxC 161 | DocProject/Help/*.hhc 162 | DocProject/Help/*.hhk 163 | DocProject/Help/*.hhp 164 | DocProject/Help/Html2 165 | DocProject/Help/html 166 | 167 | # Click-Once directory 168 | publish/ 169 | 170 | # Publish Web Output 171 | *.[Pp]ublish.xml 172 | *.azurePubxml 173 | # Note: Comment the next line if you want to checkin your web deploy settings, 174 | # but database connection strings (with potential passwords) will be unencrypted 175 | *.pubxml 176 | *.publishproj 177 | 178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 179 | # checkin your Azure Web App publish settings, but sensitive information contained 180 | # in these scripts will be unencrypted 181 | PublishScripts/ 182 | 183 | # NuGet Packages 184 | *.nupkg 185 | # The packages folder can be ignored because of Package Restore 186 | **/[Pp]ackages/* 187 | # except build/, which is used as an MSBuild target. 188 | !**/[Pp]ackages/build/ 189 | # Uncomment if necessary however generally it will be regenerated when needed 190 | #!**/[Pp]ackages/repositories.config 191 | # NuGet v3's project.json files produces more ignorable files 192 | *.nuget.props 193 | *.nuget.targets 194 | 195 | # Microsoft Azure Build Output 196 | csx/ 197 | *.build.csdef 198 | 199 | # Microsoft Azure Emulator 200 | ecf/ 201 | rcf/ 202 | 203 | # Windows Store app package directories and files 204 | AppPackages/ 205 | BundleArtifacts/ 206 | Package.StoreAssociation.xml 207 | _pkginfo.txt 208 | *.appx 209 | 210 | # Visual Studio cache files 211 | # files ending in .cache can be ignored 212 | *.[Cc]ache 213 | # but keep track of directories ending in .cache 214 | !?*.[Cc]ache/ 215 | 216 | # Others 217 | ClientBin/ 218 | ~$* 219 | *~ 220 | *.dbmdl 221 | *.dbproj.schemaview 222 | *.jfm 223 | *.pfx 224 | *.publishsettings 225 | orleans.codegen.cs 226 | 227 | # Including strong name files can present a security risk 228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 229 | #*.snk 230 | 231 | # Since there are multiple workflows, uncomment next line to ignore bower_components 232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 233 | #bower_components/ 234 | 235 | # RIA/Silverlight projects 236 | Generated_Code/ 237 | 238 | # Backup & report files from converting an old project file 239 | # to a newer Visual Studio version. Backup files are not needed, 240 | # because we have git ;-) 241 | _UpgradeReport_Files/ 242 | Backup*/ 243 | UpgradeLog*.XML 244 | UpgradeLog*.htm 245 | ServiceFabricBackup/ 246 | *.rptproj.bak 247 | 248 | # SQL Server files 249 | *.mdf 250 | *.ldf 251 | *.ndf 252 | 253 | # Business Intelligence projects 254 | *.rdl.data 255 | *.bim.layout 256 | *.bim_*.settings 257 | *.rptproj.rsuser 258 | *- Backup*.rdl 259 | 260 | # Microsoft Fakes 261 | FakesAssemblies/ 262 | 263 | # GhostDoc plugin setting file 264 | *.GhostDoc.xml 265 | 266 | # Node.js Tools for Visual Studio 267 | .ntvs_analysis.dat 268 | node_modules/ 269 | 270 | # Visual Studio 6 build log 271 | *.plg 272 | 273 | # Visual Studio 6 workspace options file 274 | *.opt 275 | 276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 277 | *.vbw 278 | 279 | # Visual Studio LightSwitch build output 280 | **/*.HTMLClient/GeneratedArtifacts 281 | **/*.DesktopClient/GeneratedArtifacts 282 | **/*.DesktopClient/ModelManifest.xml 283 | **/*.Server/GeneratedArtifacts 284 | **/*.Server/ModelManifest.xml 285 | _Pvt_Extensions 286 | 287 | # Paket dependency manager 288 | .paket/paket.exe 289 | paket-files/ 290 | 291 | # FAKE - F# Make 292 | .fake/ 293 | 294 | # JetBrains Rider 295 | .idea/ 296 | *.sln.iml 297 | 298 | # CodeRush personal settings 299 | .cr/personal 300 | 301 | # Python Tools for Visual Studio (PTVS) 302 | __pycache__/ 303 | *.pyc 304 | 305 | # Cake - Uncomment if you are using it 306 | # tools/** 307 | # !tools/packages.config 308 | 309 | # Tabs Studio 310 | *.tss 311 | 312 | # Telerik's JustMock configuration file 313 | *.jmconfig 314 | 315 | # BizTalk build output 316 | *.btp.cs 317 | *.btm.cs 318 | *.odx.cs 319 | *.xsd.cs 320 | 321 | # OpenCover UI analysis results 322 | OpenCover/ 323 | 324 | # Azure Stream Analytics local run output 325 | ASALocalRun/ 326 | 327 | # MSBuild Binary and Structured Log 328 | *.binlog 329 | 330 | # NVidia Nsight GPU debugger configuration file 331 | *.nvuser 332 | 333 | # MFractors (Xamarin productivity tool) working folder 334 | .mfractor/ 335 | 336 | # Local History for Visual Studio 337 | .localhistory/ 338 | 339 | # BeatPulse healthcheck temp database 340 | healthchecksdb 341 | /mutex 342 | -------------------------------------------------------------------------------- /reverse/reverse.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | {DA0AA536-96F2-45A1-B7AF-EBC4CE030BC8} 24 | reverse 25 | 10.0 26 | 27 | 28 | 29 | Application 30 | true 31 | v142 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v142 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v142 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v142 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Level3 87 | true 88 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 89 | true 90 | 91 | 92 | Console 93 | true 94 | 95 | 96 | 97 | 98 | Level3 99 | true 100 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 101 | true 102 | 103 | 104 | Console 105 | true 106 | 107 | 108 | 109 | 110 | Level3 111 | true 112 | true 113 | true 114 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 115 | true 116 | 117 | 118 | Console 119 | true 120 | true 121 | true 122 | 123 | 124 | 125 | 126 | Level3 127 | true 128 | true 129 | true 130 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 131 | true 132 | 133 | 134 | Console 135 | true 136 | true 137 | true 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /quick_sort/quick_sort.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | {C3E7EF0F-B5D0-458F-838A-3CE0EA41039A} 24 | quicksort 25 | 10.0 26 | 27 | 28 | 29 | Application 30 | true 31 | v142 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v142 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v142 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v142 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Level3 87 | true 88 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 89 | true 90 | 91 | 92 | Console 93 | true 94 | 95 | 96 | 97 | 98 | Level3 99 | true 100 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 101 | true 102 | 103 | 104 | Console 105 | true 106 | 107 | 108 | 109 | 110 | Level3 111 | true 112 | true 113 | true 114 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 115 | true 116 | 117 | 118 | Console 119 | true 120 | true 121 | true 122 | 123 | 124 | 125 | 126 | Level3 127 | true 128 | true 129 | true 130 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 131 | true 132 | 133 | 134 | Console 135 | true 136 | true 137 | true 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | -------------------------------------------------------------------------------- /thread_safe_queue/thread_safe_queue.vcxproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | Debug 14 | x64 15 | 16 | 17 | Release 18 | x64 19 | 20 | 21 | 22 | 16.0 23 | {B07CD316-0020-4DBA-9B86-3D08AFC75CCF} 24 | threadsafequeue 25 | 10.0 26 | 27 | 28 | 29 | Application 30 | true 31 | v142 32 | Unicode 33 | 34 | 35 | Application 36 | false 37 | v142 38 | true 39 | Unicode 40 | 41 | 42 | Application 43 | true 44 | v142 45 | Unicode 46 | 47 | 48 | Application 49 | false 50 | v142 51 | true 52 | Unicode 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | true 74 | 75 | 76 | true 77 | 78 | 79 | false 80 | 81 | 82 | false 83 | 84 | 85 | 86 | Level3 87 | true 88 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 89 | true 90 | 91 | 92 | Console 93 | true 94 | 95 | 96 | 97 | 98 | Level3 99 | true 100 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions) 101 | true 102 | 103 | 104 | Console 105 | true 106 | 107 | 108 | 109 | 110 | Level3 111 | true 112 | true 113 | true 114 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 115 | true 116 | 117 | 118 | Console 119 | true 120 | true 121 | true 122 | 123 | 124 | 125 | 126 | Level3 127 | true 128 | true 129 | true 130 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions) 131 | true 132 | 133 | 134 | Console 135 | true 136 | true 137 | true 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | --------------------------------------------------------------------------------