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 6680159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:26:33+00:00 2026-05-26T04:26:33+00:00

I have a loop that I’m trying to parallelize and in it I am

  • 0

I have a loop that I’m trying to parallelize and in it I am filling a container, say an STL map. Consider then the simple pseudo code below where T1 and T2 are some arbitrary types, while f and g are some functions of integer argument, returning T1, T2 types respectively:

#pragma omp parallel for schedule(static) private(i) shared(c)
for(i = 0; i < N; ++i) {
   c.insert(std::make_pair<T1,T2>(f(i),g(i))
}

This looks rather straighforward and seems like it should be trivially parallelized but it doesn’t speed up as I expected. On the contrary it leads to run-time errors in my code, due to unexpected values being filled in the container, likely due to race conditions. I’ve even tried putting barriers and what-not, but all to no-avail. The only thing that allows it to work is to use a critical directive as below:

#pragma omp parallel for schedule(static) private(i) shared(c)
for(i = 0; i < N; ++i) {
#pragma omp critical
   {
      c.insert(std::make_pair<T1,T2>(f(i),g(i))
   }
}

But this sort of renders useless the whole point of using omp in the above example, since only one thread at a time is executing the bulk of the loop (the container insert statement). What am I missing here? Short of changing the way the code is written, can somebody kindly explain?

  • 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-05-26T04:26:34+00:00Added an answer on May 26, 2026 at 4:26 am

    This particular example you have is not a good candidate for parallelism unless f() and g() are extremely expensive function calls.

    1. STL containers are not thread-safe. That’s why you’re getting the race conditions. So accessing them needs to be synchronized – which makes your insertion process inherently sequential.

    2. As the other answer mentions, there’s a LOT of overhead for parallelism. So unless f() and g() extremely expensive, your loop doesn’t do enough work to offset the overhead of parallelism.

    Now assuming f() and g() are extremely expensive calls, then your loop can be parallelized like this:

    #pragma omp parallel for schedule(static) private(i) shared(c)
        for(i = 0; i < N; ++i) {
            std::pair<T1,T2> p = std::make_pair<T1,T2>(f(i),g(i));
    
    #pragma omp critical
           {
              c.insert(p);
           }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a loop that enqueues say 100 tasks and each one of
I have a loop that opens a socket, acts on the socket, then closes
Say I have a loop that looks like this: for(int i = 0; i
I have a loop that does the following: short fooID; char line[256] map<short,foo> foos;
Say I have a loop that creates ImageViews and adds them to a layout
I have a loop that reads each line in a file using getline() :
I have a loop that finds duplicate lines in a .ini file. I can
I have a loop that runs through a variety of websites and I'd like
I have a loop that runs for approx. 25 minutes i.e 1500 seconds. [100
I have a loop that calculates the similarity between two documents. It collects all

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.