-1
should_go = False
cv = Condition()
while True:
    with cv:
        if not should_go:
            cv.wait()
        if should_go:
            # process_time_cost_tasks()
            should_go = False


def request():
    with cv:
        should_go = True
        cv.notify()

Question: Does it matter if I possess the lock during the task processing time(quite long)?

Bob
  • 105
  • 1
  • 12

1 Answers1

0

It seems that you want to use cv as a mutex for should_go. bool assignment is atomic, so this is not needed.

If you're asking about whether holding the lock inside of process_time_cost_tasks will block request, it will. It's a lock, that's what it does.

Community
  • 1
  • 1
Veedrac
  • 58,273
  • 15
  • 112
  • 169
  • I mean is it a good practice to put a time-cost task inside a lock? – Bob Sep 25 '14 at 09:12
  • Do you want the lock to be locked when you're doing the "time-cost task"? – Veedrac Sep 25 '14 at 09:16
  • Yes I do. But a little afraid something goes wrong during this period of time. People told me I should make the code as short as possible within a lock. – Bob Sep 25 '14 at 09:23
  • What does "But a little afraid something goes wrong during this period of time." mean? Also, "as short *as possible*. If you need the lock locked, lock the lock. – Veedrac Sep 25 '14 at 09:24
  • It is said that Linux kernel tries to shorten the lock critical path in order to reduce risks? – Bob Sep 25 '14 at 09:33
  • I guess it's the same to the high level app development? Sorry, newbie to lock concept. – Bob Sep 25 '14 at 09:35
  • I wouldn't think someone new to development has the same concerns as a kernel developer. – Veedrac Sep 25 '14 at 09:36