我想以某种方式使用来自pthread库的读写器锁,编写者优先于读者。我在我的手册页中读到了
如果支持线程执行调度选项,并且锁中涉及的线程正在使用调度策略SCHED_FIFO或SCHED_RR执行,则如果编写者持有锁或者编写器更高或相等,则调用线程不应获取锁。优先级被锁定;否则,调用线程将获取锁。
所以我编写了一个小函数来设置线程调度选项。
void thread_set_up(int _thread)
{
struct sched_param *_param=malloc(sizeof (struct sched_param));
int *c=malloc(sizeof(int));
*c=sched_get_priority_min(SCHED_FIFO)+1;
_param->__sched_priority=*c;
long *a=malloc(sizeof(long));
*a=syscall(SYS_gettid);
int *b=malloc(sizeof(int));
*b=SCHED_FIFO;
if (pthread_setschedparam(*a,*b,_param) == -1)
{
//depending on which thread calls this functions, few thing can happen
if (_thread == MAIN_THREAD)
client_cleanup();
else if (_thread==ACCEPT_THREAD)
{
pthread_kill(params.main_thread_id,SIGINT);
pthread_exit(NULL);
}
}
}
对不起那些a,b,c
,但我尝试了malloc
所有内容,我仍然SIGSEGV
打电话给pthread_setschedparam
,我想知道为什么?
答案 0 :(得分:1)
我不知道这些是否是您问题的确切原因,但它们应该帮助您磨练它。
(1)pthread_setschedparam
成功时返回0,否则返回正数。所以
if (pthread_setschedparam(*a,*b,_param) == -1)
永远不会执行。它应该是这样的:
if ((ret = pthread_setschedparam(*a, *b, _param)) != 0)
{ //yada yada
}
顺便说一句,它并不是100%清楚你在做什么,但pthread_kill
看起来是一种丑陋的方式尽可能地做到这一点。
(2)syscall(SYS_gettid)
获取OS threadID。 pthread__setschedparam
期望 pthreads线程ID ,这是不同的。 pthreads线程ID由数据类型pthread_create
中的pthread_self
和pthread_t
返回。更改pthread__setschedparam
以使用此类型和正确的值,看看情况是否有所改善。
(3)您需要作为特权用户运行才能更改计划。尝试以root或sudo或其他方式运行程序。