【问题标题】:how to exit pthread immediately [duplicate]如何立即退出pthread [重复]
【发布时间】:2014-12-14 18:22:55
【问题描述】:

我的代码是:

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

main() 中,我使用create_BarcodeThread() 创建pthread。我用我的设备扫描条码没问题。 之后,我调用quit_BarcodeThread() 退出线程,但read(scan_fd, &ev, sizeof(struct input_event)) 正在等待,我无法立即退出线程。我必须扫描条形码到read()运行,然后quit_barcode_flag == 1,现在我断线了。

如何read()不等待或者我可以立即退出pthread?

【问题讨论】:

  • 您可以使描述符非阻塞,并使用例如select 有一个短暂的超时时间来找出何时有数据要从设备读取。
  • 看看pselect你可能需要使用信号。
  • 您可以使用第二个线程将 EOF 通过管道传输到文件吗?
  • 一种可能性:使用带有较短超时值的 select() 来确定是否有一些数据可供读取。在 select() 超时时,检查退出代码。这不是很直接,但不会超过 select() 使用的超时值

标签: c multithreading


【解决方案1】:

尽管最好的做法是使用select,但您的下一个最佳选择是使用pthread_cancel。只需在调用pthread_join 之前在idtb 上调用它,它就会向线程发送一个信号,要求它停止,并执行它所能做的所有清理工作。

【讨论】:

    猜你喜欢
    • 2020-04-17
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    • 2017-04-12
    • 2012-09-13
    相关资源
    最近更新 更多