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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:51:43+00:00 2026-05-31T20:51:43+00:00

I have some Thread count pCount and I have some float[] array. I want

  • 0

I have some Thread count pCount and I have some float[] array. I want to get a pointer to the array and then based on pCount create that many threads and fill the array with data.

fixed (float* pointer = array)
{
    IntPtr fPtr = new IntPtr(pointer);

    for (int i = 0; i < pCount; i++)
    {
        Thread t = new Thread(() => ThreadMethod(fPtr, blockWidth, blockHeight, xIndex)));
        t.Start();
    }
}


private unsafe void ThreadMethod(IntPtr p, int blockWidth, int blockHeight, int startX)
{
    Random RandomGenerator = new Random();
    for (int x = startX; x < startX + blockWidth * blockHeight; x++)
    {
        ((float*)p)[x] = ((float)(RandomGenerator.NextDouble()) - 0.5f) * 2.0f;
    }
}

So if the array was 1000×1000 and I have 4 threads I want thread 1 to fill data from 0 – 250 then thread 2 from 250 – 500, thread 3 from 500 – 750 and thread 4 from 750 – 1000.

But the way I have described up there does not work. Can anyone help?

  • 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-31T20:51:45+00:00Added an answer on May 31, 2026 at 8:51 pm

    There’s no need to use pointer arithmetic to access array in C#. Here’s a simplified example of how it can be done:

    public void ParalellizeArrayFill(int threadCount, float[] array)
    {
        if (array == null || array.Length == 0)
            throw new ArgumentException("Array cannot be empty");
    
        if (threadCount <= 1)
            throw new ArgumentException("Thread count should be bigger than 1");
    
        int itemsPerThread = array.Length / threadCount;
        for (int i = 0; i < threadCount; i++)
        {
            Thread thread = new Thread( (state) => FillArray(array, i*itemsPerThread, itemsPerThread));
            thread.Start();
        }  
    }
    
    private void FillArray(float[] array, int startIndex, int count)
    {
        for (int i = startIndex; i < startIndex + count; i++)
        {
            // init value
            array[i] = value; 
        }
    }
    

    There are a few caveats to be aware of. First of all, your division may not divide equally (500/3 for example), so you have to handle this case. Also, you don’t have to use pointer arithmetic since array is already passed by reference and can be accessed by index.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a background thread and the thread calls some methods that update the
I have a thread that downloads some images from internet using different proxies. Sometimes
I have a thread that does the following: 1) Do some work 2) Wait
Suppose I have a custom collection class that provides some internal thread synchronization. For
I have a small program that is suppose to count lexemes. What I get
I have some thread-related questions, assuming the following code. Please ignore the possible inefficiency
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some code where I use a thread static object in C#. [ThreadStatic]
I have some BitmapFrames created on a thread other than the main UI thread;
Hi all i have a background worker thread and some unmanaged code dlls ,

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.