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

相关推荐

线程池项目 给了实例化线程池的参数,问他是干什么用的,就介绍各个参数的作用怎么根据运行状态调整参数,举例说任务堆积太多,就申请多线程问计算密集型的用什么配置,问跟上面的例子矛盾了。所以应该加上对cpu、io和内存等的监控。线程的调度是jvm还是操作系统,是用户态还是内核态答了内核态,为什么用内核态,用户态不是更快吗,答了安全问题,问有什么造成安全的场景(好像可以从协程和线程的优缺点来回答)索引讲一下innoDB存储引擎的模型,就是索引的存储结构等,讲了B+树,聚簇索引和非聚簇索引,存储的东西。问为什么不用B树和红黑树假设订单用联合索引查询, select * where "楼栋号" = and "手机号" = ,问用哪种顺序建立索引好。主键用相对自增的还是用随机的,答了自增,顺序写比较快(他不太满意这个答案)日志redoLog的作用。问为什么不直接把数据刷盘,而是先持久化redoLog,开销的对比。讲了可以用后台线程,问后台线程阻塞了怎么办,数据不久丢失了。想起来好像是redoLog可以先写到内核缓冲区,这样只要系统不崩溃就不会丢失。binLog的作用,跟redoLog的区别外卖项目是自己练手还是真实应用的,QPS有多高手撕给一个时间区间,起始时间和结束时间,格式“HH:MM:ss Weekday”一个具体的时间点,何时"Y-M-D-HH:MM:ss",判断时间点在不在每周的这个区间内,如果在直接返回时间区间,不在返回离它最近的时间区间(以"Y-M-D-HH:MM:ss"的格式)
拼多多集团-PDD一面356人在聊
点赞 评论 收藏
分享
评论
2
2
分享

创作者周榜

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