鱼C论坛

 找回密码
 立即注册
查看: 1286|回复: 10

[已解决]请问有了解C++/QT多线程的大佬吗?

[复制链接]
发表于 2019-8-29 12:21:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
今天遇到一个题目是,三个线程顺序打印自己的ID十次,请问有大佬会做的吗?
最佳答案
2019-8-30 12:36:41
唐宋元明沁 发表于 2019-8-30 08:19
你这个应该差不多了,只是最好能123123123这样,把锁换个位置就对了,用了auto感觉看着别扭,我不是很熟 ...

行吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-8-30 17:07:24 | 显示全部楼层
本帖最后由 唐宋元明沁 于 2019-8-30 17:09 编辑

#include <stdio.h>
#include <stdlib.h>
#include<pthread.h>
/*int pthread_create(pthread_t*restrict tidp,
*  * const pthread_attr_t *restrict_attr,
*   * void*(*start_rtn)(void*),void *restrict arg);*/
/*第一个参数为指向线程标识符的指针。
* 第二个参数用来设置线程属性。
* 第三个参数是线程运行函数的起始地址。
* 最后一个参数是运行函数的参数。*/
#define ITEM 4
struct {

    pthread_mutex_t mutex;
    pthread_cond_t cond;
    int i;
}test={
    PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZER,0
};
void PrintA(void *arg)
{

    int num=*(int*)arg;
    int i;
    for(i=0;i<10;i++){
        pthread_mutex_lock(&test.mutex);
        while(test.i!=num)
            pthread_cond_wait(&test.cond,&test.mutex);
        printf("%c",'A'+num);
        test.i=(test.i+1)%ITEM;
        pthread_mutex_unlock(&test.mutex);
        pthread_cond_broadcast(&test.cond);

    }
}
int main(void) {
    pthread_t t[ITEM];
    int i=1;
    printf("共有%d个线程\n",ITEM);
    for(i=0;i<ITEM;i++){
        int *a=(int *)malloc(sizeof(int));
        *a=i;
        pthread_create(&t,NULL,PrintA,(void*)a);
    }
    for(i=0;i<ITEM;i++){
        pthread_join(t,NULL);
    }
    return EXIT_SUCCESS;
}

这代码经过测试,可行,C++原理差不错,只是可能名字稍稍有点不同
这是Linux环境下的代码,编译的时候记得加上  -lpthread 哦     如:gcc xx.c -lpthread
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-29 12:54:20 | 显示全部楼层
  1. #include <iostream>
  2. #include <thread>

  3. void fun1(std::thread *obj)
  4. {
  5.         for(size_t i = 0; i < 10; ++i)
  6.                 std::cout << "fun1: " << std::hex << obj->get_id() << std::endl;
  7. }
  8. void fun2(std::thread *obj)
  9. {
  10.         for(size_t i = 0; i < 10; ++i)
  11.                 std::cout << "fun2: " << std::hex << obj->get_id() << std::endl;
  12. }
  13. void fun3(std::thread *obj)
  14. {
  15.         for(size_t i = 0; i < 10; ++i)
  16.                 std::cout << "fun3: " << std::hex << obj->get_id() << std::endl;
  17. }

  18. int main()
  19. {
  20.         std::thread t1(fun1, &t1);
  21.         std::thread t2(fun2, &t2);
  22.         std::thread t3(fun3, &t3);
  23.         t1.join();
  24.         t2.join();
  25.         t3.join();
  26.         return 0;
  27. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-29 12:54:54 | 显示全部楼层
  1. g++ -g -Wall -o main main.cpp -lpthread
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-29 12:56:02 | 显示全部楼层
  1. $ ./main
  2. fun1: fun3: 7f66092a3700
  3. fun3: 7f66092a3700
  4. fun3: 7f66092a3700
  5. fun3: 7f66092a3700
  6. fun3: 7f66092a3700
  7. fun3: 7f66092a3700
  8. fun3: 7f66092a3700
  9. fun3: 7f66092a3700
  10. fun3: 7f66092a3700
  11. fun3: 7f66092a3700
  12. fun2: 7f6609aa4700
  13. fun2: 7f6609aa4700
  14. fun2: 7f6609aa4700
  15. fun2: 7f6609aa4700
  16. fun2: 7f6609aa4700
  17. fun2: 7f6609aa4700
  18. fun2: 7f6609aa4700
  19. fun2: 7f6609aa4700
  20. fun2: 7f6609aa4700
  21. fun2: 7f6609aa4700
  22. 7f660a2a5700
  23. fun1: 7f660a2a5700
  24. fun1: 7f660a2a5700
  25. fun1: 7f660a2a5700
  26. fun1: 7f660a2a5700
  27. fun1: 7f660a2a5700
  28. fun1: 7f660a2a5700
  29. fun1: 7f660a2a5700
  30. fun1: 7f660a2a5700
  31. fun1: 7f660a2a5700
  32. $
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-29 13:48:48 | 显示全部楼层

楼主说的是顺序打印,应该是按线程创建的顺序打印完线程1的再到线程2的.... 而且你这个cout不弄个线程安全输出乱了。。。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-29 14:49:03 | 显示全部楼层
迷雾少年 发表于 2019-8-29 13:48
楼主说的是顺序打印,应该是按线程创建的顺序打印完线程1的再到线程2的.... 而且你这个cout不弄个线程安 ...

对,大概就是控制执行顺序,应该是要condition这种条件锁才行,我自己却没有实际操作过,今天想了半天没实现出来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-29 14:50:40 | 显示全部楼层

首先感谢您的回答,只是这个并不能保证是按照想要的样子来的,谁先抢到时间碎片就执行谁
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-29 18:22:59 | 显示全部楼层
不知道是不是你要的效果
  1. // ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
  2. //

  3. #include <iostream>
  4. #include <thread>
  5. #include <mutex>
  6. #include <ostream>
  7. #include <atomic>
  8. #include <sstream>
  9. using namespace std;

  10. mutex* g_mx;
  11. int g_cout = 0;
  12. void t_(int param)
  13. {
  14.         while (1)
  15.         {
  16.                 int t = -1;
  17.                 {
  18.                         unique_lock<std::mutex> tmp(*g_mx);
  19.                         t = g_cout;
  20.                 }
  21.                 if (t == param)
  22.                         break;

  23.                 std::this_thread::sleep_for(std::chrono::milliseconds(1));
  24.         }
  25.         //output
  26.         for(auto i=0;i<10;i++)
  27.                 cout << "thread num:" << param << ",thread id:" << this_thread::get_id()<<endl;

  28.         {
  29.                 unique_lock<std::mutex> tmp(*g_mx);
  30.                 g_cout++;
  31.         }
  32. }
  33. int main()
  34. {
  35.         thread* ar[3];
  36.         g_mx = new mutex;
  37.         for (auto i=0;i<3;i++)
  38.         {
  39.                 ar[i] = new thread(t_,i);

  40.                 ar[i]->detach();

  41.         }
  42.        
  43.         getchar();
  44.         for (auto i = 0; i < 3; i++)
  45.                 delete ar[i];

  46.         return 0;
  47. }
复制代码


  1. thread num:0,thread id:1716
  2. thread num:0,thread id:1716
  3. thread num:0,thread id:1716
  4. thread num:0,thread id:1716
  5. thread num:0,thread id:1716
  6. thread num:0,thread id:1716
  7. thread num:0,thread id:1716
  8. thread num:0,thread id:1716
  9. thread num:0,thread id:1716
  10. thread num:0,thread id:1716
  11. thread num:1,thread id:4824
  12. thread num:1,thread id:4824
  13. thread num:1,thread id:4824
  14. thread num:1,thread id:4824
  15. thread num:1,thread id:4824
  16. thread num:1,thread id:4824
  17. thread num:1,thread id:4824
  18. thread num:1,thread id:4824
  19. thread num:1,thread id:4824
  20. thread num:1,thread id:4824
  21. thread num:2,thread id:14072
  22. thread num:2,thread id:14072
  23. thread num:2,thread id:14072
  24. thread num:2,thread id:14072
  25. thread num:2,thread id:14072
  26. thread num:2,thread id:14072
  27. thread num:2,thread id:14072
  28. thread num:2,thread id:14072
  29. thread num:2,thread id:14072
  30. thread num:2,thread id:14072
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-30 08:19:05 | 显示全部楼层
迷雾少年 发表于 2019-8-29 18:22
不知道是不是你要的效果

你这个应该差不多了,只是最好能123123123这样,把锁换个位置就对了,用了auto感觉看着别扭,我不是很熟悉,等会中午了再来研究一下,我昨天试了一个C语言的成功了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-30 12:36:41 | 显示全部楼层    本楼为最佳答案   
唐宋元明沁 发表于 2019-8-30 08:19
你这个应该差不多了,只是最好能123123123这样,把锁换个位置就对了,用了auto感觉看着别扭,我不是很熟 ...

行吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 10:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表