0
我所拥有的是一个数组,我想在一个元素上放置一个锁,以便其他元素不能改变它。使用锁定数组的锁定元素C++
一种方法来描述这更好的为您展示:
Array A = new Array;
Thread1() {
while(array hasn't been completely changed) {
grab lock for random element within the array
alter the elements to this array
free the lock for the element
}
}
Thread2() {
while(array hasn't been completely changed) {
grab lock for random element within the array
alter the elements to this array
free the lock for the element
}
}
的目标是有两个线程执行操作的元素,但它锁定所以没有其他线程可以访问它。
虽然这只使用一个单一的锁,但我的目标是构建优良的颗粒锁,所以我想为每个数组索引锁定一个锁。 – QQCompi
@QQCompi,然后只是做一个互斥数组配对数组索引,只是为了知道,你会访问相同的数组索引形式不止一个线程? – Netwave
@QQCompi你可以为数组中存储的每个对象提供一个互斥体作为类成员。 –