-1
私は、coutを邪魔することなく、cinを実行しようとしています(例や説明などの対話)。スレッドを使用する(使用、作成など)スレッドを主に実行する
cin >> aの場合は、コードが実行されている間にコードがユーザーの入力を待つのを一時停止するので、私は新しいスレッドであるため、スレッドを使用して初めてスレッドを作成しました。問題は、メインスレッドが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 stop\n";
system("exit");
}
else
{
cout << "this is my story\n";
}
};
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 talk\n";
usleep(10000000);
cout << "I am a nice person\n";
cout << "I like pie\n";
usleep(10000000);
cout << "At any time if i get out of line just type you good cuz to stop me\n";
if (typesome == "you good cuz")
cout << "You typed " << typesome << " I will stop";
else
cout << "this is my story\n";
usleep(10000000);
cout << "West philly born and raised\n";
return 0;
}
cout/cin関数が行うスレッドの安全性/契約性を考慮する必要があります。http://stackoverflow.com/questions/6374264/is-cout-synchronized-thread-safe – AhiyaHiya