خرید بک لینک

Vote count: 0

Forgive me if this has been asked before. I have looked around a lot, but I get the feeling that I don't have the proper vocabulary to find this by searching the web.

I have a multithreaded application in python. I want to be able to lock a certain block of code but only to other threads with a certain condition. Let me give an example: There are three threads, t_a, t_b and t_c. Each thread may run through the function foo at any time. I don't want any two threads with bar equal to each other to be able to access Code block ALPHA at the same time. However, I don't want to block threads whose bar value is different. In this case, let's say t_a has a bar == 5 and hits line (3) first. Before t_a hits line (5), let's say t_b, with bar == 5 hits line (3). I would like for t_b to wait. But if t_c comes along, with bar == 3, I would like for it to be able to keep going.

(1) def foo(bar):
(2)    
(3)     lock(bar)
(4)     # Code block ALPHA (two threads with equivalent bar should not be in here)
(5)     unlock(bar)

Thank you for any help. The library I am looking at is the python threading library

asked 2 mins ago

برچسب: نویسنده: استخدام کار تاريخ: شنبه 15 خرداد 1395 ساعت: 5:37

صفحه بندی