Using Threads(Use, Creation, etc) Making a thread run over the main
I am trying to get a cin running without interupting the cout (a dialogue for example or instructions).
If you do just cin >> a; while your cout(s) are running through it, it pauses the code to wait for user input, so I was directed to a new thread for it, as I am new and it's my first time using threads finally figuring it out I still have an issue, it waits for the main thread to finish before it completes the if statement (exit) but it will give the cout instantly.
Any help is appreciated.
#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/30818.html