您好我想用C ++构建一个控制台服务器.. 它必须写出其他线程的输出,但也接收命令 我能怎么做?我确定我的例子是错的:/因为cin会阻止所有......
static string output = "";
//The function we want to make the thread run.
void task1(string msg){
for (int i= 0; i < 10; i++)
output += "task1 says: " + msg + "\n";
}
void task2(string msg){
char c;
cout << output << endl << "_> ";
output = "";
cin >> c;
cout << endl;
}
int main(){
// Constructs the new thread and runs it. Does not block execution.
thread t1(task1, "Hello");
thread t2(task2, "Hello");
//Makes the main thread wait for the new thread to finish execution, therefore blocks its own execution.
t1.join();
t2.join();
char c;
cin >> c;
return 0;
}