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

  • Home
  • SEARCH
  • 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 8238435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:48:54+00:00 2026-06-07T19:48:54+00:00

Basically I need the Master thread to keep doing some operation based on the

  • 0

Basically I need the Master thread to keep doing some operation based on the value of some global variables that can be edited (at some selected intervals) by a secondary thread. Something Like:

vector<int> mySharedVar;

void secondaryThreadFunction() {
   Do some operations
   And update mySharedVar if necessarily  
}

int main() {
 int count = 0;
 while(true) {
    count++;

    if (count%100) {  //> Each 100 iterations run a parallel thraed
      //> RUN secondaryThreadFunction in a separateThread
    }

    this is the main thread that based its operation on mySharedVar
 }
}

Which is the openmp command to run a single parallel thread with secondaryThreadFunction(); ?

Are there any other better way than this:

#pragma omp parallel num_threads(2)
    {
        int i = omp_get_thread_num();

        if (i == 0){
            mainThread();
        }
        if (i == 1 || omp_get_num_threads() != 2){
            secondaryThreadFunction();
        }
    }
  • 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-07T19:48:56+00:00Added an answer on June 7, 2026 at 7:48 pm

    Here is what I came up with:

    #include <omp.h>
    #include <unistd.h>
    #include <vector>
    #include <iostream>
    
    std::vector<int> mySharedVar(10);
    
    void secondaryThreadFunction() {
      mySharedVar[5]++;
    }
    
    int main() {
      int  i = 0 ;
    
    #pragma omp parallel sections shared(mySharedVar) private(i)
      {
    #pragma omp section
        //main thread
        {
          while( mySharedVar[5] < 10) {
            std::cout << "main: ";
            for(i=0; i < mySharedVar.size(); ++i){
              std::cout << mySharedVar[i] << " ";
            }
            std::cout << std::endl;
            usleep(1.e5); // wait 0.1 seconds
          }
        }
    #pragma omp section
        {  
          while( mySharedVar[5] < 10) {
            secondaryThreadFunction();  
            usleep(3.e5); // wait 0.3 seconds
          }
        }
      }
    }
    

    compile and run with g++ -fopenmp test_omp_01.cc && ./a.out

    output:

    main: 0 0 0 0 0 1 0 0 0 0 
    main: 0 0 0 0 0 1 0 0 0 0 
    main: 0 0 0 0 0 1 0 0 0 0 
    main: 0 0 0 0 0 2 0 0 0 0 
    main: 0 0 0 0 0 2 0 0 0 0 
    main: 0 0 0 0 0 2 0 0 0 0 
    main: 0 0 0 0 0 3 0 0 0 0 
    main: 0 0 0 0 0 3 0 0 0 0 
    main: 0 0 0 0 0 3 0 0 0 0 
    main: 0 0 0 0 0 4 0 0 0 0 
    main: 0 0 0 0 0 4 0 0 0 0 
    main: 0 0 0 0 0 4 0 0 0 0 
    main: 0 0 0 0 0 5 0 0 0 0 
    main: 0 0 0 0 0 5 0 0 0 0 
    main: 0 0 0 0 0 5 0 0 0 0 
    main: 0 0 0 0 0 6 0 0 0 0 
    main: 0 0 0 0 0 6 0 0 0 0 
    main: 0 0 0 0 0 6 0 0 0 0 
    main: 0 0 0 0 0 7 0 0 0 0 
    main: 0 0 0 0 0 7 0 0 0 0 
    main: 0 0 0 0 0 7 0 0 0 0 
    main: 0 0 0 0 0 8 0 0 0 0 
    main: 0 0 0 0 0 8 0 0 0 0 
    main: 0 0 0 0 0 8 0 0 0 0 
    main: 0 0 0 0 0 9 0 0 0 0 
    main: 0 0 0 0 0 9 0 0 0 0 
    main: 0 0 0 0 0 9 0 0 0 0 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

basically need to change the value that - admin_url() returns any idea?
Basically need to generate custom(some different then yes no) messeges(alert) in JS , how
Basically I need to capture the video from videocamera do some processing on frames
I need some advice with my desired setup with git and Rails. Basically for
How can Nginx serve crossdomain.xml file to a flash/flex program. Basically I need to
I have a page associated with master page. I need to implement the Ajax(basically
Here is my code, basically its written so that all you need to do
I basically need to get user input: gets.chomp(input?) And then to convert the given
I basically need a custom function to be used only when, for example, the
I basically need to check if there is an easier way to do this

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.