您尚未登录。

楼主 # 2025-03-08 01:26:58

ubuntu
会员
注册时间: 2020-03-30
已发帖子: 288
积分: 266

一个简单的Linux线程通讯测试

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

// 共享数据及同步变量
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static char play_filename[256] = {0};
static int play_request = 0;

void* audioplay_thread(void* arg)
{
	(void)arg;

	while (1)
	{
		pthread_mutex_lock(&mutex);

		while (!play_request) {
			printf("now waiting signal ...\n");
		    pthread_cond_wait(&cond, &mutex);
			printf("signal waited!\n");
		}

		printf("in audio play thread\n");
		play_request = 0; // 重置请求标志
		pthread_mutex_unlock(&mutex);



		printf("now playing: %s\n", play_filename);
		usleep(3*1000*1000);
	}
}

// 初始化并启动播放线程
void init_playback_thread() {
    pthread_t tid;
    pthread_create(&tid, NULL, audioplay_thread, NULL);
}

// 触发播放的函数
void request_play(const char* filename) {
    pthread_mutex_lock(&mutex);
    strncpy(play_filename, filename, sizeof(play_filename)-1);
    play_filename[sizeof(play_filename)-1] = '\0';
    play_request = 1;
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);
}

int main() {
    init_playback_thread();

    // 模拟其他线程触发播放
    request_play("test.wav");

    while(1)
    {

		char ch = getchar();
		if((ch == 'a') || (ch == 'A')) {
			request_play("test.wav");
		}
    }

    return 0;
}

离线

楼主 #1 2025-03-08 02:02:38

ubuntu
会员
注册时间: 2020-03-30
已发帖子: 288
积分: 266

Re: 一个简单的Linux线程通讯测试

改为 tinyplay 播放:

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

// 共享数据及同步变量
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static char play_filename[256] = {0};
static int play_request = 0;
extern int tinyplay(int argc, const char **argv);

void* audioplay_thread(void* arg)
{
	(void)arg;

	while (1)
	{
		pthread_mutex_lock(&mutex);

		while (!play_request) {
			printf("now waiting signal ...\n");
		    pthread_cond_wait(&cond, &mutex);
			printf("signal waited!\n");
		}

		printf("in audio play thread\n");
		play_request = 0; // 重置请求标志
		pthread_mutex_unlock(&mutex);



		printf("now playing: %s\n", play_filename);
		const char* argv[] = {"play", play_filename};
		int argc = sizeof(argv) / sizeof(argv[0]);

		tinyplay(argc, argv);
		//usleep(3*1000*1000);
	}
}

// 初始化并启动播放线程
void init_playback_thread() {
    pthread_t tid;
    pthread_create(&tid, NULL, audioplay_thread, NULL);
}

// 触发播放的函数
void request_play(const char* filename) {
    pthread_mutex_lock(&mutex);
    strncpy(play_filename, filename, sizeof(play_filename)-1);
    play_filename[sizeof(play_filename)-1] = '\0';
    play_request = 1;
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mutex);
}

int main() {
    init_playback_thread();

    // 模拟其他线程触发播放
    request_play("test.wav");

    while(1)
    {

		char ch = getchar();
		if((ch == 'a') || (ch == 'A')) {
			request_play("/usr/lib/libreoffice/share/gallery/sounds/ok.wav");
		}
    }

    return 0;
}

离线

页脚

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

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