使用用户输入控制 POSIX 个线程
Controlling POSIX threads with user input
我有一个在 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 文件并分块播放,这样你就可以打破主循环(播放 .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;
}
我有一个在 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 文件并分块播放,这样你就可以打破主循环(播放 .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;
}