如何允许某些线程锁定一个互斥体的使用pthreads的优先 [英] How to allow certain threads to have priority in locking a mutex use PTHREADS

查看:158
本文介绍了如何允许某些线程锁定一个互斥体的使用pthreads的优先的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设下面code是由10个线程执行。

Assume that the following code is being executed by 10 threads.

pthread_mutex_lock(&lock)
Some trivial code
pthread_mutex_unlock(&lock)

有关解释的目的,可以说线程是T1,T2,T3 ..... T10。
我的要求是,只要T1或T2或T3(即任何的T1,T2或T3的)正在等待获取锁,其它线程它T4,T5,T6 ..... T10不应能获得锁即T1,T2和T3应具有precedence在相对于其他线程获取锁

For purpose of explanations lets say the threads are T1, T2, T3.....T10. My requirement is that as long as T1 or T2 or T3( i.e any of T1, T2 or T3) is waiting for acquiring a lock, the other threads i.t T4, T5, T6.....T10 should not be able to acquire the lock i.e T1, T2 and T3 should have precedence in acquiring the lock with respect to other threads.

我想它可以通过增加线程T1,T2和T3

I guess it could be done by increasing the priority of threads T1, T2 and T3

即这里是伪code

if this thread is T1 or T2 or T3
increase its priority 
pthread_mutex_lock(&lock)
Some trivial code
pthread_mutex_unlock(&lock)
if this thread is T1 or T2 or T3 decrease it priority to normal

请注意,我想一个解决方案,是一个用于Linux平台的作品,应使用pthreads的。我真的不关心其他任何平台。

另外请注意,我真的不想让这些3个线程的实时的,我希望他们能表现出不同之处在于在上述小块code他们defualt行为(调度和优先级),我希望他们总是在获取锁precedence。

Also note that I don't really want to make these 3 threads as realtime, I want them to exhibit their defualt behaviour(scheduling and priority) except that in the above mentioned small piece of code I want them to always have precedence in acquiring lock.

我看了一下在Linux中调度策略和调度优先重点的一些手册页却无法真正使出来:(

I have read some man pages about scheduling policies and scheduling priorities in Linux but can't really make out :(

将这项工作?你能帮我完成上述任务所需的确切的pthread API?

Will this work? Can you help me with the exact pthread API required to accomplish the above task?

问候
拉里

推荐答案

据我了解,只有这样,你才能真正保证这是写的作品就像自己的锁。

As I understand it, the only way you can truly guarantee this would be to write a lock that works like that yourself.

您将需要条件变量和等待低/高优先级的线程数的计数。

You will need condition variables, and counts of the number of waiting low / high priority threads.

在您需要的概念和API方面,它比较类似于实现读/写锁(但你需要的语义是完全不同的,很明显 - 但如果你知道如何在R / W锁定工作,你就会明白如何实现你想要的)。

In terms of the concepts and APIs you'll need, it is relatively similar to implementing a read/write lock (but the semantics you need are completely different, obviously - but if you understood how the r/w lock is working, you'll understand how to implement what you want).

您可以在这里看到一个读写锁的实现:

You can see an implementation of a read write lock here:

<一个href=\"http://ptgmedia.pearsoncmg.com/images/0201633922/source$c$c/rwlock.c\">http://ptgmedia.pearsoncmg.com/images/0201633922/source$c$c/rwlock.c

在低优先级的线程,你需要等待高优先级的线程来完成,以同样的方式阅读器等作家来完成。

In the lower priority threads, you'd need to wait for high priority threads to finish, in the same way readers wait for writers to finish.

(以上code是从它采取的书也有很大的POSIX线程预订顺便说一句,<一个href=\"http://www.informit.com/store/product.aspx?isbn=0201633922\">http://www.informit.com/store/product.aspx?isbn=0201633922 )

(The book the above code is taken from it also a great posix threads book btw, http://www.informit.com/store/product.aspx?isbn=0201633922 )

这篇关于如何允许某些线程锁定一个互斥体的使用pthreads的优先的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆