Critical sections and mutexes

Cliff Wells logiplexsoftware at earthlink.net
Tue Oct 23 15:39:10 EDT 2001


On Tuesday 23 October 2001 12:06, David Brady wrote:
>
> main_Q = PriorityQueue() # queueing class
>
> class ServerThread(threading.Thread):
>     def __init__(self):
>         threading.Thread.__init__(self)
>         self.Q = PriorityQueue()
>
>     def run(self):
>         # if messages waiting on socket:
>             # get messages into self.Q
>         # if self.Q has messages:
>             # if can acquire lock on main_Q:
>                 # move msgs from self.Q to main_Q
>                 # release lock on main_Q
>
> # main thread:
> while 1:
>     # if main_Q has messages:
>         # dispatch them
>

I would put a threading.Lock() in PriorityQueue that controls access to it,
something like this:

class ThreadingQueue:
    def __init__(self, args):
        self.lock = threading.Lock()
        ....

    def put(self, something):
       self.lock.acquire()
       try:
           self._put(something)
       finally:
           self.lock.release()

     
-- 
Cliff Wells
Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308
(800) 735-0555 x308




More information about the Python-list mailing list