How to give priority to privileged thread in mutex locking?

后端 未结 8 1341
失恋的感觉
失恋的感觉 2020-12-01 02:48

First of all: I am completely a newbie in mutex/multithread programming, so sorry for any error in advance...

I have a program that runs multiple threads. The threads

8条回答
  •  情书的邮戳
    2020-12-01 03:21

    Put requesting threads on a 'priority queue'. The privileged thread can get first go at the data when it's free.

    One way to do this would be withan array of ConcurrentQueues[privilegeLevel], a lock and some events.

    Any thread that wants at the data enters the lock. If the data is free, (boolean), it gets the data object and exits the lock. If the data is in use by another thread, the requesting thread pushes an event onto one of the concurrent queues, depending on its privilege level, exits the lock and waits on the event.

    When a thread wants to release its ownership of the data object, it gets the lock and iterates the array of ConcurrentQueues from the highest-privilege end down, looking for an event, (ie queue count>0). If it finds one, it signals it and exits the lock, if not, it sets the 'dataFree' boolean and and exits the lock.

    When a thread waiting on an event for access to the data is made ready, it may access the data object.

    I thnk that should work. Please, other developers, check this design and see if you can think of any races etc? I'm still suffering somewhat from 'hospitality overload' after a trip to CZ..

    Edit - probably don't even need concurrent queues because of the explicit lock across them all. Any old queue would do.

提交回复
热议问题