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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:02:26+00:00 2026-05-26T13:02:26+00:00

I have a following parallel snippet: #include <omp.h> #include stdio.h int main() { omp_set_num_threads(4);

  • 0

I have a following parallel snippet:

#include <omp.h>
#include "stdio.h"

int main()
{

omp_set_num_threads(4);
    int i;
#pragma omp parallel private(i)
    {
#pragma omp for 
        for(i = 0;i < 10; i++) {
            printf("A  %d: %d\n", omp_get_thread_num(),i);
        }
#pragma omp critical
        printf("i  %d: %d\n", omp_get_thread_num(), i ); 
    }
}

I thought that after the loop, each thread will have i equal to i last value in the thread’s loop. My desired output would be:

A  0: 0
A  0: 1
A  0: 2
A  3: 9
A  2: 6
A  2: 7
A  2: 8
A  1: 3
A  1: 4
A  1: 5
i  0: 3
i  3: 10
i  2: 9
i  1: 6

whereas what I get is:

A  0: 0
A  0: 1
A  0: 2
A  3: 9
A  2: 6
A  2: 7
A  2: 8
A  1: 3
A  1: 4
A  1: 5
i  0: -1217085452
i  3: -1217085452
i  2: -1217085452
i  1: -1217085452

How to make i to hold last iteration’s value? lastprivate(i) makes i = 10 for all threads, and that is not what I want.

  • 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-26T13:02:27+00:00Added an answer on May 26, 2026 at 1:02 pm

    It turns out you can’t. OpenMP alters program semantics.

    Parallel for loops are rewritten by the compiler according to well-defined set of rules.

    This also implies you cannot break from, return from such a loop. You can also not directly manipulate the loop variable. The loop condition can not call random functions or do any conditional expression, in short: a omp parallel for loop is not a for loop

    #include <omp.h>
    #include "stdio.h"
    
    int main()
    {
    
    omp_set_num_threads(4);
    #pragma omp parallel
        {
            int i;
    #pragma omp for 
            for(i = 0;i < 10; i++) {
                printf("A  %d: %d\n", omp_get_thread_num(),i);
            }
    #pragma omp critical
            printf("i  %d: %d\n", omp_get_thread_num(), i ); 
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in VS2008: int i,j; bool pr = false; #pragma
I have the following C/C++ code using OpenMP: int nProcessors=omp_get_max_threads(); if(argv[4]!=NULL){ printf(argv[4]: %s\n,argv[4]); nProcessors=atoi(argv[4]);
Imagine following scenario: We have a lot of parallel development going on in several
I have following situation. A main table and many other tables linked together with
I have the following piece of code, which I want to make parallel in
We have the following set up for running parallel tests using selenium grid and
I have the following Interview question: class someClass { int sum=0; public void foo()
I have the following class: Class L{ public: bool foo(vector<bool> & data); private: C**
If I have a Parallel.ForEach like this using a anonymous delegate for the main
I have been following the development of the .NET Task Parallel Library (TPL) with

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.