Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C中的线程正在停止执行?_C - Fatal编程技术网

C中的线程正在停止执行?

C中的线程正在停止执行?,c,C,我有一个机器人,我用C语言通过一个特定的库来控制它 我需要一个线程来检查按钮是否被按下,如果被按下,程序将退出 这个检查应该与主线程分开进行,这样我就可以运行机器人运动代码和按钮检查代码 我做了一个小PoC,但它不工作:程序流停止,按钮检查无限期执行 这是代码。如果此代码段中缺少一些变量/函数,请不要担心,它们在实际代码中 void *brick_controller(void *vargp){ printf("Second thread is working!\n"); uin

我有一个机器人,我用C语言通过一个特定的库来控制它

我需要一个线程来检查按钮是否被按下,如果被按下,程序将退出

这个检查应该与主线程分开进行,这样我就可以运行机器人运动代码和按钮检查代码

我做了一个小PoC,但它不工作:程序流停止,按钮检查无限期执行

这是代码。如果此代码段中缺少一些变量/函数,请不要担心,它们在实际代码中

void *brick_controller(void *vargp){
    printf("Second thread is working!\n");
    uint8_t button_buffer;
    while(true)
    {
        size_t result = ev3_read_keys(&button_buffer);
        //printf("ass %d\n", buf);
        if(button_buffer == 32){
            exit(666);
        }
    }
}

int main(int argc, char** argv)
{
    printf( "Waiting the EV3 brick online...\n" );
    if ( ev3_init() < 1 ) return ( 1 );

    printf( "*** ( EV3 ) Hello! ASS ***\n" );
    ev3_sensor_init();
    ev3_tacho_init();

    app_alive = app_init();
    if (app_alive == 0)
    {
        /*int distance = 250;
        if (argc == 1)
        {} 
        else if (argc == 2)
        {
            default_speed = atoi(argv[1]);
        } 
        else if (argc == 3)
        {
            default_speed = atoi(argv[1]);
            distance = atoi(argv[2]);
        }
        else {
            printf("Too many arguments!\n");
            return 0;
        }

        printf("Speed:%d\n"
                "Distance:%d\n", default_speed, distance);
        drive(default_speed, distance);
        */
        pthread_create(&brick_controller_thread, NULL, *brick_controller, NULL); 
        pthread_join(brick_controller_thread, NULL); 

        int i = 0;
        while(i < 200){ // This never executes :(
            i++;
            printf("All is running! %d\n", i); 
        }

    } else {
        printf("App initialization failed! Error code: %d\n", app_alive);
        return 2;
    }

    ev3_uninit();
    return 0;
根据该页面:

pthread_join函数等待线程指定的线程 终止。如果该线程已经终止,那么 pthread_join立即返回。线程指定的线程 必须是可接合的


pthread_join等待线程终止,而这永远不会终止。

我建议您花一些时间阅读更多关于它的内容和作用。您不应该将pthread_join放在那里,因为您不想等待线程在此时退出。您想使用pthread_exit而不是return退出main,否则,当主线程结束时,进程的所有其他线程也将结束。