我有:
struct employee
{
uint64_t id;
uint32_t a;
uint32_t b;
employee() { }
struct By_id {};
struct By_a {};
struct By_b {};
struct Change_a : public std::unary_function< employee, void >
{
uint32_t p;
Change_a(const uint32_t &_p) : p(_p) {}
void operator()(employee & r) { r.a = p; }
};
struct Change_b : public std::unary_function< employee, void >
{
uint32_t p;
Change_a(const uint32_t &_p) : p(_p) {}
void operator()(employee & r) { r.b = p; }
};
};
typedef multi_index_container<
employee,
indexed_by<
ordered_unique< tag<employee::By_id>, member<employee, uint64_t, &employee::id> >,
ordered_non_unique< tag<employee::By_a>, member<employee, uint32_t, &employee::a> >,
ordered_non_unique< tag<employee::By_b>, member<employee, uint32_t, &employee::b> >,
>
> employee_set;
employee_set es;
typedef employee_set::index<employee::By_id>::type List_id;
typedef employee_set::index<employee::By_a>::type List_a;
typedef employee_set::index<employee::By_b>::type List_b;
//...
thread 1
List_id::iterator it_id;
es.modify( it_id, employee::Change_a( 0 ) );
thread 2
List_id::iterator it_id;
es.modify( it_id, employee::Change_b( 0 ) );
//...
这个标准的例子如何使用boost多索引容器。 如果按id找到一个节点并将迭代器存储在List_id :: iterator it_id;
中我想在不同的线程中更改(修改)员工的不同字段。
concurent操作是否是线程安全的?
答案 0 :(得分:1)
Boost.MultiIndex与标准库中的其他容器具有相同的基本线程安全保证:
因此,调用modify
(或导致容器更改的任何其他操作)必须使用一些类似互斥锁的机制。