您尚未登录。

楼主 #1 2018-06-21 17:32:16

晕哥
管理员
注册时间: 2017-09-06
已发帖子: 9,350
积分: 9202

基于pthread做一个简单Linux的命令行控制的音乐播放器.

#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.
-----------------





离线

楼主 #2 2018-06-21 18:45:49

晕哥
管理员
注册时间: 2017-09-06
已发帖子: 9,350
积分: 9202

Re: 基于pthread做一个简单Linux的命令行控制的音乐播放器.

#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
分别是播放、重新播放、停止命令.

基本可以工作。





离线

楼主 #3 2018-06-22 10:26:36

晕哥
管理员
注册时间: 2017-09-06
已发帖子: 9,350
积分: 9202

Re: 基于pthread做一个简单Linux的命令行控制的音乐播放器.

#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, 任何时候可以中断前面一个播放任务,开启新的播放任务。





离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn