boost threads mutex array

后端 未结 2 1605
南旧
南旧 2021-01-05 15:57

My problem is, I have block matrix updated by multiple threads. Multiple threads may be updating disjoint block at a time but in general there may be race conditions. right

2条回答
  •  既然无缘
    2021-01-05 16:36

    It might be a several approaches you can use:

    1. Pre-allocate array if CriticalSection/Mutexes (2500 is not so many), and use block index as a lock index to gather block access; before updating blocks lock all of them which you want to change; do update; unlock;

    2. If computation time is significantly longer then Lock/Unlock, then copy content of the block in thread context and keep block unlocked during that time; before updating block lock it again and make sure that it was not updated by another thread (if it's relevant for this); if it was updated by another thread, then repeat operation;

    3. If the size of the block content is small, use atomic data exchange to update block content, no locks required; just not sure if you use data from one blocks to calculate data for another, in that case locking across all updated blocks required;

    4. Is there reading operation on the matrix? If yes use Read/Write locks to improve performance.

提交回复
热议问题