C-Thread-Pool icon indicating copy to clipboard operation
C-Thread-Pool copied to clipboard

`bsem_post_all` may not be working as intended

Open npc1054657282 opened this issue 9 months ago • 2 comments

I've noticed a potential issue with the bsem_post_all function. It appears that this function may not be working as intended and is essentially equivalent to bsem_post. Here's my analysis:

https://github.com/Pithikos/C-Thread-Pool/blob/4eb5a69a439f252d2839af16b98d94464883dfa5/thpool.c#L545-L571

1.bsem_post_all only set bsem_p->v = 1 once. 2. Although it uses pthread_cond_broadcast, multiple threads waiting in bsem_wait would check if bsem_p->v is set to 1 before they leave bsem_wait. 3. If a thread leaves bsem_wait successfully, it will set bsem_p->v = 0, preventing other threads from leaving bsem_wait.

As a result, using one bsem_post_all still only releases one thread waiting in bsem_wait. bsem_post_all is functionally equivalent to bsem_post, possibly consuming more resources due to the broadcast. In thpool_destroy, looping bsem_post should be sufficient and potentially more efficient:

npc1054657282 avatar Apr 20 '25 12:04 npc1054657282

I refactor the project: refactored codes The refactored codes also tried to solve the issue: #88 #59 #20

npc1054657282 avatar Apr 30 '25 10:04 npc1054657282

Find a better realization of C threadpool. sogou workflow thrdpool

npc1054657282 avatar May 01 '25 18:05 npc1054657282