我有一个只有2个线程的程序。一个是主线程,第二个被用作“音乐处理器”。音乐处理器最初通过调用pthread_cond_wait休眠在条件变量上。主线程将要由另一个线程处理的数据放在一个共享变量中,并使用pthread_cond_signal唤醒该线程。
我已经在Ubuntu 16系统上构建了该程序,并且运行完美。然后,我继续使用最新的Linux内核和GLibc 2.17构建GNU系统,我需要使用LFS 8.2指令在其上运行该程序。
在此系统上运行音乐处理线程时,总是失败,并在调用pthread_cond_wait时显示“ futex设施返回了意外的错误代码”消息。那会导致这个吗?我看了遍,找不到任何解释。
编辑
这是简化的代码:
struct _audio_processor {
/* Other variables
.
.
.
*/
pthread_mutex_t frameAdvanceLock,
queuedEffectsLock;
pthread_cond_t frameAdvanceBarrier;
} __attribute__((packed));
typedef struct _audio_processor * AudioProcessor;
static void * _playbackThreadBody ( register void * p )
{
register AudioProcessor processor = NULL;
processor = (AudioProcessor) p;
while ( processor->audioThreadRunning ) {
pthread_mutex_lock ( & processor->frameAdvanceLock );
/* This is where it INVARIABLY fails. */
pthread_cond_wait ( & processor->frameAdvanceBarrier, & processor->frameAdvanceLock );
pthread_mutex_lock ( & processor->frameAdvanceLock );
/* Rest of the thread (stuff happens here that takes time).
.
.
.
*/
}
return NULL;
};
AudioProcessor CreateAudioProcessor ( void )
{
register AudioProcessor result = NULL;
register int status = -1;
pthread_attr_t attributes;
result = & _mainAudioProcessor;
/*
Other variables initialized here.
.
.
.
*/
pthread_cond_init ( & result->frameAdvanceBarrier, NULL);
pthread_mutex_init ( & result->frameAdvanceLock, NULL );
pthread_mutex_init ( & result->queuedEffectsLock, NULL );
pthread_attr_init ( & attributes );
pthread_attr_setstacksize ( & attributes, 8192 );
status = pthread_create ( & _audioProcessorThread, & attributes, _playbackThreadBody, result );
sched_yield ();
return result;
};
void AudioProcessorPlaybackMusic ( register const AudioProcessor processor )
{
register int status = -1;
pthread_cond_signal ( & processor->frameAdvanceBarrier );
};