Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有可能将cin与cout并行使用? 我试图写一个C++程序,负责模拟汽车中的眨眼。我希望它简单,并在控制台窗口中编译它_C++_Printf_Cin_Cout - Fatal编程技术网

是否有可能将cin与cout并行使用? 我试图写一个C++程序,负责模拟汽车中的眨眼。我希望它简单,并在控制台窗口中编译它

是否有可能将cin与cout并行使用? 我试图写一个C++程序,负责模拟汽车中的眨眼。我希望它简单,并在控制台窗口中编译它,c++,printf,cin,cout,C++,Printf,Cin,Cout,是否可以为始终处于活动状态的输入创建一个线程,为同时运行的输出创建第二个线程 我想用线程来解决这个问题,但它不能像我希望的那样工作。我理解线程有点困难。如果有人能帮我解决这个问题,我将不胜感激 int in() { int i; cout<<"press 1 for left blinker or 0 to turn it off: "; cin>>i; return i; } void leftBlinker() { int i

是否可以为始终处于活动状态的输入创建一个线程,为同时运行的输出创建第二个线程

我想用线程来解决这个问题,但它不能像我希望的那样工作。我理解线程有点困难。如果有人能帮我解决这个问题,我将不胜感激

int in()
{
    int i;
    cout<<"press 1 for left blinker or 0 to turn it off: ";
    cin>>i;
    return i;
}

void leftBlinker()
{
    int i;
    cout << "<-";
    Sleep(1000/3);
    cout << "  ";
    Sleep(1000/3);

}


int main()
{
    thread t1 (in);


    if (in()==1)
    {
        for (int i=0; i<100; i++)
        {
            thread t2(leftBlinker);
            if (in()==0)
                break;
        }
    }

    system("pause");
    return 0;
}

int in()
{
int i;
库蒂;
返回i;
}
void leftBlinker()
{
int i;

cout下面是一个简单的示例代码:

#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>

int in(std::atomic_int &i) {
  while (true) {
    std::cout << "press 1 for left blinker or 0 to turn it off: ";
    int input;
    std::cin >> input;
    i = input;
  }
}

void leftBlinker(std::atomic_int &i) {
  while (true) {
    if (i) {
      std::cout << "<-" << std::endl;
      std::this_thread::sleep_for(std::chrono::milliseconds{333});
      std::cout << "  " << std::endl;
      std::this_thread::sleep_for(std::chrono::milliseconds{333});
    }
  }
}

int main() {
  std::atomic_int i{0};
  std::thread t1(in, std::ref(i));
  std::thread t2(leftBlinker, std::ref(i));

  t1.join();
  t2.join();
  return 0;
}
#包括
#包括
#包括
#包括
int-in(std::atomic_int&i){
while(true){
std::cout>输入;
i=输入;
}
}
void leftBlinker(标准::原子_int&i){
while(true){
如果(i){

STD:当然可以,你可以使用同一个终端,你可以使用两个终端,你可以使用类似的……很多可能的方法来实现它。这是你的问题吗?@ ThomasSablik一个小例子很好。我在C++中有点新鲜,我现在不太喜欢它,所以是错误的站点来请求教程。试试它吧。d如果您遇到特定问题,请回来,我们将尽力帮助您。您可以从两个线程开始,一个线程用于输入,一个线程用于输出。然后您可以考虑基于文本的用户界面(TUI)或两个终端,例如GDB可以做到这一点。下面是一个示例