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

相关推荐

点赞 评论 收藏
分享
05-09 15:21
门头沟学院 Java
- 在Java中,为什么String类要被final修饰?- ArrayList和LinkedList有什么区别?- 如何让ArrayList变成线程安全?- 你提到的`CopyOnWriteArrayList`的内部实现了解吗?- HashMap为什么线程不安全?多线程使用HashMap会导致哪些问题?(分为hash冲突以及不冲突两种情况)- ConcurrentHashMap如何解决多线程带来的问题?- 为什么不使用HashTable而是ConcurrentHashMap- HashTable与ConcurrentHashMap在性能上的区别是什么?- HashTable与ConcurrentHashMap在多线程性能上的区别是什么?- HashMap的默认负载因子是多少?为什么默认是0.75?- HashMap在什么情况时会转为红黑树?小于该值会不会转回链表?- 深拷贝和浅拷贝的区别是什么?如何实现这两种拷贝?- Java的反射机制为什么性能比直接调用慢?- 用Lambda表达式将List转为Map时,如何处理Key冲突(保留较大的Value)?- 如何用Lambda表达式将List转为Map,使相同Key的Value合并为List?- java中有两种动态代理,你能说一下是哪两种吗- error跟exception的区别- Java中有哪些实现锁的方式?- synchronized的锁升级过程是怎样的?- Lock类相比synchronized有哪些优势?- Spring Security的用户信息上下文存储在哪- ThreadLocal的原理是什么?实际应用场景除了保存登录信息有哪些?- volatile关键字的作用是什么?- 线程池参数如何设置(IO密集型任务,8核CPU)?- 线程工厂(ThreadFactory)的作用是什么?- 线程池去执行一个多线程任务的时候,有哪些提交方式- CountDownLatch若某个线程异常未调用`countDown()`,如何避免主线程阻塞?- G1垃圾回收器的工作原理是什么?- 双亲委派机制- Tomcat为什么打破双亲委派机制?- 联合索引(A,B)中,仅用B字段查询能否命中索引?- 索引下推优化在何种查询条件下会触发?- 对日期字段(包含时分秒)建立索引后,使用`BETWEEN`且格式化为年月日查询,能否命中索引?- Spring如何解决循环依赖?能否解决构造器注入的循环依赖,为什么不能?- MyBatis中`#{}`和`${}`的区别是什么?两者的生成SQL是否相同?- 扫码登录的完整流程是如何设计的(前后端协作)?- lua脚本中具体使用的命令
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客企业服务