code
ofstream myfile("file_path");
#pragma omp parallel for default(none) schedule(dynamic) firstprivate(myfile) private(i)
for(i=0; i<10000; i++) {
myfile<<omp_get_thread_num()+100<<endl;
}
but the compiler show me the error:
error: use of deleted function 'std::basic_ofstream<_CharT, _Traits>::basic_ofstream(const std::basic_ofstream<_CharT, _Traits>&) [with _CharT = char; _Traits = std::char_traits]'
/usr/include/c++/5/fstream:723:7: note: declared here basic_ofstream(const basic_ofstream&) = delete;
error: 'myfile' not specified in enclosing parallel
firstprivate
works by making a thread-private copy of the value. This does not work with streams, because you cannot copy them. You cannot safely write to a file just by having multiple streams of it open. There are basically two options:
Have a shared stream , protect all threaded access to it with #pragma omp critical
.
ofstream myfile("file_path"); #pragma omp parallel for for (int i=0; i < 10000; i++) { #pragma omp critical myfile << (omp_get_thread_num()+100) << endl; }
Open one stream for each thread on a different file .
#pragma omp parallel { ofstream myfile(std::string("file_path.") + std::to_string(omp_get_thread_num())); #pragma omp for for (int i=0; i < 10000; i++) { myfile << (omp_get_thread_num()+100) << endl; } }
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.