The following minimal example illustrates the behaviour of stxxl when initializing containers in parallel (using openMP):
#include <omp.h>
#include <stdio.h>
#include <stxxl.h>
typedef stxxl::VECTOR_GENERATOR<float>::result VEC_T;
int main(int argc, char* argv[]) {
const unsigned long NUM = 8;
#pragma omp parallel num_threads(NUM)
{
VEC_T v;
printf("%d\t%p\n", omp_get_thread_num(), &v);
}
return EXIT_SUCCESS;
}
running into either
[STXXL-ERROR] File too large
or
[SYSTEM-ERROR]Segmentation fault
How can I allocate stxxl containers in multiple threads ?
The initialization of stxxl containers isn't thread-safe, therefore a mutual exclusion for a thread initializing a container is needed. Using openMP, this will read as follows:
#include <omp.h>
#include <stdio.h>
#include <stxxl.h>
typedef stxxl::VECTOR_GENERATOR<float>::result VEC_T;
int main(int argc, char* argv[]) {
const unsigned long NUM = 8;
#pragma omp parallel num_threads(NUM)
{
VEC_T* v;
#pragma omp critical
{
v = new VEC_T();
}
printf("%d\t%p\n", omp_get_thread_num(), &v);
delete v;
}
return EXIT_SUCCESS;
}