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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:40:14+00:00 2026-06-06T15:40:14+00:00

I’m having trouble adjusting my thinking to suit OpenMP’s way of doing things. Roughly,

  • 0

I’m having trouble adjusting my thinking to suit OpenMP’s way of doing things.

Roughly, what I want is:

for(int i=0; i<50; i++)
{
   doStuff();
   thread t;
   t.start(callback(i)); //each time around the loop create a thread to execute callback
}

I think I know how this would be done in c++11, but I need to be able to accomplish something similar with OpenMP.

  • 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-06T15:40:17+00:00Added an answer on June 6, 2026 at 3:40 pm

    The closest thing to what you want are OpenMP tasks, available in OpenMP v3.0 and later compliant compilers. It goes like:

    #pragma omp parallel
    {
        #pragma omp single
        for (int i = 0; i < 50; i++)
        {
            doStuff();
            #pragma omp task
            callback(i);
        }
    }
    

    This code will make the loop execute in one thread only and it will create 50 OpenMP tasks that will call callback() with different parameters. Then it will wait for all tasks to finish before exiting the parallel region. Tasks will be picked (possibly at random) by idle threads to be executed. OpenMP imposes an implicit barrier at the end of each parallel region since its fork-join execution model mandates that only the main thread runs outside of parallel regions.

    Here is a sample program (ompt.cpp):

    #include <stdio.h>
    #include <unistd.h>
    #include <omp.h>
    
    void callback (int i)
    {
       printf("[%02d] Task stated with thread %d\n", i, omp_get_thread_num());
       sleep(1);
       printf("[%02d] Task finished\n", i);
    }
    
    int main (void)
    {
       #pragma omp parallel
       {
          #pragma omp single
          for (int i = 0; i < 10; i++)
          {
             #pragma omp task
             callback(i);
             printf("Task %d created\n", i);
          }
       }
       printf("Parallel region ended\n");
    
       return 0;
    }
    

    Compilation and execution:

    $ g++ -fopenmp -o ompt.x ompt.cpp
    $ OMP_NUM_THREADS=4 ./ompt.x
    Task 0 created
    Task 1 created
    Task 2 created
    [01] Task stated with thread 3
    [02] Task stated with thread 2
    Task 3 created
    Task 4 created
    Task 5 created
    Task 6 created
    Task 7 created
    [00] Task stated with thread 1
    Task 8 created
    Task 9 created
    [03] Task stated with thread 0
    [01] Task finished
    [02] Task finished
    [05] Task stated with thread 2
    [04] Task stated with thread 3
    [00] Task finished
    [06] Task stated with thread 1
    [03] Task finished
    [07] Task stated with thread 0
    [05] Task finished
    [08] Task stated with thread 2
    [04] Task finished
    [09] Task stated with thread 3
    [06] Task finished
    [07] Task finished
    [08] Task finished
    [09] Task finished
    Parallel region ended
    

    Note that tasks are not executed in the same order they were created in.

    GCC does not support OpenMP 3.0 in versions older than 4.4. Unrecognised OpenMP directives are silently ignored and the resulting executable will that code section in serial:

    $ g++-4.3 -fopenmp -o ompt.x ompt.cpp
    $ OMP_NUM_THREADS=4 ./ompt.x
    [00] Task stated with thread 3
    [00] Task finished
    Task 0 created
    [01] Task stated with thread 3
    [01] Task finished
    Task 1 created
    [02] Task stated with thread 3
    [02] Task finished
    Task 2 created
    [03] Task stated with thread 3
    [03] Task finished
    Task 3 created
    [04] Task stated with thread 3
    [04] Task finished
    Task 4 created
    [05] Task stated with thread 3
    [05] Task finished
    Task 5 created
    [06] Task stated with thread 3
    [06] Task finished
    Task 6 created
    [07] Task stated with thread 3
    [07] Task finished
    Task 7 created
    [08] Task stated with thread 3
    [08] Task finished
    Task 8 created
    [09] Task stated with thread 3
    [09] Task finished
    Task 9 created
    Parallel region ended
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm interested in microtypography issues on the web. I want a tool to fix:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I want to show the soap response to UIWebview.. my soap response is, <p><img

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.