我正在寻找一种使用<iostream>
语义在线程之间创建通信流的简单方法。我一直在寻找类似以下的东西:
#include <iostream>
#include <thread>
void thread1(std::istream from_main, std::ostream to_main) {
std::string s;
from_main >> s;
to_main << "Received:" << s << std::endl;
}
int main() {
std::istream from_thread;
std::ostream to_thread;
std::thread t(thread1, to_thread, from_thread);
to_thread << "Hi-Thread\n";
std::string s;
from_thread >> s; // Received:Hi-Thread
t.join();
}
有没有一种简单的方法可以在不使用pipe
、创建文件描述符和用系统调用填充代码的情况下实现这一点?