Multiple simultaneous notify() calls combine to 1.

Only one notify() is received in main thread
if multiple threads send call simultaneously.

Test: update only one record. The first 4 notify() come as 1.

Fix: Use a counter to track number of threads completed.
This commit is contained in:
Arun Prakash Jana 2016-12-01 00:45:29 +05:30
parent bf6893dd92
commit f8d4d331d1
No known key found for this signature in database
GPG Key ID: A75979F35C080412

View File

@ -767,7 +767,7 @@ class BukuDb:
return False
query = 'UPDATE bookmarks SET metadata = ? WHERE id = ?'
done = {'value': 0} # count threads completed
cond = threading.Condition()
cond.acquire()
@ -821,17 +821,16 @@ class BukuDb:
logdbg('Thread %d: processed %d' % (threading.get_ident(), count))
with cond:
done['value'] += 1
cond.notify()
thread_count = NUM_THREADS
for i in range(NUM_THREADS):
thread = threading.Thread(target=refresh, args=(i, cond))
thread.start()
while thread_count > 0:
while done['value'] < NUM_THREADS:
cond.wait()
thread_count -= 1
logdbg('%d threads still active', thread_count)
logdbg('%d threads completed', done['value'])
cond.release()