Question: I have a `socketChannel` (java.nio.channels.SocketChannel) listening for reading requests (from multiple clients). It stores each request in a **Request Queue**. 

Also `socketChannel.configureBlocking(false)`

Then I want the multiple threads to take one request at a time from the R**equest Queue** and write to the `socketChannel`

I have read the following from a documentation.

> 
> Socket channels are safe for use by multiple concurrent threads. They
>  support concurrent reading and writing, though at most one thread may
>  be reading and at most one thread may be writing at any given time.
> 
> 
> 

Since only 1 thread can be written, what can I do in the case of multiple writes?

Answer: 
Check the return of scanf to make sure it was successful. If not, the input stream will need to be cleaned of pending characters. 

```
#include <stdio.h>

typedef struct{
    char movieName[20];
    int screenTime;
    float price;
    int seatNum;
} MovieTicket;

int main( void) {
    MovieTicket ticket;
    int result = 0;
    int validTime[] = {2, 5, 8, 11};
    int limit = sizeof ( validTime) / sizeof ( validTime[0]);
    int match = 0;

    do{
        match = 0;
        printf("Enter your screen time: ");
        if ( 1 == ( result = scanf("%d", &ticket.screenTime))) {
            //check for matching validTime
            for ( int each = 0; each < limit; each++) {
                if ( validTime[each] == ticket.screenTime) {
                    match = 1;
                    break;
                }
            }
            if ( !match) {
                printf("Invalid Screen Time! Enter again please!\n");
                result = 0;
            }
        }
        else {
            //scanf could not parse an int
            printf("Enter Screen Time! Enter again please!\n");
            while ( '\n' != getchar ( )) { }//clear input stream
        }

    } while ( result != 1);

    system("pause");
}

```
To make something easy to understand you can avoid using those magic numbers or wrap them in a meaningful structure like `checkTime` and then use it for this kind of check. That way suppose next time you are asked to build a check time with some different times you won't have to look for hardcoded times and replace with it new things.

For example you can do something like this ,

```
struct checkTime{
  int *checkedTimes;
  int length;
  int (*checkIfTimeExists)(const struct checkTime* ch, int tm);
};
int checkTimerExistAux(const struct checkTime* ch,int tm)
{
    for(int i=0;i<ch->length;i++)
      if(ch->checktedTimes[i]==tm) return 1;
    return 0;
}

Then you create an `initilaize` function which will allocate memory and do this hardcoded thing. (like putting times and all that)[2,5,8,11] in your case.

initialize(); // here you created the structure `checkTimeInst`
// struct checkTime* checkTimeInstPtr = malloc(sizeof(struct checkTime));
// checkTimeInstPtr->checkedTimes = malloc(sizeof(int)*N);
// checkTimeInstPtr->length = 0;
// checkTimeInstPtr->checkIfTimeExists = checkTimerExistAux;
 do{
printf("Enter your screen time: ");
scanf("%d", &ticket.screenTime);

if(checkTimeInstPtr->checkIfTimeExists(checkTimeInstPtr,ticket.screenTime)==0){
    printf("Invalid Screen Time! Enter again please!\n");
}

} while(checkTimeInstPtr->checkIfTimeExists(checkTimeInstPtr,ticket.screenTime)==0);

```

---

This is not about speed up but it is about maintainability.

Here there is nothing that you can do about speed up. But yes if you are trying to squeeze time even more try to use the reader or scanner yourself using `getchar()` or similar. 

---

Initially the code posted assumed C++ where as OP asked for C language specific answer or modification. I have modified it to follow that criteria after **Paul Griffiths** comment.