Get user input without blocking while loop using threads in C

odd

I would like to get a value from the user, i.e. setpoint for a variable, inside a while loop without blocking other tasks to perform. I am trying to use pthreads and my trial resulted in a failure. Even though I am utilizing pthread, the program is blocked by the scanf function.

This is how I create the pthread inside the main() function

uint16_t refAngle = 0;
char refAngleString[64];

int main(void)
{
   pthread_t thread_id;

   while(1) {
       pthread_create(&thread_id, NULL, threadUserInput, NULL);
       pthread_join(thread_id, NULL);

       // Other functions were called below ...
   }
}

Then I have the thread function named threadUserInput

void *threadUserInput(void* vargp)
{
    scanf("%s", refAngleString);
    refAngle = (uint16_t) atoi(refAngleString);
    printf("Angle is: %d\n", refAngle);

    return NULL;
}

Any help would be appreciated, thanks in advance.

John Bollinger

Even though I am utilizing pthread, the program is blocked by the scanf function.

Well, yes. The created thread is blocked in scanf(), and the parent thread is blocked in pthread_join(), waiting for the other. I'm having trouble coming up with any good reason for launching a single thread and then immediately joining it, as opposed to simply calling the thread function directly.

If you want to take user input once per iteration of the loop, but perform some other processing (within the same iteration) without waiting for that input, then the solution is to move the pthread_join() call past all the work that can be done before the user input is received:

   while (1) {
       pthread_create(&thread_id, NULL, threadUserInput, NULL);

       // do work that does not require the user input ...

       pthread_join(thread_id, NULL);

       // do work that _does_ require the user input (if any) ...
   }

Alternatively, perhaps you're looking for something even more decoupled, with the loop churning through as many iterations as it likes until input becomes available. In that case, you should start the I/O thread outside the loop and just keep it running, reading input after input. Have it provide some kind of signal when there is input available for the main thread to consume. Schematically, that might look like this:

   pthread_create(&thread_id, NULL, threadAllUserInput, NULL);

   while (1) {

       // ... some work ...

       if (get_input_if_available(/* arguments */)) {
           // handle user input ...
       }

       // ... more work ...
   }

   force_input_thread_to_stop();
   pthread_join(thread_id, NULL);

I omit all details of how get_input_if_available() and force_input_thread_to_stop() might be implemented. There are multiple alternatives, some of which will be better suited to your particular needs than are others.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Taking input from user using do while loop in c

From Dev

input inside of while loop without waiting for user

From Dev

Get user input to stop a while loop

From Dev

Can you simultaneously get user input while also running a function in C++ for a windows program without using GetAsyncKeyState or threading?

From Dev

user input using while loop to store data

From Dev

Using while loop to write user input to a file

From Dev

Break while loop using user input

From Dev

while loop not prompting for user input (c++)

From Dev

While loop user input?

From Dev

lock with while loop on multthread application form c# blocking all threads on single core machine

From Dev

Creating folders from user input using while or do while loop

From Java

Interrupt cin while loop without the user entering input

From Java

Detecting a particular user input without first processing it in a while loop

From Dev

How to stop a while loop with a user input without interrupting the program?

From Dev

Using while loop to make user input only an existing user

From Dev

Python, exit While loop with User Input using multithreading (cntrl+c wont work)

From Dev

How to stop recvfrom() from blocking while using in threads with UDP server in C linux?

From Java

How to close a while loop using user input for doubles

From Java

using a while loop for user to input multiple int and find the max and min

From Dev

Java - losing first user input using a while loop

From Dev

While loop using a counter utilizing user's input of an integer

From Dev

Check for user input while filling an empty list using a for loop with range

From Dev

Palindrome checker using user input and while loop in bash

From Java

While loop skips user input

From Dev

Getting user input in a while loop

From Java

Iterating a While Loop with User Input

From Dev

Python while loop with user input

From Dev

python while loop user input

From Dev

While loop for integers with user input

Related Related

  1. 1

    Taking input from user using do while loop in c

  2. 2

    input inside of while loop without waiting for user

  3. 3

    Get user input to stop a while loop

  4. 4

    Can you simultaneously get user input while also running a function in C++ for a windows program without using GetAsyncKeyState or threading?

  5. 5

    user input using while loop to store data

  6. 6

    Using while loop to write user input to a file

  7. 7

    Break while loop using user input

  8. 8

    while loop not prompting for user input (c++)

  9. 9

    While loop user input?

  10. 10

    lock with while loop on multthread application form c# blocking all threads on single core machine

  11. 11

    Creating folders from user input using while or do while loop

  12. 12

    Interrupt cin while loop without the user entering input

  13. 13

    Detecting a particular user input without first processing it in a while loop

  14. 14

    How to stop a while loop with a user input without interrupting the program?

  15. 15

    Using while loop to make user input only an existing user

  16. 16

    Python, exit While loop with User Input using multithreading (cntrl+c wont work)

  17. 17

    How to stop recvfrom() from blocking while using in threads with UDP server in C linux?

  18. 18

    How to close a while loop using user input for doubles

  19. 19

    using a while loop for user to input multiple int and find the max and min

  20. 20

    Java - losing first user input using a while loop

  21. 21

    While loop using a counter utilizing user's input of an integer

  22. 22

    Check for user input while filling an empty list using a for loop with range

  23. 23

    Palindrome checker using user input and while loop in bash

  24. 24

    While loop skips user input

  25. 25

    Getting user input in a while loop

  26. 26

    Iterating a While Loop with User Input

  27. 27

    Python while loop with user input

  28. 28

    python while loop user input

  29. 29

    While loop for integers with user input

HotTag

Archive