题目1:子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码

代码1:

  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. using namespace std;
  5.  
  6. const int count = ;
  7. int flag = ;
  8. std::mutex mtex;
  9.  
  10. void fun(const int num, const string &str) {
  11. for(int i = ; i < count; ++i) {
  12. while(num != flag) std::this_thread::yield();
  13. mtex.lock();
  14. for(int j = ; j < num; ++j) {
  15. cout << str << endl;
  16. }
  17. std::this_thread::sleep_for(std::chrono::seconds());
  18. flag = (flag == ? : );
  19. mtex.unlock();
  20. }
  21. }
  22.  
  23. int main(void) {
  24. auto start = std::chrono::high_resolution_clock::now();
  25. thread child(fun, , "child");
  26. fun(, "father");
  27. child.join();
  28. auto end = std::chrono::high_resolution_clock::now();
  29. std::chrono::duration<double, std::milli> elapsed = end - start;
  30. cout << elapsed.count() << endl;
  31.  
  32. return ;
  33. }

代码2:

  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <condition_variable>
  5. using namespace std;
  6.  
  7. const int count = ;
  8. int flag = ;
  9. condition_variable cv;
  10. std::mutex mtex;
  11.  
  12. void fun(const int num, const string &str) {
  13. for(int i = ; i < count; ++i) {
  14. unique_lock<std::mutex> lk(mtex);
  15. cv.wait(lk, [&]{
  16. return num == flag;
  17. });
  18. for(int j = ; j < num; ++j) {
  19. cout << str << endl;
  20. }
  21. std::this_thread::sleep_for(std::chrono::seconds());
  22. flag = (flag == ? : );
  23. mtex.unlock();
  24. cv.notify_one();
  25. }
  26. }
  27.  
  28. int main(void) {
  29. auto start = std::chrono::high_resolution_clock::now();
  30. thread child(fun, , "child");
  31. fun(, "father");
  32. child.join();
  33. auto end = std::chrono::high_resolution_clock::now();
  34. std::chrono::duration<double, std::milli> elapsed = end - start;
  35. cout << elapsed.count() << endl;
  36.  
  37. return ;
  38. }

题目2:编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推

代码1:

  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. using namespace std;
  5.  
  6. int main(void) {
  7. std::mutex mtex;
  8. int cnt = ;
  9. auto work = [&](char ch, int num) {
  10. while(num--) {
  11. while(ch != cnt + 'A') std::this_thread::yield();
  12. std::unique_lock<std::mutex> lk(mtex);
  13. cout << ch;
  14. cnt = (cnt + ) % ;
  15. lk.unlock();
  16. }
  17. };
  18. thread t1(work, 'A', );
  19. thread t2(work, 'B', );
  20. work('C', );
  21.  
  22. t1.join();
  23. t2.join();
  24.  
  25. return ;
  26. }

代码2:

  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <chrono>
  5. #include <condition_variable>
  6. using namespace std;
  7.  
  8. condition_variable cv;
  9. std::mutex mtex;
  10. int cnt = ;
  11.  
  12. void work(char ch, int num) {
  13. while(num--) {
  14. std::this_thread::sleep_for(std::chrono::seconds());
  15. unique_lock<std::mutex> lk(mtex);
  16. cv.wait(lk, [&]{
  17. return 'A' + cnt == ch;
  18. });
  19. cout << ch;
  20. cnt = (cnt + ) % ;
  21. lk.unlock();
  22. cv.notify_one();
  23. }
  24. }
  25.  
  26. int main(void) {
  27. thread t1(work, 'A', );
  28. thread t2(work, 'B', );
  29. work('C', );
  30.  
  31. t1.join();
  32. t2.join();
  33.  
  34. return ;
  35. }

题目3:(google笔试题):有四个线程1、2、3、4。线程 1 的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:

A:1 2 3 4 1 2....

B:2 3 4 1 2 3....

C:3 4 1 2 3 4....

D:4 1 2 3 4 1....

代码:

  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <fstream>
  5. #include <condition_variable>
  6. using namespace std;
  7.  
  8. const string filename = "D://code//c++//ac36";
  9. condition_variable cv;
  10. std::mutex mtex;
  11.  
  12. int vis[] = {, , , };//四个文件上一次分别写入的值
  13.  
  14. ofstream f1(filename + "_A.txt");
  15. ofstream f2(filename + "_B.txt");
  16. ofstream f3(filename + "_C.txt");
  17. ofstream f4(filename + "_D.txt");
  18.  
  19. ofstream *file[];
  20.  
  21. void solve(const int x, int gg) {
  22. while(gg--){
  23. for(int i = ; i < ; ++i) {
  24.  
  25. std::unique_lock<std::mutex> lk(mtex);
  26.  
  27. if(x == ) {
  28. cv.wait(lk, [&]{
  29. return vis[i] == ;
  30. });
  31. (*file[i]) << ;
  32. vis[i] = ;
  33. lk.unlock();
  34. cv.notify_all();
  35. }else if(x == ) {
  36. cv.wait(lk, [&]{
  37. return vis[i] == ;
  38. });
  39. (*file[i]) << ;
  40. vis[i] = ;
  41. lk.unlock();
  42. cv.notify_all();
  43. }else if(x == ) {
  44. cv.wait(lk, [&]{
  45. return vis[i] == ;
  46. });
  47. (*file[i]) << ;
  48. vis[i] = ;
  49. lk.unlock();
  50. cv.notify_all();
  51. }else {
  52. cv.wait(lk, [&]{
  53. return vis[i] == ;
  54. });
  55. (*file[i]) << ;
  56. vis[i] = ;
  57. lk.unlock();
  58. cv.notify_all();
  59. }
  60. }
  61. }
  62. }
  63.  
  64. int main(void) {
  65. file[] = &f1;
  66. file[] = &f2;
  67. file[] = &f3;
  68. file[] = &f4;
  69.  
  70. thread t1(solve, , );
  71. thread t2(solve, , );
  72. thread t3(solve, , );
  73. solve(, );
  74.  
  75. t1.join();
  76. t2.join();
  77. t3.join();
  78.  
  79. for(int i = ; i < ; ++i) {
  80. file[i]->close();
  81. }
  82. return ;
  83. }

题目4:有一个写者很多读者,多个读者可以同时读文件,但写者在写文件时不允许有读者在读文件,同样有读者读时写者也不能写

代码:

  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <fstream>
  5. #include <stdio.h>
  6. #include <condition_variable>
  7. using namespace std;
  8.  
  9. const string filename = "D://code//c++//ac36.txt";
  10. std::condition_variable cv;
  11. std::mutex mtx;
  12. int cnt = ;
  13.  
  14. ifstream in(filename);
  15. ofstream out(filename, ios::app);
  16.  
  17. void write() {
  18. char ch;
  19. while(true) {
  20. std::unique_lock<std::mutex> lk(mtx);
  21. ch = getchar();
  22. out << ch;
  23. ++cnt;
  24. lk.unlock();
  25. cv.notify_all();
  26. }
  27. }
  28.  
  29. void read() {
  30. char ch;
  31. while(true) {
  32. std::unique_lock<std::mutex> lk(mtx);
  33. cv.wait(lk, [&]{
  34. return cnt > ;
  35. });
  36. in >> ch;
  37. cout << "cout: " << ch << endl;
  38. --cnt;
  39. }
  40. }
  41.  
  42. int main(void) {
  43. cnt = in.tellg();
  44.  
  45. std::thread tw(write);
  46. std::thread tr1(read);
  47. std::thread tr2(read);
  48. std::thread tr3(read);
  49.  
  50. tw.join();
  51. tr1.join();
  52. tr2.join();
  53. tr3.join();
  54.  
  55. in.close();
  56. out.close();
  57.  
  58. return ;
  59. }

题目5:

线程安全的 queue

STL 中的 queue 是非线程安全的,一个组合操作:front();  pop() 先读取队首元素然后删除队首元素,若是有多个线程执行这个组合操作的话,可能会发生执行序列交替执行,导致一些意想不到的行为。因此需要重新设计线程安全的 queue 的接口

代码:

  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <queue>
  5. #include <chrono>
  6. #include <condition_variable>
  7. using namespace std;
  8.  
  9. template <typename T>
  10. class thread_safe_queue{
  11. private:
  12. std::condition_variable cv;
  13. std::queue<T> que;
  14. std::mutex mtx;
  15.  
  16. public:
  17. thread_safe_queue() = default;
  18. thread_safe_queue(const std::queue<T> q) : que(q) {}
  19. thread_safe_queue(const thread_safe_queue &tsf) {
  20. std::unique_lock<std::mutex> lk(mtx);
  21. que = tsf.que;
  22. }
  23. ~thread_safe_queue() = default;
  24.  
  25. void push(const T &value) {
  26. std::unique_lock<std::mutex> lk(mtx);
  27. que.push(value);
  28. lk.unlock();
  29. cv.notify_all();
  30. }
  31.  
  32. T pop(void) {
  33. std::unique_lock<std::mutex> lk(mtx);
  34. cv.wait(lk, [&]{
  35. return bool(!que.empty());
  36. });
  37. T value = que.front();
  38. que.pop();
  39. return value;
  40. }
  41.  
  42. bool empty(void) {
  43. std::unique_lock<std::mutex> lk(mtx);
  44. return que.empty();
  45. }
  46. };
  47.  
  48. thread_safe_queue<int> q;
  49. std::mutex mtx;
  50.  
  51. int main(void) {
  52.  
  53. auto push_value = [&]{
  54. for(int i = ; i < ; ++i) {
  55. q.push(i);
  56. std::this_thread::sleep_for(std::chrono::seconds());
  57. }
  58. };
  59.  
  60. auto pop_value = [&]{
  61. while() {
  62. while(!q.empty()) {
  63. std::unique_lock<std::mutex> lk(mtx);
  64. cout << q.pop() << '\n';
  65. }
  66. }
  67. };
  68.  
  69. thread push_thread1(push_value);
  70. thread pop_thread1(pop_value);
  71. thread pop_thread2(pop_value);
  72. thread pop_thread3(pop_value);
  73.  
  74. push_thread1.join();
  75. pop_thread1.join();
  76. pop_thread2.join();
  77. pop_thread3.join();
  78.  
  79. return ;
  80. }

题目6:编写程序完成如下功能:

1)有一int型全局变量g_Flag初始值为0

2) 在主线称中起动线程1,打印“this is thread1”,并将g_Flag设置为1

3) 在主线称中启动线程2,打印“this is thread2”,并将g_Flag设置为2

4) 线程序1需要在线程2退出后才能退出

5) 主线程在检测到g_Flag从1变为2,或者从2变为1的时候退出

代码1:

  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <condition_variable>
  5. using namespace std;
  6.  
  7. std::condition_variable cv;
  8. std::mutex metx;
  9. int g_Flag = ;
  10. int cnt = ;
  11. bool flag = false;
  12.  
  13. int main(void) {
  14. thread t1([&]{
  15. std::unique_lock<std::mutex> lk(metx);
  16. cout << "this is thread1\n";
  17. g_Flag = ;
  18. ++cnt;
  19. cv.wait(lk, [&]{{
  20. return flag;
  21. }});
  22. cout << "thread1 exit\n";
  23. });
  24.  
  25. thread t2([&]{
  26. std::unique_lock<std::mutex> lk(metx);
  27. cout << "this is thread2\n";
  28. g_Flag = ;
  29. cnt++;
  30. flag = true;
  31. cv.notify_all();
  32. cout << "thread2 exit\n";
  33. });
  34.  
  35. t1.detach();//分离线程
  36. t2.detach();
  37.  
  38. std::unique_lock<std::mutex> lc(metx);
  39. cv.wait(lc, [&]{
  40. return cnt >= ;
  41. });
  42. cout << "main thread exit\n";
  43.  
  44. return ;
  45. }

代码2:

  1. #include <iostream>
  2. #include <thread>
  3. #include <mutex>
  4. #include <atomic>
  5. #include <future>
  6. using namespace std;
  7.  
  8. atomic<int> g_Flag(), cnt();
  9.  
  10. void thread1(std::future<int> fu) {
  11. cout << "this is thread1\n";
  12. g_Flag = ;
  13. ++cnt;
  14. fu.get();//线程1阻塞至线程2设置共享状态, get等待异步操作结束并返回结果
  15. cout << "thread1 exit\n";
  16. }
  17.  
  18. void thread2(std::promise<int> pro) {
  19. cout << "this is thread2\n";
  20. g_Flag = ;
  21. ++cnt;
  22. cout << "thread2 exit\n";
  23. pro.set_value();//设置共享值
  24. }
  25.  
  26. int main(void) {
  27. std::promise<int> prom;//创建一个promise对象
  28. std::future<int> fu = prom.get_future();//获得promise内部的future,fut将和prom共享prom中的共享状态
  29.  
  30. std::thread t1(thread1, std::move(fu));//通过fut在线程1中得到线程2的状态
  31. std::thread t2(thread2, std::move(prom));//通过prom设置线程2中的共享状态
  32.  
  33. t1.detach();
  34. t2.detach();
  35.  
  36. while(cnt < );
  37. cout << "main thread exit\n";
  38.  
  39. return ;
  40. }

一些c++多线程习题的更多相关文章

  1. Java多线程习题 ===重点 ,错题积累

    多线程重点,错题分析 1: 2: 3: 4: 5: 6: 7: 8: 9: 10: . 12: 13: 14: 15:

  2. Java并发相关知识点梳理和研究

    1. 知识点思维导图 (图比较大,可以右键在新窗口打开) 2. 经典的wait()/notify()/notifyAll()实现生产者/消费者编程范式深入分析 & synchronized 注 ...

  3. 算法(第四版)C# 习题题解——2.3

    写在前面 整个项目都托管在了 Github 上:https://github.com/ikesnowy/Algorithms-4th-Edition-in-Csharp 查找更为方便的版本见:http ...

  4. Java多线程之内存可见性和原子性:Synchronized和Volatile的比较

    Java多线程之内存可见性和原子性:Synchronized和Volatile的比较     [尊重原创,转载请注明出处]http://blog.csdn.net/guyuealian/article ...

  5. Java精通并发-多线程同步关系实例剖析与详解

    在上一次https://www.cnblogs.com/webor2006/p/11422587.html中通过实践来解了一个案例,先来回顾一下习题: 编写一个多线程程序,实现这样的一个目标: 1.存 ...

  6. Java习题练习

    Java习题练习 1. 依赖注入和控制反转是同一概念: 依赖注入和控制反转是对同一件事情的不同描述,从某个方面讲,就是它们描述的角度不同.依赖注入是从应用程序的角度在描述,可以把依赖注入描述完整点:应 ...

  7. Java习题10.25

    Java习题10.25 1. 实际上这道题考查的是两同两小一大原则: 方法名相同,参数类型相同 子类返回类型小于等于父类方法返回类型, 子类抛出异常小于等于父类方法抛出异常, 子类访问权限大于等于父类 ...

  8. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  9. 多线程爬坑之路-Thread和Runable源码解析之基本方法的运用实例

    前面的文章:多线程爬坑之路-学习多线程需要来了解哪些东西?(concurrent并发包的数据结构和线程池,Locks锁,Atomic原子类) 多线程爬坑之路-Thread和Runable源码解析 前面 ...

随机推荐

  1. requirejs——基础

    一.requirejs存在的意义: 我们引用外部JS文件通常是这样引用的: <script src="1.js"></script> <script ...

  2. DataReader方式 获取数据

    /// /// 得到一个对象实体 DataReader方式 /// /// /// 成功返回对象模型,失败返回null public DotNet.Model.Base_Department GetM ...

  3. chart左侧

    左侧单位 Chart1.Axes.Left.Minimum := ; Chart1.Axes.Left.Maximum := Series1.YValues.MaxValue * ; Chart1.A ...

  4. WebRTC相关的基础知识点

    这里主要用来记录自己整理的和webRTC相关的一些基本的知识点,后续整理的一些基础和零碎的知识点都会更新在这里.内容大部分来自于webRTC官网.w3c以及一些前辈们的博客中的文章和相关书籍等. 20 ...

  5. java之飞机大战的记分标准

    import java.awt.Image; import java.util.ArrayList; import java.util.Timer; import javax.swing.ImageI ...

  6. 【转发】徐汉彬:Web系统大规模并发——电商秒杀与抢购

    徐汉彬:Web系统大规模并发——电商秒杀与抢购 发表于2014-12-02 09:30| 73110次阅读| 来源CSDN| 114 条评论| 作者徐汉彬 问底徐汉彬大数据 摘要:电商的秒杀和抢购,从 ...

  7. python子进程模块subprocess详解与应用实例 之一

    subprocess--子进程管理器 一.subprocess 模块简介 subprocess最早是在2.4版本中引入的. subprocess模块用来生成子进程,并可以通过管道连接它们的输入/输出/ ...

  8. 简单tarjan》一道裸题(BZOJ1051)(easy)

    这是一道水题,实际考察的是你会不会写强连通分量...(在BZOJ上又水了一道题) Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...

  9. Tensorflow训练结果测试

    代码参考(https://blog.csdn.net/disiwei1012/article/details/79928679) import osimport sysimport randomimp ...

  10. 217. Contains Duplicate数组重复元素 123

    [抄题]: Given an array of integers, find if the array contains any duplicates. Your function should re ...