I need some help understanding the critical sections of the code described below, in particular whether how I use std::map
is thread safe.
I am loading a file line-by-line, building a string out of the contents, and every now and then reach a separator. On this separator, I create a std::map
key/value pair, and create a task to operate on this value (see below). This value is a pair, the first being a housekeeping int
and the second being a vector of uint64_t
.
#pragma omp parallel num_threads(4)
#pragma omp single
{
while (getline(fin, line) && ...)
{
if (line[0] == '>')
{
#pragma omp task
do_work_on_value(tmp, map[key])
tmp.clear();
// insert map entry (new key!)
map.insert(...);
}
else
tmp += line;
}
}
...
static void do_work_on_value(string vals, pair<int, vector<uint64_t>> &val)
{
...
}
So, the key operations being :
- Adding new key/value pairs whilst operating on a different value
- Working on the vector of a value (
push_back
and some arithmetic/bitwise on contents)
I learnt about some of the OpenMP directives using this answer, and is it sufficient to use a #pragma omp critical
over the map.insert
? Each task will be operating on a unique key's value, so I don't believe I need to do much more.
Cheers in advance!
@Gilad thanks for your link to the YouTube Intel OpenMP tutorials,
These are specific relevant videos -