使用线程(使用,创建等)使一个线程运行在主线程上

我试图让cin在不中断cout的情况下运行(例如对话或指令)。

如果你只是cin >> a; 当你的cout正在运行时,它会暂停代码以等待用户输入,所以我被引导到了一个新的线程,因为我是新手,这是我第一次使用线程,最终找出它,我仍然有一个问题,它等待主线程完成if语句(退出)之前完成,但它会立即给出cout。

任何帮助表示赞赏。

#include <QCoreApplication>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <cstdlib>
#include <thread>
#include <unistd.h>
#include <cstdlib>
using namespace std;


string typesome;
thread function1;

void function_1()
{
    //cout << "I am Running.. - CIN";
    getline(cin, typesome);
   //std::thread relc(releasecin, getline(cin, typesome));
    if (typesome == "you good cuz")
{
    cout << "You typed " << typesome << " I will stopn";
    system("exit");
}
    else
{
    cout << "this is my storyn";
}

};

int main(void)
{

    std::thread t1(function_1); // Start the CIN
   // t1.join(); // Waits for the CIN to load
    cout << "Hey type something while i talkn";
    usleep(10000000);
    cout << "I am a nice personn";
    cout << "I like pien";
    usleep(10000000);
    cout << "At any time if i get out of line just type you good cuz to stop men";
    if (typesome == "you good cuz")
    cout << "You typed " << typesome << " I will stop";
    else
    cout << "this is my storyn";
    usleep(10000000);
    cout << "West philly born and raisedn";


    return 0;

}

我想通过在线程中放一段时间来完成这个任务,所以当getline是正确的单词时,它会切换到while循环,忘记main,并且我找出了线程和所有这些!

链接地址: http://www.djcxy.com/p/30817.html

上一篇: Using Threads(Use, Creation, etc) Making a thread run over the main

下一篇: Incorrect result after serializing and deserializing time