Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.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 使用用户输入控制POSIX线程_C_Multithreading_Pthreads_Posix - Fatal编程技术网

C 使用用户输入控制POSIX线程

C 使用用户输入控制POSIX线程,c,multithreading,pthreads,posix,C,Multithreading,Pthreads,Posix,我有一个使用C语言的ao_lib播放mp3的线程。我需要在播放过程中跳到下一个mp3,因此在创建播放mp3的线程后,我尝试创建另一个线程,等待用户输入字符,如果第一个线程在第二个之前加入,则第二个线程也会被终止 main() { ret1 = pthread_create(&thread1, NULL, func_play, (void *) url); ret2 = pthread_create(&thread2, NULL, func_char, NULL);

我有一个使用C语言的ao_lib播放mp3的线程。我需要在播放过程中跳到下一个mp3,因此在创建播放mp3的线程后,我尝试创建另一个线程,等待用户输入字符,如果第一个线程在第二个之前加入,则第二个线程也会被终止

main()
{
    ret1 = pthread_create(&thread1, NULL, func_play, (void *) url);
    ret2 = pthread_create(&thread2, NULL, func_char, NULL);

    /* I need to somehow do something here to break func_play if a user
       enters a specific char in func_char */

    pthread_join(thread1, NULL);
    pthread_cancel(thread2);

    return 0;
}
那没用。任何解决方案都是非常受欢迎的


谢谢

这是我认为可行的模式,我不知道mp3播放功能是如何工作的,但您需要它是非阻塞的,也许可以读取.mp3文件并分块播放,这样您就可以使用类似的方法来中断主循环(\u播放.mp3的循环

#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <string.h>

static pthread_mutex_t mutex;

struct SharedData
{
    const char *url;
    int stop;
    int ready;
};

void *
mainloop(void *data)
{
    struct SharedData *sharedData;
    int ready;
    int stop;

    sharedData = data;
    if (sharedData == NULL)
        return NULL;

    stop  = 0;
    ready = 0;
    while (ready == 0)  
     {
        pthread_mutex_lock(&mutex);
        ready = sharedData->ready;
        pthread_mutex_unlock(&mutex);

        usleep(1000);
     }

    while (stop == 0)
     {
        pthread_mutex_lock(&mutex);
        stop = sharedData->stop;
        pthread_mutex_unlock(&mutex);

        printf(".");
        fflush(stdout);
        sleep(1);
     }
    return NULL;
}

void *
controlthread(void *data)
{
    int                chr;
    struct SharedData *sharedData;

    sharedData = data;
    if (sharedData == NULL)
        return NULL;

    pthread_mutex_lock(&mutex);
    sharedData->ready = 1;
    pthread_mutex_unlock(&mutex);

    printf("Press return to stop the main loop...\n");
    while (((chr = getchar()) != EOF) && (chr != '\n'))
        usleep(1000);

    pthread_mutex_lock(&mutex);
    sharedData->stop = 1;
    pthread_mutex_unlock(&mutex);

    return NULL;
}

int main(void)
{
    struct SharedData data;
    pthread_t         threads[2];

    pthread_mutex_init(&mutex, NULL);

    memset(&data, 0, sizeof(data));

    pthread_create(&threads[0], NULL, mainloop, &data);
    pthread_create(&threads[1], NULL, controlthread, &data);

    pthread_join(threads[0], NULL);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
静态pthread_mutex_t mutex;
结构共享数据
{
const char*url;
int stop;
int就绪;
};
空虚*
主循环(无效*数据)
{
结构SharedData*SharedData;
int就绪;
int stop;
sharedData=数据;
if(sharedData==NULL)
返回NULL;
停止=0;
就绪=0;
while(ready==0)
{
pthread_mutex_lock(&mutex);
就绪=共享数据->就绪;
pthread_mutex_unlock(&mutex);
usleep(1000);
}
while(stop==0)
{
pthread_mutex_lock(&mutex);
停止=共享数据->停止;
pthread_mutex_unlock(&mutex);
printf(“.”);
fflush(stdout);
睡眠(1);
}
返回NULL;
}
空虚*
控制线程(无效*数据)
{
int-chr;
结构SharedData*SharedData;
sharedData=数据;
if(sharedData==NULL)
返回NULL;
pthread_mutex_lock(&mutex);
sharedData->ready=1;
pthread_mutex_unlock(&mutex);
printf(“按回车键停止主循环…\n”);
而(((chr=getchar())!=EOF)&&(chr!='\n'))
usleep(1000);
pthread_mutex_lock(&mutex);
sharedData->stop=1;
pthread_mutex_unlock(&mutex);
返回NULL;
}
内部主(空)
{
结构共享数据;
pthread_t线程[2];
pthread_mutex_init(&mutex,NULL);
memset(&data,0,sizeof(data));
pthread_创建(&threads[0],NULL,mainloop,&data);
pthread_创建(&threads[1],NULL,controlthread,&data);
pthread_join(线程[0],NULL);
返回0;
}

Hmm
pthread\u cancel(3)
?请发布代码模式听起来正确,我认为可能是实现的原因。@iharob添加了代码,我很困惑在注释区域中要做什么,以确保用户输入的内容是可读的,在thread1自行退出之前,您必须做的第一件事是启用编译器警告如果您使用的是gcc或clang/llvm,那么
-Wall-Werror
会有很大帮助,我发现要么您忽略警告,要么根本不启用警告,因为
main()
的签名错误,它应该返回
int
,如果它是不带参数的,那么
intmain(void)
是正确的。还请记住,没有同步保证,第二个函数可能在第一个函数之前启动。可能您需要在线程之间共享数据,例如,而不是传递
url
传递
struct
,传递带有标志的
mp3
正在播放的循环+
url
,您应该小心使用互斥锁来防止争用情况,不能允许两个线程同时尝试访问共享数据。