How can a C program poll for user input while simultaneously performing other actions in a Linux environment? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How do I prompt for Yes/No/Cancel input in a Linux shell script?How can I use grep to show just filenames on Linux?reading input with fgets(goes into endless loop)C - how to handle user input in a while loopHow to interrupt loop/process using terminal input in C++ on a Linux applicationDealing with user input on linuxHow to scan for input while looping (C Program)How to use fgets() to control the execution of while loop through user input in c?Stopping Linux console from echoing input during program executionHow can I poll keyboard input in c?

How to react to hostile behavior from a senior developer?

Why do early math courses focus on the cross sections of a cone and not on other 3D objects?

Amount of permutations on an NxNxN Rubik's Cube

If Windows 7 doesn't support WSL, then what does Linux subsystem option mean?

What's the meaning of "fortified infraction restraint"?

What order were files/directories outputted in dir?

Is a ledger board required if the side of my house is wood?

How does the math work when buying airline miles?

How to write the following sign?

Can anything be seen from the center of the Boötes void? How dark would it be?

Do wooden building fires get hotter than 600°C?

Question about debouncing - delay of state change

Why do we bend a book to keep it straight?

What was the first language to use conditional keywords?

What is this clumpy 20-30cm high yellow-flowered plant?

Has negative voting ever been officially implemented in elections, or seriously proposed, or even studied?

How does the secondary effect of the Heat Metal spell interact with a creature resistant/immune to fire damage?

Is there any word for a place full of confusion?

Dating a Former Employee

An adverb for when you're not exaggerating

Crossing US/Canada Border for less than 24 hours

Why should I vote and accept answers?

Can the Great Weapon Master feat's damage bonus and accuracy penalty apply to attacks from the Spiritual Weapon spell?

AppleTVs create a chatty alternate WiFi network



How can a C program poll for user input while simultaneously performing other actions in a Linux environment?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How do I prompt for Yes/No/Cancel input in a Linux shell script?How can I use grep to show just filenames on Linux?reading input with fgets(goes into endless loop)C - how to handle user input in a while loopHow to interrupt loop/process using terminal input in C++ on a Linux applicationDealing with user input on linuxHow to scan for input while looping (C Program)How to use fgets() to control the execution of while loop through user input in c?Stopping Linux console from echoing input during program executionHow can I poll keyboard input in c?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








12















Background:



I'm a relatively inexperienced developer trying to write software to interface with a PCI motion controller. I'm using C (compiled with gcc) on Ubuntu Linux 18.04.



The program I'm writing needs to regularly check for unsolicited status messages sent by the motion controller (approx. once per second) and display any messages it finds on a terminal screen (for which I'm using the ncurses library).



What I have:



Right now, to do this, I'm calling a function that checks for unsolicited messages in a while loop. The code is roughly akin to:



while (1)

// check for messages from PCI and store them in a traffic buffer
checkForMessages(PCIconnection, trafficBuffer);

// output the traffic buffer to the screen
printf("%s", trafficBuffer);



What I need:



I need the user to be prompted for input in a way that allows them to end the loop. For example, the user could input end causing the loop to stop and the program to continue.



The problem:



I'm not aware of a way to achieve this without putting fgets inside the while loop, causing the program to stop and wait for the user to input something on every loop iteration.



I've looked for a solution, but I haven't been able to find discussion on how to achieve the functionality I need. Opening a new thread or process seems like a step in the right direction?



I'm open to completely restructuring my code if what I'm currently doing is poor practice.



Thank you for any help!










share|improve this question







New contributor




josephsturm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


























    12















    Background:



    I'm a relatively inexperienced developer trying to write software to interface with a PCI motion controller. I'm using C (compiled with gcc) on Ubuntu Linux 18.04.



    The program I'm writing needs to regularly check for unsolicited status messages sent by the motion controller (approx. once per second) and display any messages it finds on a terminal screen (for which I'm using the ncurses library).



    What I have:



    Right now, to do this, I'm calling a function that checks for unsolicited messages in a while loop. The code is roughly akin to:



    while (1)

    // check for messages from PCI and store them in a traffic buffer
    checkForMessages(PCIconnection, trafficBuffer);

    // output the traffic buffer to the screen
    printf("%s", trafficBuffer);



    What I need:



    I need the user to be prompted for input in a way that allows them to end the loop. For example, the user could input end causing the loop to stop and the program to continue.



    The problem:



    I'm not aware of a way to achieve this without putting fgets inside the while loop, causing the program to stop and wait for the user to input something on every loop iteration.



    I've looked for a solution, but I haven't been able to find discussion on how to achieve the functionality I need. Opening a new thread or process seems like a step in the right direction?



    I'm open to completely restructuring my code if what I'm currently doing is poor practice.



    Thank you for any help!










    share|improve this question







    New contributor




    josephsturm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      12












      12








      12


      2






      Background:



      I'm a relatively inexperienced developer trying to write software to interface with a PCI motion controller. I'm using C (compiled with gcc) on Ubuntu Linux 18.04.



      The program I'm writing needs to regularly check for unsolicited status messages sent by the motion controller (approx. once per second) and display any messages it finds on a terminal screen (for which I'm using the ncurses library).



      What I have:



      Right now, to do this, I'm calling a function that checks for unsolicited messages in a while loop. The code is roughly akin to:



      while (1)

      // check for messages from PCI and store them in a traffic buffer
      checkForMessages(PCIconnection, trafficBuffer);

      // output the traffic buffer to the screen
      printf("%s", trafficBuffer);



      What I need:



      I need the user to be prompted for input in a way that allows them to end the loop. For example, the user could input end causing the loop to stop and the program to continue.



      The problem:



      I'm not aware of a way to achieve this without putting fgets inside the while loop, causing the program to stop and wait for the user to input something on every loop iteration.



      I've looked for a solution, but I haven't been able to find discussion on how to achieve the functionality I need. Opening a new thread or process seems like a step in the right direction?



      I'm open to completely restructuring my code if what I'm currently doing is poor practice.



      Thank you for any help!










      share|improve this question







      New contributor




      josephsturm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      Background:



      I'm a relatively inexperienced developer trying to write software to interface with a PCI motion controller. I'm using C (compiled with gcc) on Ubuntu Linux 18.04.



      The program I'm writing needs to regularly check for unsolicited status messages sent by the motion controller (approx. once per second) and display any messages it finds on a terminal screen (for which I'm using the ncurses library).



      What I have:



      Right now, to do this, I'm calling a function that checks for unsolicited messages in a while loop. The code is roughly akin to:



      while (1)

      // check for messages from PCI and store them in a traffic buffer
      checkForMessages(PCIconnection, trafficBuffer);

      // output the traffic buffer to the screen
      printf("%s", trafficBuffer);



      What I need:



      I need the user to be prompted for input in a way that allows them to end the loop. For example, the user could input end causing the loop to stop and the program to continue.



      The problem:



      I'm not aware of a way to achieve this without putting fgets inside the while loop, causing the program to stop and wait for the user to input something on every loop iteration.



      I've looked for a solution, but I haven't been able to find discussion on how to achieve the functionality I need. Opening a new thread or process seems like a step in the right direction?



      I'm open to completely restructuring my code if what I'm currently doing is poor practice.



      Thank you for any help!







      c linux






      share|improve this question







      New contributor




      josephsturm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      josephsturm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      josephsturm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Apr 12 at 23:30









      josephsturmjosephsturm

      635




      635




      New contributor




      josephsturm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      josephsturm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      josephsturm is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          3 Answers
          3






          active

          oldest

          votes


















          12














          Your task requires an event loop based on select or epoll. One event it would wait for is user input - when STDIN_FILENO becomes ready for read. Another is the 1-second periodic timer when you need to poll the controller.



          There are quite a few libraries that implement an event loop for you so that you can focus on what events you need to handle and how. libevent is one of the oldest, feature rich and popular.






          share|improve this answer























          • Thank you for answering! This is definitely a valid answer to the question I asked, so I'm accepting it. After a lot more reading, I also discovered that there's an answer built into ncurses itself. Calling the function timeout and passing it 0 makes getch (an ncurses input function) non-blocking, which also solved my problem.

            – josephsturm
            Apr 13 at 5:38







          • 4





            The problem with this is that you're likely to have a busy-waiting loop that takes 100% of your cpu time, unless you use some variant of sleep, which makes responding to input non-immediate in most cases. select/poll, or a library that's based on those, would almost certainly have less undesirable side effects.

            – Guntram Blohm
            Apr 13 at 7:24


















          4














          I believe that the "Unix" way would be not to ask for user input, but to react to a user signal. For example, when the user presses Ctrl-C, the currently running process receives SIGINT.



          An example how to properly use SIGINT to interrupt a loop can be found here. Copying it into the answer in case the link gets stale:



          #include <stdlib.h>
          #include <signal.h>
          #include <stdio.h>
          #include <string.h>
          #include <unistd.h>

          static volatile sig_atomic_t got_signal = 0;

          static void my_sig_handler(int signo)

          got_signal = 1;


          int main()

          struct sigaction sa;

          memset(&sa, 0, sizeof(struct sigaction));
          sa.sa_handler = &my_sig_handler;
          if (sigaction(SIGINT, &sa, NULL) == -1)
          perror("sigaction");
          return EXIT_FAILURE;


          for (;;)
          if (got_signal)
          got_signal = 0;
          printf("Received interrupt signal!n");

          printf("Doing useful stuff...n");
          sleep(1); /* Sleep is not only useful, it is essential! */

          return EXIT_SUCCESS;



          (in your case it would be a good idea to put break; into the if block or to use while(!got_signal))






          share|improve this answer






























            3














            Simple answer is multi-threading, where you have thread deployed to wait for user input, while loop continues on. So have this:



            char flag = 1;

            while (flag)
            // run the loop

            // if thing happens deploy the thread which will ask user for input




            I have not done threading in a while, I think this page would be better than me trying to explain it to you:
            https://randu.org/tutorials/threads/






            share|improve this answer


















            • 1





              But see 5 Big Fat Reasons Why Mutexes Suck Big Time for a few considerations on multi-threading.

              – David C. Rankin
              Apr 13 at 0:00











            • Yes, I'd didn't have much of a feel one way or the other about where and what kind of pitfalls were involved in multithreading until that article was posted on the accu-general mailing list accu-general@accu.org. It makes some very good points on the inherent inability to validate multithreaded code.

              – David C. Rankin
              Apr 13 at 1:39











            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            josephsturm is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55660630%2fhow-can-a-c-program-poll-for-user-input-while-simultaneously-performing-other-ac%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            12














            Your task requires an event loop based on select or epoll. One event it would wait for is user input - when STDIN_FILENO becomes ready for read. Another is the 1-second periodic timer when you need to poll the controller.



            There are quite a few libraries that implement an event loop for you so that you can focus on what events you need to handle and how. libevent is one of the oldest, feature rich and popular.






            share|improve this answer























            • Thank you for answering! This is definitely a valid answer to the question I asked, so I'm accepting it. After a lot more reading, I also discovered that there's an answer built into ncurses itself. Calling the function timeout and passing it 0 makes getch (an ncurses input function) non-blocking, which also solved my problem.

              – josephsturm
              Apr 13 at 5:38







            • 4





              The problem with this is that you're likely to have a busy-waiting loop that takes 100% of your cpu time, unless you use some variant of sleep, which makes responding to input non-immediate in most cases. select/poll, or a library that's based on those, would almost certainly have less undesirable side effects.

              – Guntram Blohm
              Apr 13 at 7:24















            12














            Your task requires an event loop based on select or epoll. One event it would wait for is user input - when STDIN_FILENO becomes ready for read. Another is the 1-second periodic timer when you need to poll the controller.



            There are quite a few libraries that implement an event loop for you so that you can focus on what events you need to handle and how. libevent is one of the oldest, feature rich and popular.






            share|improve this answer























            • Thank you for answering! This is definitely a valid answer to the question I asked, so I'm accepting it. After a lot more reading, I also discovered that there's an answer built into ncurses itself. Calling the function timeout and passing it 0 makes getch (an ncurses input function) non-blocking, which also solved my problem.

              – josephsturm
              Apr 13 at 5:38







            • 4





              The problem with this is that you're likely to have a busy-waiting loop that takes 100% of your cpu time, unless you use some variant of sleep, which makes responding to input non-immediate in most cases. select/poll, or a library that's based on those, would almost certainly have less undesirable side effects.

              – Guntram Blohm
              Apr 13 at 7:24













            12












            12








            12







            Your task requires an event loop based on select or epoll. One event it would wait for is user input - when STDIN_FILENO becomes ready for read. Another is the 1-second periodic timer when you need to poll the controller.



            There are quite a few libraries that implement an event loop for you so that you can focus on what events you need to handle and how. libevent is one of the oldest, feature rich and popular.






            share|improve this answer













            Your task requires an event loop based on select or epoll. One event it would wait for is user input - when STDIN_FILENO becomes ready for read. Another is the 1-second periodic timer when you need to poll the controller.



            There are quite a few libraries that implement an event loop for you so that you can focus on what events you need to handle and how. libevent is one of the oldest, feature rich and popular.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Apr 12 at 23:39









            Maxim EgorushkinMaxim Egorushkin

            90.6k11104193




            90.6k11104193












            • Thank you for answering! This is definitely a valid answer to the question I asked, so I'm accepting it. After a lot more reading, I also discovered that there's an answer built into ncurses itself. Calling the function timeout and passing it 0 makes getch (an ncurses input function) non-blocking, which also solved my problem.

              – josephsturm
              Apr 13 at 5:38







            • 4





              The problem with this is that you're likely to have a busy-waiting loop that takes 100% of your cpu time, unless you use some variant of sleep, which makes responding to input non-immediate in most cases. select/poll, or a library that's based on those, would almost certainly have less undesirable side effects.

              – Guntram Blohm
              Apr 13 at 7:24

















            • Thank you for answering! This is definitely a valid answer to the question I asked, so I'm accepting it. After a lot more reading, I also discovered that there's an answer built into ncurses itself. Calling the function timeout and passing it 0 makes getch (an ncurses input function) non-blocking, which also solved my problem.

              – josephsturm
              Apr 13 at 5:38







            • 4





              The problem with this is that you're likely to have a busy-waiting loop that takes 100% of your cpu time, unless you use some variant of sleep, which makes responding to input non-immediate in most cases. select/poll, or a library that's based on those, would almost certainly have less undesirable side effects.

              – Guntram Blohm
              Apr 13 at 7:24
















            Thank you for answering! This is definitely a valid answer to the question I asked, so I'm accepting it. After a lot more reading, I also discovered that there's an answer built into ncurses itself. Calling the function timeout and passing it 0 makes getch (an ncurses input function) non-blocking, which also solved my problem.

            – josephsturm
            Apr 13 at 5:38






            Thank you for answering! This is definitely a valid answer to the question I asked, so I'm accepting it. After a lot more reading, I also discovered that there's an answer built into ncurses itself. Calling the function timeout and passing it 0 makes getch (an ncurses input function) non-blocking, which also solved my problem.

            – josephsturm
            Apr 13 at 5:38





            4




            4





            The problem with this is that you're likely to have a busy-waiting loop that takes 100% of your cpu time, unless you use some variant of sleep, which makes responding to input non-immediate in most cases. select/poll, or a library that's based on those, would almost certainly have less undesirable side effects.

            – Guntram Blohm
            Apr 13 at 7:24





            The problem with this is that you're likely to have a busy-waiting loop that takes 100% of your cpu time, unless you use some variant of sleep, which makes responding to input non-immediate in most cases. select/poll, or a library that's based on those, would almost certainly have less undesirable side effects.

            – Guntram Blohm
            Apr 13 at 7:24













            4














            I believe that the "Unix" way would be not to ask for user input, but to react to a user signal. For example, when the user presses Ctrl-C, the currently running process receives SIGINT.



            An example how to properly use SIGINT to interrupt a loop can be found here. Copying it into the answer in case the link gets stale:



            #include <stdlib.h>
            #include <signal.h>
            #include <stdio.h>
            #include <string.h>
            #include <unistd.h>

            static volatile sig_atomic_t got_signal = 0;

            static void my_sig_handler(int signo)

            got_signal = 1;


            int main()

            struct sigaction sa;

            memset(&sa, 0, sizeof(struct sigaction));
            sa.sa_handler = &my_sig_handler;
            if (sigaction(SIGINT, &sa, NULL) == -1)
            perror("sigaction");
            return EXIT_FAILURE;


            for (;;)
            if (got_signal)
            got_signal = 0;
            printf("Received interrupt signal!n");

            printf("Doing useful stuff...n");
            sleep(1); /* Sleep is not only useful, it is essential! */

            return EXIT_SUCCESS;



            (in your case it would be a good idea to put break; into the if block or to use while(!got_signal))






            share|improve this answer



























              4














              I believe that the "Unix" way would be not to ask for user input, but to react to a user signal. For example, when the user presses Ctrl-C, the currently running process receives SIGINT.



              An example how to properly use SIGINT to interrupt a loop can be found here. Copying it into the answer in case the link gets stale:



              #include <stdlib.h>
              #include <signal.h>
              #include <stdio.h>
              #include <string.h>
              #include <unistd.h>

              static volatile sig_atomic_t got_signal = 0;

              static void my_sig_handler(int signo)

              got_signal = 1;


              int main()

              struct sigaction sa;

              memset(&sa, 0, sizeof(struct sigaction));
              sa.sa_handler = &my_sig_handler;
              if (sigaction(SIGINT, &sa, NULL) == -1)
              perror("sigaction");
              return EXIT_FAILURE;


              for (;;)
              if (got_signal)
              got_signal = 0;
              printf("Received interrupt signal!n");

              printf("Doing useful stuff...n");
              sleep(1); /* Sleep is not only useful, it is essential! */

              return EXIT_SUCCESS;



              (in your case it would be a good idea to put break; into the if block or to use while(!got_signal))






              share|improve this answer

























                4












                4








                4







                I believe that the "Unix" way would be not to ask for user input, but to react to a user signal. For example, when the user presses Ctrl-C, the currently running process receives SIGINT.



                An example how to properly use SIGINT to interrupt a loop can be found here. Copying it into the answer in case the link gets stale:



                #include <stdlib.h>
                #include <signal.h>
                #include <stdio.h>
                #include <string.h>
                #include <unistd.h>

                static volatile sig_atomic_t got_signal = 0;

                static void my_sig_handler(int signo)

                got_signal = 1;


                int main()

                struct sigaction sa;

                memset(&sa, 0, sizeof(struct sigaction));
                sa.sa_handler = &my_sig_handler;
                if (sigaction(SIGINT, &sa, NULL) == -1)
                perror("sigaction");
                return EXIT_FAILURE;


                for (;;)
                if (got_signal)
                got_signal = 0;
                printf("Received interrupt signal!n");

                printf("Doing useful stuff...n");
                sleep(1); /* Sleep is not only useful, it is essential! */

                return EXIT_SUCCESS;



                (in your case it would be a good idea to put break; into the if block or to use while(!got_signal))






                share|improve this answer













                I believe that the "Unix" way would be not to ask for user input, but to react to a user signal. For example, when the user presses Ctrl-C, the currently running process receives SIGINT.



                An example how to properly use SIGINT to interrupt a loop can be found here. Copying it into the answer in case the link gets stale:



                #include <stdlib.h>
                #include <signal.h>
                #include <stdio.h>
                #include <string.h>
                #include <unistd.h>

                static volatile sig_atomic_t got_signal = 0;

                static void my_sig_handler(int signo)

                got_signal = 1;


                int main()

                struct sigaction sa;

                memset(&sa, 0, sizeof(struct sigaction));
                sa.sa_handler = &my_sig_handler;
                if (sigaction(SIGINT, &sa, NULL) == -1)
                perror("sigaction");
                return EXIT_FAILURE;


                for (;;)
                if (got_signal)
                got_signal = 0;
                printf("Received interrupt signal!n");

                printf("Doing useful stuff...n");
                sleep(1); /* Sleep is not only useful, it is essential! */

                return EXIT_SUCCESS;



                (in your case it would be a good idea to put break; into the if block or to use while(!got_signal))







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 12 at 23:56









                Kit.Kit.

                75569




                75569





















                    3














                    Simple answer is multi-threading, where you have thread deployed to wait for user input, while loop continues on. So have this:



                    char flag = 1;

                    while (flag)
                    // run the loop

                    // if thing happens deploy the thread which will ask user for input




                    I have not done threading in a while, I think this page would be better than me trying to explain it to you:
                    https://randu.org/tutorials/threads/






                    share|improve this answer


















                    • 1





                      But see 5 Big Fat Reasons Why Mutexes Suck Big Time for a few considerations on multi-threading.

                      – David C. Rankin
                      Apr 13 at 0:00











                    • Yes, I'd didn't have much of a feel one way or the other about where and what kind of pitfalls were involved in multithreading until that article was posted on the accu-general mailing list accu-general@accu.org. It makes some very good points on the inherent inability to validate multithreaded code.

                      – David C. Rankin
                      Apr 13 at 1:39















                    3














                    Simple answer is multi-threading, where you have thread deployed to wait for user input, while loop continues on. So have this:



                    char flag = 1;

                    while (flag)
                    // run the loop

                    // if thing happens deploy the thread which will ask user for input




                    I have not done threading in a while, I think this page would be better than me trying to explain it to you:
                    https://randu.org/tutorials/threads/






                    share|improve this answer


















                    • 1





                      But see 5 Big Fat Reasons Why Mutexes Suck Big Time for a few considerations on multi-threading.

                      – David C. Rankin
                      Apr 13 at 0:00











                    • Yes, I'd didn't have much of a feel one way or the other about where and what kind of pitfalls were involved in multithreading until that article was posted on the accu-general mailing list accu-general@accu.org. It makes some very good points on the inherent inability to validate multithreaded code.

                      – David C. Rankin
                      Apr 13 at 1:39













                    3












                    3








                    3







                    Simple answer is multi-threading, where you have thread deployed to wait for user input, while loop continues on. So have this:



                    char flag = 1;

                    while (flag)
                    // run the loop

                    // if thing happens deploy the thread which will ask user for input




                    I have not done threading in a while, I think this page would be better than me trying to explain it to you:
                    https://randu.org/tutorials/threads/






                    share|improve this answer













                    Simple answer is multi-threading, where you have thread deployed to wait for user input, while loop continues on. So have this:



                    char flag = 1;

                    while (flag)
                    // run the loop

                    // if thing happens deploy the thread which will ask user for input




                    I have not done threading in a while, I think this page would be better than me trying to explain it to you:
                    https://randu.org/tutorials/threads/







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Apr 12 at 23:38









                    GoxGox

                    2,41021129




                    2,41021129







                    • 1





                      But see 5 Big Fat Reasons Why Mutexes Suck Big Time for a few considerations on multi-threading.

                      – David C. Rankin
                      Apr 13 at 0:00











                    • Yes, I'd didn't have much of a feel one way or the other about where and what kind of pitfalls were involved in multithreading until that article was posted on the accu-general mailing list accu-general@accu.org. It makes some very good points on the inherent inability to validate multithreaded code.

                      – David C. Rankin
                      Apr 13 at 1:39












                    • 1





                      But see 5 Big Fat Reasons Why Mutexes Suck Big Time for a few considerations on multi-threading.

                      – David C. Rankin
                      Apr 13 at 0:00











                    • Yes, I'd didn't have much of a feel one way or the other about where and what kind of pitfalls were involved in multithreading until that article was posted on the accu-general mailing list accu-general@accu.org. It makes some very good points on the inherent inability to validate multithreaded code.

                      – David C. Rankin
                      Apr 13 at 1:39







                    1




                    1





                    But see 5 Big Fat Reasons Why Mutexes Suck Big Time for a few considerations on multi-threading.

                    – David C. Rankin
                    Apr 13 at 0:00





                    But see 5 Big Fat Reasons Why Mutexes Suck Big Time for a few considerations on multi-threading.

                    – David C. Rankin
                    Apr 13 at 0:00













                    Yes, I'd didn't have much of a feel one way or the other about where and what kind of pitfalls were involved in multithreading until that article was posted on the accu-general mailing list accu-general@accu.org. It makes some very good points on the inherent inability to validate multithreaded code.

                    – David C. Rankin
                    Apr 13 at 1:39





                    Yes, I'd didn't have much of a feel one way or the other about where and what kind of pitfalls were involved in multithreading until that article was posted on the accu-general mailing list accu-general@accu.org. It makes some very good points on the inherent inability to validate multithreaded code.

                    – David C. Rankin
                    Apr 13 at 1:39










                    josephsturm is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    josephsturm is a new contributor. Be nice, and check out our Code of Conduct.












                    josephsturm is a new contributor. Be nice, and check out our Code of Conduct.











                    josephsturm is a new contributor. Be nice, and check out our Code of Conduct.














                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55660630%2fhow-can-a-c-program-poll-for-user-input-while-simultaneously-performing-other-ac%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wikipedia:Vital articles Мазмуну Biography - Өмүр баян Philosophy and psychology - Философия жана психология Religion - Дин Social sciences - Коомдук илимдер Language and literature - Тил жана адабият Science - Илим Technology - Технология Arts and recreation - Искусство жана эс алуу History and geography - Тарых жана география Навигация менюсу

                    Bruxelas-Capital Índice Historia | Composición | Situación lingüística | Clima | Cidades irmandadas | Notas | Véxase tamén | Menú de navegacióneO uso das linguas en Bruxelas e a situación do neerlandés"Rexión de Bruxelas Capital"o orixinalSitio da rexiónPáxina de Bruselas no sitio da Oficina de Promoción Turística de Valonia e BruxelasMapa Interactivo da Rexión de Bruxelas-CapitaleeWorldCat332144929079854441105155190212ID28008674080552-90000 0001 0666 3698n94104302ID540940339365017018237

                    What should I write in an apology letter, since I have decided not to join a company after accepting an offer letterShould I keep looking after accepting a job offer?What should I do when I've been verbally told I would get an offer letter, but still haven't gotten one after 4 weeks?Do I accept an offer from a company that I am not likely to join?New job hasn't confirmed starting date and I want to give current employer as much notice as possibleHow should I address my manager in my resignation letter?HR delayed background verification, now jobless as resignedNo email communication after accepting a formal written offer. How should I phrase the call?What should I do if after receiving a verbal offer letter I am informed that my written job offer is put on hold due to some internal issues?Should I inform the current employer that I am about to resign within 1-2 weeks since I have signed the offer letter and waiting for visa?What company will do, if I send their offer letter to another company