1

I'm trying to write a program in C++ which will be responsible for simulating blinkers in cars. I want it to be simple and to compile it in a console window.

Is it possible to create one thread for input which will be always active and second for output that will run simultaneously?

I wanted to use threads to solve this but it doesn't work as I would like. I have a little trouble to understand threads. If anyone could help me to fix this I would be grateful.

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;
}

Jeff Linahan
  • 3,775
  • 5
  • 37
  • 56
Kuba Głowacz
  • 49
  • 1
  • 6
  • Of course, it's possible. You can use the same terminal, you can use two terminals, you can use something like [ncurses](https://invisible-island.net/ncurses/), ... Many possible ways to achieve it. – Thomas Sablik Apr 17 '20 at 15:25
  • Does this answer your question? [Corrupted output with C++, cin, cout, threads and sync\_with\_stdio](https://stackoverflow.com/questions/14010147/corrupted-output-with-c-cin-cout-threads-and-sync-with-stdio) – Ryan Wilson Apr 17 '20 at 15:27
  • @ThomasSablik a small example would be nice.. I'm kinda fresh in C++ and I don't really now much about it – Kuba Głowacz Apr 17 '20 at 15:30
  • SO is the wrong site to ask for tutorials. Try it and if you get a specific problem, come back and we will try to help you. You can start with two threads, one thread for input and one thread for output. Then you can think about a text-based user interface (TUI) or about two terminals, e.g. GDB can do this. – Thomas Sablik Apr 17 '20 at 15:31
  • @ThomasSablik I added an answer with my code – Kuba Głowacz Apr 17 '20 at 16:24
  • @KubaGłowacz That should have been an edit to your question, not an answer. You may be able to reopen your question if you do so. – Ardent Coder Apr 17 '20 at 16:24
  • @ArdentCoder sorry, I did as you said – Kuba Głowacz Apr 17 '20 at 16:38
  • @KubaGłowacz Good, but I don't have enough reputation to cast a reopen vote. I'll take this to the chat and they shall take a decision. – Ardent Coder Apr 17 '20 at 16:46
  • So should i write a new question or will someone reopen it? – Kuba Głowacz Apr 17 '20 at 17:08
  • @KubaGłowacz Upto you, also consider adding the **expected output vs actual output**. – Ardent Coder Apr 17 '20 at 17:10
  • yeah, but i cant open it myself – Kuba Głowacz Apr 17 '20 at 17:16
  • @KubaGłowacz You can only edit the question to be as clear as possible, and someone (having the required reputation score) might reopen it for you. Formatting, wordings, a minimum code that reproduces your problem, the output, etc. will help you. – Ardent Coder Apr 17 '20 at 17:19
  • [Why is "Can someone help me?" not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) What is your actual question? Probably you should have a look at [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Thomas Sablik Apr 17 '20 at 18:37
  • 1
    Here is an exampe https://godbolt.org/z/GHFiqx – Thomas Sablik Apr 17 '20 at 19:52
  • Thank you, as you see you understood my question :) – Kuba Głowacz Apr 17 '20 at 21:06

1 Answers1

1

Here is a simple example code:

#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;
}

A reference to std::atomic_int is passed to both functions for communication. std::atomic_int ensures thread-safe reads and writes. At the end you should join or detach the threads.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62