J'ai commencé deux threads, thread t1
attend la saisie via cin
. Puis-je mettre quelque chose comme EOF
bit à cin
de thread t2
pour arrêter cin
de lire? J'ai essayé '\n'
et ios::eofbit
. Les deux ne fonctionnaient pas.Comment arrêter, à partir d'un autre thread, std :: cin de lire plus d'entrée?
#include <thread>
#include <iostream>
#include <string>
#include <condition_variable>
std::condition_variable cv;
void Foo()
{
std::string sFoo;
std::cin >> sFoo;
// Do stuff with sFoo
}
void Bar()
{
// Do stuff
// If a condition is fullfilled I don't need the input in Foo anymore and so I want to cancel cin.
std::cin.setstate(std::ios::setstate(std::ios::eofbit); // Does not work
std::cin.putback('\n'); // Does not work
// maybe something similar like these above
}
int main()
{
std::thread t1(Foo);
std::thread t2(Bar);
}
@KerrekSB Il n'y a pas de fonction cin.close() ... S'il vous plaît me corriger si je me trompe – Mario
Vous avez raison, cela ne tienne: -S –
@mario 'Essayez :: close (STDIN_FILENO) ; 'ou' :: fclose (stdin); 'ou même' :: close (0); '. Voir http://stackoverflow.com/questions/288062/is-close-fclose-on-stdin-guaranteed-to-be-correct C'est un peu drastique ... –