【发布时间】:2016-10-11 17:32:43
【问题描述】:
因此,我正在 Linux 上开发一个多进程实时应用程序,并且有一些操作受到 Mutexes 的保护。我正在尝试将我们的互斥体切换到 PTHREAD_PRIO_PROTECT 协议,但我一定遗漏了一些东西,因为我总是在 pthread_mutex_lock() 调用上得到 EINVAL。
以下所有代码都使用类似的错误报告检查。我将用 // 错误报告标记它以简化代码清单。每个都有以下 blob 的一些变体:
{
char message[256];
throw std::runtime_error( std::string("Error locking Mutex: ") +
strerror_r( errno, message, sizeof(message) ) );
}
这是基本的互斥量初始化代码,设置上限为 40:
pthread_mutex_t thelock;
pthread_mutexattr_t attrib;
pthread_mutexattr_init(&attrib);
if ( pthread_mutexattr_setprotocol(&attrib, PTHREAD_PRIO_PROTECT) )
{
// Error Reporting
}
if ( pthread_mutexattr_setprioceiling(&attrib, 40) )
{
// Error Reporting
}
pthread_mutex_init(&thelock, &attrib);
在当前线程上设置优先级为 20(远低于上限)的调度:
struct sched_param param;
param.sched_priority = 20;
if ( -1 == sched_setscheduler(0, SCHED_FIFO, ¶m) )
{
// Error Reporting
}
我还尝试了未设置显式调度程序的线程,并在创建之前在线程属性上设置调度程序/参数。
无论我采用哪种方式,我最终都会得到以下代码的 EINVAL:
if ( pthread_mutex_lock(&thelock) )
{
// Error Reporting
}
我在运行前设置了我的测试程序的功能,以便它能够正确更改优先级:
sudo setcap 'cap_sys_nice=eip' threadpriotest
【问题讨论】: