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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:46:41+00:00 2026-05-20T01:46:41+00:00

Say you have a function that counts the occurences of a value in a

  • 0

Say you have a function that counts the occurences of a value in a large array. You want to multithread this function by having each thread count the occurences in its own part of the array, then adding the results together. We can assume that each thread has a unique rank (from 0 to N-1), and that each thread will call the function at about the same time. E.g.:

int count[N];  // global array

int countOccurences()
{
    count[rank] = /* count occurences in one part of the array */
    // wait until all other threads reached this point
    int total = 0;
    for (int i=0; i<N; i++)
        total += count[i];
    return total;
}

The question is, in the above example, could I move the count array into the body of countOccurences() as a static variable? I don’t need it to exist anywhere outside the function body: will a static array be shared among threads?

  • 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-20T01:46:42+00:00Added an answer on May 20, 2026 at 1:46 am

    The answer to your question is yes, threads have access to global data and static data in the same compilation unit, but there is more to this topic about “sharing data between threads” that is important to understand.

    For each thread, there is a corresponding function (the “thread function”) that a thread will execute in parallel with other threads. A thread has access to whatever that function can access, either through pointer or reference parameters, global data, static data in the same compilation unit as the thread function, or global and/or static data that is readable/modifiable via other functions that the thread function can call. You should be able to determine every memory region that a given function can read or modify. Those memory regions of a given thread function are exactly the memory regions that the thread has access to.

    It is easy to see that global data and static data in the same compilation unit are accessible to a thread function, so that’s why the answer to your question is “yes”.

    One thing that you might want to look into is OpenMP, which has built-in constructs for parallelizing a countOccurrences operation (a so-called “reduction”):

    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    size_t countOccurrences(const int *arr, size_t n, int which) {
        size_t count, i;
    
        count = 0;
    
        #pragma omp parallel for reduction(+:count)
        for (i = 0; i < n; ++i) {
            if (arr[i] == which)
                ++count;
        }
    
        return count;
    }
    
    int main()
    {
        int arr[] = { 3, 5, -1, -1, 0 };
    
        size_t count = countOccurrences(arr, sizeof (arr)/sizeof (arr[0]), -1);
        printf("count = %d\n", (int) count);
        return EXIT_SUCCESS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a function foo that I want to call n times. In
Say I have a recursive function that I want to know how many times
Lets say that I have an array that I want to convert to a
Say I have a function that does the following in Vb.net For i as
Say I have a function that takes an arbitrary number of arguments (the last
Lets say I have a recursive function that creates lists within lists. It return
Say I have a function func(i) that creates an object for an integer i,
I have a function that updates several tables. As an example, let's say it
Say I have two functions that expect ...rest parameters private function a(...myParams):void { trace(myParams.length);
Let's say that I have an Erlang function, with spec. -spec foo(integer(), string()) ->

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.