Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9194021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:18:16+00:00 2026-06-17T21:18:16+00:00

#include /usr/lib/gcc/i686-linux-gnu/4.6/include/omp.h #include <iostream> #include<list> using namespace std; int main() { list<int> lst; for(int

  • 0
#include "/usr/lib/gcc/i686-linux-gnu/4.6/include/omp.h"
#include <iostream>
#include<list>
using namespace std;

int main()
{
    list<int> lst;
    for(int i=0;i<5;i++)
        lst.push_back(i);

#pragma omp parallel for
    for(int i=0;i<5;i++)
    {
        cout<<i<<" "<<omp_get_thread_num()<<endl;
    }   
}

suppose that I can get this:

0  0
1  0
2  0
3  1
4  1

However, sometimes I can get this result:

30  0
1  0
2  0
  1
4  1

or even this kind of result:

30 1 0
4 1

1 0
2 0

I know this is because the output code:

cout<<i<<" "<<omp_get_thread_num()<<endl;

has been spliced into small segments and has no order when doing output.
But who can tell me how to prevent this from happening?
Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:18:18+00:00Added an answer on June 17, 2026 at 9:18 pm

    Your code

    #pragma omp parallel for
        for(int i = 0; i < 5; i++)
        {
            cout << i << " " << omp_get_thread_num() << endl;
        }
    

    is equivalent to

    #pragma omp parallel for
        for(int i = 0; i < 5; i++)
        {
            cout << i;
            cout << " ";
            cout << omp_get_thread_num();
            cout << endl;
        }
    

    Calls to << in the different threads may be executed in any order. For instance cout << i; in thread 3 may be followed by cout << i; in tread 0 which may be followed by cout << " "; in thread 3 etc. resulting in the garbled output 30 ....

    The correct way is to rewrite the code so that each thread calls cout << only once in the loop:

    #pragma omp parallel for
        for(int i = 0; i < 5; i++)
        {
            stringstream ss;
            ss << i << " " << omp_get_thread_num() << '\n';
            cout << ss.str();
        }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <iostream> using namespace std; int main() { int a, b, c, max; cout<<a=;
#include <iostream> #include <tuple> int main(){ auto bt=std::make_tuple(std::tuple<>(),std::tuple<std::tuple<>>()); //Line 1 auto bt2=std::make_tuple(std::tuple<>(),std::tuple<>()); //Line 2
Here's a small test program I wrote: #include <iostream> using namespace std; class A
I wrote this code: #include<vector> using namespace std; int i; int j; cin>>i>>j; vector<vector<vector<vector<double>
#include <boost/algorithm/string.hpp> #include <vector> #include <iostream> #include <string> using namespace std; using namespace boost;
I'm learning C++. I regularly get errors that look like this /usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/basic_string.h:1458: instantiated from
#include <iostream> #include <cassert> #include <vector> #include <ctime> #include <cstdlib> #include <Windows.h> using namespace
simpatico$ /Developer/usr/bin/g++-4.2 -v Using built-in specs. Target: i686-apple-darwin10 Configured with: /var/tmp/gcc/gcc-5664~89/src/configure --disable-checking --enable-werror --prefix=/usr
comrade@hp:~/workspace/JNI2/src$ ls Prompt.c Prompt.class Prompt.h Prompt.java comrade@hp:~/workspace/JNI2/src$ gcc --shared -o libPrompt.so -I/usr/lib/jvm/java-6-openjdk-i386/include -I/usr/lib/jvm/java-6-openjdk-i386/include/linux -lX11
make /bin/sh /Users/myusername/Downloads/php-5.3.5/ext/mssql/libtool --mode=compile cc -I. -I/Users/myusername/Downloads/php-5.3.5/ext/mssql -DPHP_ATOM_INC -I/Users/myusername/Downloads/php-5.3.5/ext/mssql/include -I/Users/myusername/Downloads/php-5.3.5/ext/mssql/main -I/Users/myusername/Downloads/php-5.3.5/ext/mssql -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.