0
我啓動了兩個線程,thread t1
正在等待通過cin
輸入。我可以把類似EOF
位的東西從thread t2
的cin
停止cin
從閱讀?我試過'\n'
和ios::eofbit
。兩者都不起作用。如何從另一個線程停止std :: cin從讀取輸入?
#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沒有cin.close()函數...請糾正我,如果我錯了 – Mario
你是對的,永遠牢記:-S –
@Mario嘗試'::接近(STDIN_FILENO) ;'或':: fclose(stdin);'或甚至':: close(0);'。看到http://stackoverflow.com/questions/288062/is-close-fclose-on-stdin-guaranteed-to-be-correct這有點激烈...... –