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;
}

你的代码错误挺多的,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;
}

看了楼主的代码,估计很多语法概念的东西都还没懂,建议买一本C++ Primen plus或者C++ Prime先学习语法

if (usetime > sec) {不行吧,改为while吧,这里得堵塞一下,否则接着又while循环,再次启动新线程了

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632