C++多线程的练习题目
list of 50 multithreading practice programs in C++, ranging from beginner to advanced level. These exercises will help you get hands-on experience with concepts like std::thread, std::mutex, std::condition_variable, std::future, std::async, and more.
Beginner Level
1. Create a thread that prints “Hello from thread”.
2. Launch multiple threads that print their thread IDs.
3. Pass parameters to a thread function.
4. Join and detach threads.
5. Create a thread that calculates the sum of an array.
6. Launch threads to compute factorials of different numbers.
7. Synchronize access to a shared variable using std::mutex.
8. Demonstrate a race condition without std::mutex.
9. Use std::lock_guard to lock a mutex.
10. Use std::unique_lock and std::mutex.
11. Create a thread-safe counter using std::mutex.
12. Print even and odd numbers using two threads.
13. Use a lambda function as thread entry point.
14. Compare performance: single thread vs multithreaded sum.
15. Create multiple threads to increment a shared value N times.
Intermediate Level
1. Use std::condition_variable for thread signaling.
2. Implement producer-consumer using a queue and condition variable.
3. Use std::async and std::future to get result from a thread.
4. Run multiple asynchronous tasks and wait for results.
5. Simulate a dining philosophers problem.
6. Readers-Writers problem using std::shared_mutex.
7. Thread pool implementation (basic).
8. Thread-safe logging class using mutex.
9. Use std::atomic for lock-free increment.
10. Compare atomic and mutex-based counters.
11. Launch threads using a class member function.
12. Use thread local storage (thread_local keyword).
13. Implement a countdown timer using threads.
14. Run a background task and cancel it using a flag.
15. Simulate a bank account with deposit and withdraw using locks.
16. Multithreaded merge sort.
17. Parallel matrix multiplication.
18. Use std::promise and std::future for communication.
19. Measure thread execution time using std::chrono.
20. Implement a bounded blocking queue
Advanced Level
1. Simulate a multithreaded file downloader.
2. Implement a thread-safe cache (map with read/write lock).
3. Create a parallel web crawler using threads.
4. Use a thread-safe singleton pattern.
5. Build a multithreaded chat server (basic simulation).
6. Parallel image filter application (e.g., blur effect).
7. Implement work stealing thread pool.
8. Build a task scheduler using threads and a priority queue.
9. Simulate ticket booking system with concurrent access.
10. Parallel prime number finder in a given range.
11. Lock-free queue using std::atomic (advanced).
12. Design a thread-safe observer pattern.
13. Build a pipeline pattern (e.g., fetch -> decode -> process).
14. Performance comparison: std::thread vs std::async.
15. Debug deadlocks using std::mutex with intentional mistakes.
Happy Coding :)
https://medium.com/@krishnacse20/c-multithreading-practice-programs-ea8f8a1be446
Beginner Level
1. Create a thread that prints “Hello from thread”.
2. Launch multiple threads that print their thread IDs.
3. Pass parameters to a thread function.
4. Join and detach threads.
5. Create a thread that calculates the sum of an array.
6. Launch threads to compute factorials of different numbers.
7. Synchronize access to a shared variable using std::mutex.
8. Demonstrate a race condition without std::mutex.
9. Use std::lock_guard to lock a mutex.
10. Use std::unique_lock and std::mutex.
11. Create a thread-safe counter using std::mutex.
12. Print even and odd numbers using two threads.
13. Use a lambda function as thread entry point.
14. Compare performance: single thread vs multithreaded sum.
15. Create multiple threads to increment a shared value N times.
Intermediate Level
1. Use std::condition_variable for thread signaling.
2. Implement producer-consumer using a queue and condition variable.
3. Use std::async and std::future to get result from a thread.
4. Run multiple asynchronous tasks and wait for results.
5. Simulate a dining philosophers problem.
6. Readers-Writers problem using std::shared_mutex.
7. Thread pool implementation (basic).
8. Thread-safe logging class using mutex.
9. Use std::atomic for lock-free increment.
10. Compare atomic and mutex-based counters.
11. Launch threads using a class member function.
12. Use thread local storage (thread_local keyword).
13. Implement a countdown timer using threads.
14. Run a background task and cancel it using a flag.
15. Simulate a bank account with deposit and withdraw using locks.
16. Multithreaded merge sort.
17. Parallel matrix multiplication.
18. Use std::promise and std::future for communication.
19. Measure thread execution time using std::chrono.
20. Implement a bounded blocking queue
Advanced Level
1. Simulate a multithreaded file downloader.
2. Implement a thread-safe cache (map with read/write lock).
3. Create a parallel web crawler using threads.
4. Use a thread-safe singleton pattern.
5. Build a multithreaded chat server (basic simulation).
6. Parallel image filter application (e.g., blur effect).
7. Implement work stealing thread pool.
8. Build a task scheduler using threads and a priority queue.
9. Simulate ticket booking system with concurrent access.
10. Parallel prime number finder in a given range.
11. Lock-free queue using std::atomic (advanced).
12. Design a thread-safe observer pattern.
13. Build a pipeline pattern (e.g., fetch -> decode -> process).
14. Performance comparison: std::thread vs std::async.
15. Debug deadlocks using std::mutex with intentional mistakes.
Happy Coding :)
https://medium.com/@krishnacse20/c-multithreading-practice-programs-ea8f8a1be446
全部评论
相关推荐
点赞 评论 收藏
分享


金山办公
| 实习
| 76个岗位
点赞 评论 收藏
分享
05-06 17:43
肇庆学院 数据分析师 点赞 评论 收藏
分享