我在带有回调的异步模式下使用 ALSA (snd_async_add_pcm_handler())。每个 ALSA 的回调都是从 SIGIO 信号处理程序调用的。每个回调都调用我的函数getCurrentTimeMs():
// Return current milliseconds (don't care - local time or UTC).
long long getCurrentTimeMs(void)
{
std::cout << "+"; std::cout.flush();
long long ret = 0;
#define Z
#ifdef Z
struct timespec ts;
clock_gettime( CLOCK_MONOTONIC, &ts);
ret = ts.tv_sec * 1000;
ret += ts.tv_nsec / 1000000;
#else
boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
std::cout << "."; std::cout.flush();
boost::posix_time::ptime epoch_start(boost::gregorian::date(1970,1,1));
std::cout << "."; std::cout.flush();
boost::posix_time::time_duration dur = now - epoch_start;
std::cout << "."; std::cout.flush();
ret = dur.total_milliseconds();
#endif
std::cout << "-"; std::cout.flush();
return ret;
}
如果我评论#define Z,则使用提升。在“增强模式”下,应用程序在音频播放开始后经过不可预测的时间后挂起。strace显示应用程序挂起:
写(1,“+”...,1)= 1 gettimeofday({1332627252, 660534}, NULL) = 0 futex(0xb68dba4c, FUTEX_WAIT_PRIVATE, 2, NULL <未完成 ...>
但是0xb68dba4c在所有跟踪日志中只出现了 2...3 次。futex(0xb68dba4c ...不是每次 getCurrentTimeMs() 调用都会发生的事情。但是当它发生时,一切都会挂起,并且仅在gettimeofday之后发生;我在控制台上看到“+.”,然后发生了futex。但在此之前,应用程序可以播放大量声音,在每个回调中每秒调用getCurrentTimeMs() 50 次。真是个谜……
使用#define Z我的代码。在这种情况下,应用程序运行良好 - 播放千兆字节的 WAV 文件没有挂起。
该应用程序有 2 个线程通过 boost::threadpool 运行,并且都使用getCurrentTimeMs();假设我有一些死锁错误;但我不知道#define Z会如何影响它。
编辑:
我的问题是这样回答的,我接受这个答案:
1)http://permalink.gmane.org/gmane.linux.alsa.devel/96282
2)http://answerpot.com/showthread.php? 3448138-ALSA+异步+回调+重新输入+和+死锁。