Created
June 19, 2024 10:08
-
-
Save syaifulnizamyahya/c80b260f97d6e28e2b157891a49888ad to your computer and use it in GitHub Desktop.
Example showing how to use concurrency in C++ 14. (single thread, multi thread, asynchronous)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Shared data structure | |
| std::vector<int> shared_vector; | |
| std::mutex mtx; | |
| // Function for adding elements to the vector | |
| void add_elements(int thread_id) | |
| { | |
| for (int i = 0; i < 5; ++i) | |
| { | |
| std::this_thread::sleep_for(std::chrono::milliseconds(100)); // Simulate work | |
| std::lock_guard<std::mutex> lock(mtx); | |
| shared_vector.push_back(i + thread_id * 10); | |
| std::cout << "Thread " << thread_id << " added " << i + thread_id * 10 << std::endl; | |
| } | |
| } | |
| // Function for reading elements from the vector | |
| void read_elements(int thread_id) | |
| { | |
| for (int i = 0; i < 5; ++i) | |
| { | |
| std::this_thread::sleep_for(std::chrono::milliseconds(150)); // Simulate work | |
| std::lock_guard<std::mutex> lock(mtx); | |
| std::cout << "Thread " << thread_id << " reads vector: "; | |
| for (int val : shared_vector) | |
| { | |
| std::cout << val << " "; | |
| } | |
| std::cout << std::endl; | |
| } | |
| } | |
| void run() | |
| { | |
| std::cout << "single_thread" << std::endl; | |
| shared_vector.clear(); | |
| for (int i = 0; i < 3; ++i) | |
| { | |
| add_elements(i + 1); | |
| } | |
| for (int i = 0; i < 2; ++i) | |
| { | |
| read_elements(i + 1); | |
| } | |
| std::cout << "end single_thread" << std::endl; | |
| std::cout << "multiple_thread" << std::endl; | |
| shared_vector.clear(); | |
| std::vector<std::thread> threads; | |
| for (int i = 0; i < 3; ++i) | |
| { | |
| // method 1 | |
| // std::thread t(add_elements, i + 1); | |
| // threads.push_back(std::move(t)); | |
| // method 2 | |
| // threads.push_back(std::thread(add_elements, i + 1)); | |
| // method 3 | |
| threads.emplace_back(add_elements, i + 1); | |
| } | |
| for (int i = 0; i < 2; ++i) | |
| { | |
| // method 1 | |
| // std::thread t(read_elements, i + 1); | |
| // threads.push_back(std::move(t)); | |
| // method 2 | |
| // threads.push_back(std::thread(read_elements, i + 1)); | |
| // method 3 | |
| threads.emplace_back(read_elements, i + 1); | |
| } | |
| // Join all threads | |
| for (auto &t : threads) | |
| { | |
| t.join(); | |
| } | |
| std::cout << "end multiple_thread" << std::endl; | |
| // async | |
| std::cout << "async" << std::endl; | |
| shared_vector.clear(); | |
| std::vector<std::future<void>> futures; | |
| // Launch asynchronous tasks for adding elements | |
| for (int i = 0; i < 3; ++i) | |
| { | |
| futures.emplace_back(std::async(std::launch::async, add_elements, i + 1)); | |
| } | |
| // Launch asynchronous tasks for reading elements | |
| for (int i = 0; i < 2; ++i) | |
| { | |
| futures.emplace_back(std::async(std::launch::async, read_elements, i + 1)); | |
| } | |
| for (auto &f : futures) | |
| { | |
| f.get(); | |
| } | |
| std::cout << "end async" << std::endl; | |
| std::cout << "All tasks have completed" << std::endl; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment