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
全部评论

相关推荐

09-13 10:40
门头沟学院 Java
听别人介绍,刷了一堆力扣题,考场上写函数,一直无法通过。赛后才知道要自己写输入输出,力扣害人不浅
Silencer76:输入输出练习题单,请https://www.nowcoder.com/exam/oj?page=1&tab=%E7%AE%97%E6%B3%95%E7%AC%94%E9%9D%A2%E8%AF%95%E7%AF%87&topicId=372
点赞 评论 收藏
分享
亲切的00后在笔试:我也遇到了,所以我早他一步查看图片
点赞 评论 收藏
分享
我不行了,我真过不了第二关
码农索隆:嘿,哥们连界面都进去去,更别提玩了
点赞 评论 收藏
分享
评论
2
3
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务