Pet*_*ter 20 c++ containers multithreading stl c++11
我想从并行运行的线程访问基于STL的容器只读.不使用任何用户实现的锁定.以下代码的基础是C++ 11,它具有适当的标准实现.
http://gcc.gnu.org/onlinedocs/libstdc++/manual/using_concurrency.html
http://www.sgi.com/tech/stl/thread_safety.html
http://www.hpl.hp.com/personal/ Hans_Boehm/c ++ mm/threadsintro.html
http://www.open-std.org/jtc1/sc22/wg21/(当前草案或N3337,基本上是C++ 11,有轻微错误和错别字)
23.2.2集装箱数据竞赛[container.requirements.dataraces]
为避免数据争用(17.6.5.9),实现应将以下函数视为const:begin,end,rbegin,rend,front,back,data,find,lower_bound,upper_bound,equal_range,at和,除了关联或无序的关联容器,operator [].
尽管如此(17.6.5.9),除了vector <bool>之外,同时修改了同一序列中不同元素中包含对象的内容时,需要实现以避免数据争用.
[注意:对于大小大于1的向量<int> x,x [1] = 5和*x.begin()= 10可以在没有数据争用的情况下同时执行,但是x [0] = 5和*并发执行的x.begin()= 10可能导致数据竞争.作为一般规则的例外,对于向量<bool> y,y [0] = true可以与y [1] = true竞争. - 结束说明]
和
17.6.5.9数据竞争规避[res.on.data.races] 1本节规定了实现为防止数据竞争而应满足的要求(1.10).除非另有说明,否则每个标准库函数均应满足各项要求 在下面指定的情况下,实现可能会阻止数据争用.
2 C++标准库函数不应直接或间接访问除当前线程以外的线程可访问的对象(1.10),除非通过函数的论证直接或间接访问对象,包括此参数.
3 C++标准库函数不应直接或间接修改除当前线程以外的线程可访问的对象(1.10),除非通过函数的非const参数直接或间接访问对象,包括此参数.
4 [注意:这意味着,例如,实现不能在没有同步的情况下将静态对象用于内部目的,因为即使在未在线程之间显式共享对象的程序中,它也可能导致数据竞争. - 结束说明]
5 C++标准库函数不应通过其参数或其容器参数的元素访问可间接访问的对象,除非通过调用其规范所需的函数来容纳这些容器元素.
6通过调用标准库容器或字符串成员函数获得的迭代器操作可以
访问底层容器,但不得修改它.[注意:特别是,使迭代器无效的容器操作与与该容器关联的迭代器上的操作冲突. - 结束说明]7如果对象对用户不可见并且受到数据竞争保护,则实现可以在线程之间共享它们自己的内部对象.
8除非另有说明,否则C++标准库函数应仅在当前线程内执行所有操作(如果这些操作具有对用户可见(1.10)的效果).
9 [注意:如果没有可见的副作用,这允许实现并行化操作. - 结束说明]
结论
容器不是线程安全的!但是从多个并行线程调用容器上的const函数是安全的.因此,可以在没有锁定的情况下从并行线程执行只读操作.我对吗?
我假装它们不存在任何错误的实现,并且C++ 11标准的每个实现都是正确的.
样品:
// concurrent thread access to a stl container
// g++ -std=gnu++11 -o p_read p_read.cpp -pthread -Wall -pedantic && ./p_read
#include <iostream>
#include <iomanip>
#include <string>
#include <unistd.h>
#include <thread>
#include <mutex>
#include <map>
#include <cstdlib>
#include <ctime>
using namespace std;
// new in C++11
using str_map = map<string, string>;
// thread is new in C++11
// to_string() is new in C++11
mutex m;
const unsigned int MAP_SIZE = 10000;
void fill_map(str_map& store) {
int key_nr;
string mapped_value;
string key;
while (store.size() < MAP_SIZE) {
// 0 - 9999
key_nr = rand() % MAP_SIZE;
// convert number to string
mapped_value = to_string(key_nr);
key = "key_" + mapped_value;
pair<string, string> value(key, mapped_value);
store.insert(value);
}
}
void print_map(const str_map& store) {
str_map::const_iterator it = store.begin();
while (it != store.end()) {
pair<string, string> value = *it;
cout << left << setw(10) << value.first << right << setw(5) << value.second << "\n";
it++;
}
}
void search_map(const str_map& store, int thread_nr) {
m.lock();
cout << "thread(" << thread_nr << ") launched\n";
m.unlock();
// use a straight search or poke around random
bool straight = false;
if ((thread_nr % 2) == 0) {
straight = true;
}
int key_nr;
string mapped_value;
string key;
str_map::const_iterator it;
string first;
string second;
for (unsigned int i = 0; i < MAP_SIZE; i++) {
if (straight) {
key_nr = i;
} else {
// 0 - 9999, rand is not thread-safe, nrand48 is an alternative
m.lock();
key_nr = rand() % MAP_SIZE;
m.unlock();
}
// convert number to string
mapped_value = to_string(key_nr);
key = "key_" + mapped_value;
it = store.find(key);
// check result
if (it != store.end()) {
// pair
first = it->first;
second = it->second;
// m.lock();
// cout << "thread(" << thread_nr << ") " << key << ": "
// << right << setw(10) << first << setw(5) << second << "\n";
// m.unlock();
// check mismatch
if (key != first || mapped_value != second) {
m.lock();
cerr << key << ": " << first << second << "\n"
<< "Mismatch in thread(" << thread_nr << ")!\n";
exit(1);
// never reached
m.unlock();
}
} else {
m.lock();
cerr << "Warning: key(" << key << ") not found in thread("
<< thread_nr << ")\n";
exit(1);
// never reached
m.unlock();
}
}
}
int main() {
clock_t start, end;
start = clock();
str_map store;
srand(0);
fill_map(store);
cout << "fill_map finished\n";
// print_map(store);
// cout << "print_map finished\n";
// copy for check
str_map copy_store = store;
// launch threads
thread t[10];
for (int i = 0; i < 10; i++) {
t[i] = thread(search_map, store, i);
}
// wait for finish
for (int i = 0; i < 10; i++) {
t[i].join();
}
cout << "search_map threads finished\n";
if (store == copy_store) {
cout << "equal\n";
} else {
cout << "not equal\n";
}
end = clock();
cout << "CLOCKS_PER_SEC " << CLOCKS_PER_SEC << "\n";
cout << "CPU-TIME START " << start << "\n";
cout << "CPU-TIME END " << end << "\n";
cout << "CPU-TIME END - START " << end - start << "\n";
cout << "TIME(SEC) " << static_cast<double>(end - start) / CLOCKS_PER_SEC << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码可以使用GCC 4.7编译,并在我的机器上运行正常.
$ echo $?
$ 0
Jas*_*son 20
从1.10/4和1.10/21节中的C++ 11规范开始的数据争用需要至少两个对同一组内存位置具有非原子访问权限的线程,这两个线程在访问时不同步该组存储器位置,并且至少一个线程写入或修改该组存储器位置中的元件.所以在你的情况下,如果线程只是读取,你很好......根据定义,因为没有一个线程写入同一组内存位置,即使没有明确的同步机制,也没有数据争用.线程.