【发布时间】:2014-12-20 22:11:58
【问题描述】:
我想从线程向主程序“发送消息”给scanf,我在问如何给“scanf”函数或“cin”函数一些停止等待的东西。
您通常在控制台上写一些东西,然后按“回车”。 我怎样才能从另一个线程做同样的事情?
例子:
int main()
{
///// Some code to make the thread work ecc
std::cin >> mystring;
std::cout << mystring; // It should be "Text into mystring";
}
// From the other thread running...
void mythread()
{
std::string test = "Text into mystring";
// Write test to scanf! How?
}
我怎样才能做到这一点??
【问题讨论】:
-
您的问题需要澄清一下。说“向
scanf发送消息”是没有意义的,而且,无论如何,您也不想将scanf与C++std::string一起使用。我真的无法确定你在这里问什么。 -
@frasnian 已编辑。让我知道是否更清楚
-
std::cin的输入通常来自键盘或其他文件,而不是程序本身。您是否只想使用来自mythread()的字符串设置mystring。如果是这样,您应该使用std::async()并使用get()从返回的future 中获取值。有mythread()返回mystring。
标签: c++ c multithreading