Enclave_ 2022-07-05 11:28 采纳率: 88.5%
浏览 41
已结题

c++双线程进入死循环的问题

想写一个猜数字游戏,一个线程游戏,一个线程计时,我想把线程封装到一个类里,然后一运行,设置完时间,程序就死循环了

img


下面是我的代码 不知道咋改了

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
clock_t begin, end;
double usetime;
int n,num
DWORD WINAPI monitor(LPVOID lpParameter) {
    end = clock();
    usetime = double(end - begin) / CLOCKS_PER_SEC;
}
DWORD WINAPI game(LPVOID lpParameter) {
    cout << "请输入一个数:";
    cin >> n;
    if (n > num) {cout << "太大了" << endl;} 
    else if (n < num) {cout << "太小了" << endl;}
}
class Thread {
    public:
        HANDLE h1, h2;
        void Create() {
            h2 = CreateThread(NULL, 0, game, NULL, 0, NULL);
            h1 = CreateThread(NULL, 0, monitor, NULL, 0, NULL);
        }
};
class Game : public Thread {
    public:
        int sec, flag=0;
        void Play() {
            srand(time(NULL));
            int num = rand() % 10000 + 1;
            cout << "请设置游戏时间(秒):";
            cin >> sec;
            begin = clock();
            while (n != num) {
                Create();
                if (usetime > sec) {
                    flag = 1;
                    break;
                }
            }
            if (flag) {cout << "游戏超时,该数字为" << num << endl;} 
            else {cout << "正确猜出数" << num << endl;}
        }
};
int main() {
    Game game;
    game.Play();
    system("pause");
    return 0;
}
  • 写回答

4条回答 默认 最新

  • 关注

    你的代码错误挺多的,num应该用全局变量,在play函数中,不应该重新声明新的int num了。线程应该用循环执行,否则没有任何意义。你的代码修改了一下,代码修改后的运行结果:

    img

    代码修改如下:

    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <windows.h>
    using namespace std;
    
    clock_t begintime, endtime;
    double usetime;
    int n, num;
    int g_flag = 1;
    int sec;
    DWORD WINAPI monitor(LPVOID lpParameter) {
        
        while (g_flag)
        {
            endtime = clock();
            Sleep(10); //10ms判断一次
        }
        return 0;
    }
    DWORD WINAPI game(LPVOID lpParameter) {
        while (g_flag)
        {
            cout << "请输入一个数:";
            cin >> n;
            if (n > num) { cout << "太大了" << endl; }
            else if (n < num) { cout << "太小了" << endl; }
            else {
                //cout << "猜对了" << endl; 
                g_flag = 0;
            }
        }
        
        return 0;
    }
    class Thread {
    public:
        HANDLE h1, h2;
        void Create() {
            h2 = CreateThread(NULL, 0, game, NULL, 0, NULL);
            h1 = CreateThread(NULL, 0, monitor, NULL, 0, NULL);
        }
    };
    class Game :public Thread{
    public:
        void Play() {
            srand(time(NULL));
            num = rand() % 10000 + 1;
            cout << "请设置游戏时间(秒):";
            cin >> sec;
            
            usetime = 0; //初始化时间
            begintime = clock(); //初始化时间
    
            Create();
            while (usetime < sec && g_flag)
            {
                usetime = double(endtime - begintime) / CLOCKS_PER_SEC;
            }
    
            if(g_flag == 0) { cout << "正确猜出数" << num << endl; }
            else { 
                g_flag = 0; //这里让线程结束
                cout << "游戏超时,该数字为" << num << endl;
            }
        }
    };
    int main() {
        Game game;
        game.Play();
        system("pause");
        return 0;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 7月13日
  • 已采纳回答 7月5日
  • 创建了问题 7月5日

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)