mercredi 22 février 2017

How to stop, from another thread, std::cin from reading anymore input?

I started two threads and Thread1 is waiting for input via cin. Can I put something like an EOF bit to cin from Thread2 to stop cin from reading? I tried '\n' and ios::eofbit. Both did not work.

#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);
}

Aucun commentaire:

Enregistrer un commentaire