1

My code is:

int check_barcode_flag = 1;
pthread_t idtb = -2;
static int thread_barcode_open = 0;
int quit_barcode_flag = 0;
int result = 0;
int scan_fd = -1;

void thread_Barcode(){
    char barcode[SCN_BCD_SZ];
    char code[SCN_BCD_SZ];
    int i = 0;
    struct input_event ev;
    char *device = "/dev/event1";
    int rd;
    char mtemp[128];

    scan_fd = open(device, O_RDONLY);
    if (scan_fd == -1) {
        printf("Failed to open event device.\n");
        huy_debug_com_opened("Failed to open event device.");
        exit(1);
    }
    huy_debug_com_opened("open event device.");
    while (1){      
        if (quit_barcode_flag == 1)
        {
            huy_debug_com_opened("huhuhu why?");
            break;
        }

        read(scan_fd, &ev, sizeof(struct input_event));
        if (ev.type == 1 && ev.value == 1){
            if (ev.code != 28 && ev.code != 0 && ev.code != 42)
            {
                code[i++] = keycodelist(ev.code);                           
            }           

            if (ev.code == 28){
                WM_ClrLine(LINE4, LINE7);
                sprintf(Barcode_Code, "%s", code);
                WM_DispString(NULL, Barcode_Code, LINE4, 0, LEFT_DISP, NORMAL_DISP);
                sprintf(mtemp, "BARCODE = %s", code);
                i = 0;
                memset(code, 0, SCN_BCD_SZ);                
            }
        }
    }
}

void create_BarcodeThread(void)
{
    int ret;

    printf("create_timeThread  111\n");

    if (thread_barcode_open == 0)
    {
        printf("create_timeThread  222\n");
        quit_barcode_flag = 0;
        ret = pthread_create(&idtb, NULL, (void *)thread_Barcode, NULL);
        printf("create_timeThread  333\n");
        if (ret != 0)
            printf("Create pthread error!\n");
        else
        {
            check_barcode_flag = 1;
            thread_barcode_open = 1;
            printf("Create pthread success!\n");
        }
    }
    printf("create_timeThread  444\n");
}

void quit_BarcodeThread(void)
{
    printf("quit_timeThread\n");
    quit_barcode_flag = 1;
    if (thread_barcode_open)
    {
        pthread_join(idtb, NULL);
    }

    idtb = -2;
    thread_barcode_open = 0;
}

In main(), I create pthread with create_BarcodeThread(). I use my device to scan barcode is ok. After, I call quit_BarcodeThread() to exit thread, but read(scan_fd, &ev, sizeof(struct input_event)) is waiting, and I can not exit thread immediately. I must scan barcode to read() run, and then quit_barcode_flag == 1, now I break thread.

How to read() is not waiting or I can exit pthread immediately ?

pilcrow
  • 56,591
  • 13
  • 94
  • 135
  • You can make the descriptor *non-blocking*, and use e.g. `select` with a short timeout to find out when there's data to read from the device. – Some programmer dude Dec 14 '14 at 18:31
  • 2
    take a look at [pselect](http://linux.die.net/man/2/select) you might need to use signals. – Iharob Al Asimi Dec 14 '14 at 18:31
  • Could you use a second thread to pipe an EOF to the file? – i_am_jorf Dec 14 '14 at 19:07
  • one possibility: use select(), with a short timeout value, to determine if some data is available to read. on select() timeout, check the exit code. This is not quite immediate, but will take no longer than the timeout value used with select() – user3629249 Dec 15 '14 at 04:30

1 Answers1

1

Even though the best practice for this is to use select, your next best bet is to use pthread_cancel. Just call it on idtb before you invoke pthread_join and it will send a signal to the thread asking it to stop, performing all the cleanup it can as it does so.

randomusername
  • 7,927
  • 23
  • 50