#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
pthread_t pt_play;
sem_t sem_cmd_play;
sem_t sem_cmd_stop;
void* thread_play(void* p)
{
int i = 0;
while(1)
{
if(sem_wait(&sem_cmd_play) == 0)
{
for(i=0;i<10;i++)
{
printf("now play music ...\n");
usleep(100*1000);
}
printf("play end.\n-----------------\n");
}
}
}
int main()
{
char input[16];
sem_init(&sem_cmd_play, 0, 0);
sem_init(&sem_cmd_stop, 0, 0);
pthread_create(&pt_play, NULL, thread_play, NULL);
while(1)
{
scanf("%s", input);
if(strcmp(input, "play") == 0)
{
printf("now play:\n");
sem_post(&sem_cmd_play);
}
}
}
输入play命令, 另外一个线程, 就开始模拟播放音乐:
$ ./test
play
now play:
now play music ...
now play music ...
now play music ...
now play music ...
now play music ...
now play music ...
now play music ...
now play music ...
now play music ...
now play music ...
play end.
-----------------
离线
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
pthread_t pt_play;
sem_t sem_cmd_play;
sem_t sem_stopped;
pthread_mutex_t mutex;
pthread_cond_t cond;
int stop = 0;
int start = 0;
void* thread_play(void* p)
{
int i = 0;
while(1)
{
pthread_mutex_lock(&mutex);
while (start == 0)
{
pthread_cond_wait(&cond, &mutex);
}
for(i=0;i<20;i++)
{
if(stop == 1)
break;
printf("now play music ...\n");
usleep(100*1000);
}
start = 0;
stop = 0;
printf("play end.\n-----------------\n");
sem_post(&sem_stopped);
pthread_mutex_unlock(&mutex);
}
}
int main()
{
char input[16];
struct timespec tm;
sem_init(&sem_cmd_play, 0, 0);
sem_init(&sem_stopped, 0, 0);
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
pthread_create(&pt_play, NULL, thread_play, NULL);
while(1)
{
scanf("%s", input);
if(strcmp(input, "play") == 0)
{
printf("now play:\n");
pthread_mutex_lock(&mutex);
start = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
else if(strcmp(input, "stop") == 0)
{
printf("now stop:\n");
stop = 1;
}
else if(strcmp(input, "replay") == 0)
{
printf("now stop:\n");
stop = 1;
clock_gettime(CLOCK_REALTIME, &tm);
tm.tv_nsec += 100000000;
sem_timedwait(&sem_stopped, &tm);
pthread_mutex_lock(&mutex);
stop = 0;
start = 1;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
}
}
三个命令:
play
replay
stop
分别是播放、重新播放、停止命令.
基本可以工作。
离线
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
pthread_t pt_play;
sem_t sem_cmd_play;
sem_t sem_stopped;
pthread_mutex_t mutex_play;
pthread_cond_t cond_play;
int stop = 0;
int start = 0;
void* thread_play(void* p)
{
int i = 0;
while(1)
{
pthread_mutex_lock(&mutex_play);
while (start == 0)
{
pthread_cond_wait(&cond_play, &mutex_play);
}
for(i=0;i<100;i++)
{
if(stop == 1)
break;
printf("now play music ... %d\n", i);
usleep(100*1000);
}
start = 0;
stop = 0;
printf("play end.\n-----------------\n");
sem_post(&sem_stopped);
pthread_mutex_unlock(&mutex_play);
}
}
int main()
{
char input[16];
struct timespec tm;
sem_init(&sem_cmd_play, 0, 0);
sem_init(&sem_stopped, 0, 0);
pthread_mutex_init(&mutex_play, NULL);
pthread_cond_init(&cond_play, NULL);
pthread_create(&pt_play, NULL, thread_play, NULL);
while(1)
{
scanf("%s", input);
if(strcmp(input, "play") == 0)
{
printf("now play:\n");
pthread_mutex_lock(&mutex_play);
start = 1;
pthread_cond_signal(&cond_play);
pthread_mutex_unlock(&mutex_play);
}
else if(strcmp(input, "stop") == 0)
{
printf("now stop:\n");
stop = 1;
}
else if(strcmp(input, "replay") == 0)
{
printf("now stop:\n");
stop = 1;
int wait_count = 0;
do
{
clock_gettime(CLOCK_REALTIME, &tm);
tm.tv_nsec += 100000000;
if(0 == sem_timedwait(&sem_stopped, &tm))
{
break;
}
printf("\twait %d\n", wait_count);
wait_count++;
}
while(wait_count < 10);
pthread_mutex_lock(&mutex_play);
stop = 0;
start = 1;
pthread_cond_signal(&cond_play);
pthread_mutex_unlock(&mutex_play);
}
}
}
输入 replay, 任何时候可以中断前面一个播放任务,开启新的播放任务。
离线