相关疑难解决方法(0)

使用两个线程运行并打破无限循环

我试图运行一个循环,直到用户选择打破它.无论用户是想要整晚运行该功能还是仅仅几秒钟,循环应该重复,直到用户决定停止它为止.

在研究解决方案时,我遇到了使用两个线程来实现这一目标.第一个线程将运行无限循环,而第二个线程等待用户输入.在接收到该输入后,第二个线程将终止第一个然后返回.

  • 如何使用第二个线程终止第一个?

#include <iostream>
#include <iomanip>
#include <ctime>
#include <thread>
#include <cstdlib>
#include <Windows.h>

using namespace std;

void timeCount()
{
    time_t rawTime;
    struct tm * timeinfo;

    do
    {
        Sleep(500);
        system("cls");
        time(&rawTime);
        cout << "Seconds passed this epoch:" << rawTime << endl << endl;
        timeinfo = localtime(&rawTime);
        cout << "The local time is:" << asctime(timeinfo) << endl;
        timeinfo = gmtime(&rawTime);
        cout << "The UTC time is :" << asctime(timeinfo) << endl;
    } while (1 != 0);

};

void getStop()
{ …
Run Code Online (Sandbox Code Playgroud)

c++ multithreading infinite-loop c++11

0
推荐指数
1
解决办法
3936
查看次数

标签 统计

c++ ×1

c++11 ×1

infinite-loop ×1

multithreading ×1