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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:02:59+00:00 2026-06-11T18:02:59+00:00

I have this chunk of code: noObjs = 0 Dim oName As String Dim

  • 0

I have this chunk of code:

noObjs = 0
Dim oName As String
Dim i As Integer
Dim tripleIndex As Integer = 0
Do While sr.Peek() <> -1
   readCSV = sr.ReadLine.Split(sepChar(0))
   If readCSV.Length >= 3 Then
       oName = readCSV(0)
       For i = noObjs - 1 To 0 Step -1
           If oName = objNames(i) Then
               obIndOfTriple(tripleIndex) = i
               Exit For
           End If
       Next i
       If i = -1 Then
           objNames(noObjs) = oName
           obIndOfTriple(tripleIndex) = noObjs
           noObjs += 1
       End If
   End If
   tripleIndex += 1
Loop
sr.Close()

And I’m trying to parallelise as such:

noObjs = 0
Dim oName As String
Dim i As Integer
Dim tripleIndex As Integer = 0
Dim allData() As String = File.ReadAllLines(in_file)
Parallel.For(0, allData.Count, Sub(k)
                               readCSV = allData(k).Split(sepChar(0))
                               If readCSV.Length >= 3 Then
                                   oName = readCSV(0)
                                   For i = noObjs - 1 To 0 Step -1
                                       If oName = objNames(i) Then
                                           obIndOfTriple(tripleIndex) = i
                                           Exit For
                                       End If
                                   Next i
                                   If i = -1 Then
                                       objNames(noObjs) = oName
                                       obIndOfTriple(tripleIndex) = noObjs
                                       noObjs += 1
                                   End If
                               End If
                               tripleIndex += 1
                               End Sub)

However, I get an “index was outside the bounds of the array” at:

If oName = objNames(i) Then

I should also mention here that objNames() and obIndOfTriple() are declared globally (with a fixed size).
From some searching around, I understand that this has to do with thread safety, although I’m still a newbie in parallelism.
Could anyone point me in the right direction?
Thanks.

  • 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-11T18:03:00+00:00Added an answer on June 11, 2026 at 6:03 pm

    The crux of the problem is that you have multiple threads accessing shared resources without synchronizing access to those resources and thereby introducing a race condition.

    For instance, consider noObjs in relation to objNames. I suspect you want noObjs to always reflect the number of actual items in objNames. Now suppose you have two threads that reach objNames(noObjs) = oName at the same time, and that noObjs is 4 at the time. One thread will write a value into objNames(4) and then the other thread will immediately overwrite it. The first thread hasn’t gotten to the line to increment noObjs yet! Additionally, when both threads do execute noObjs += 1, noObjs will be 6, but you will have nothing stored in noObjs(5). That’s not the exact case of the exception you’re seeing, but it’s another symptom of the fragility of the implementation.

    In the code that each thread executes, you want to make sure that each thread has its own variable space to work with. You could do that by having objNames and objIndOfTriple be two dimensional arrays. The first dimension would be the loop iteration, k, and the second would be the index into the array just for that iteration. Likewise, noObjs would be an array, and noObjs(k) would be the number of elements in the objNames array associated with loop index k.

    Technically that should work, but then you will need to coalesce objNames from a bunch of small arrays into a single large one following the execution of Parallel.For – essentially completing the implementation of a map-reduce pattern.

    If you do get all that implemented, you might want to take a look at the performance. You’re parallelizing the processing for a single line of input and from the code it doesn’t appear you’re doing much work for each line. In other words, parallelizing it as you have it, line by line, may actually add more overhead than if you’d just done it sequentially. If you have 1000 lines, you’re essentially asking for 1000 tiny tasks to run at the same time, so managing the tasks becomes more work than actually executing them. Now, the TPL may decide whether or not to really do something in parallel based on what it thinks is best so that could mitigate the performance hit.

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

Sidebar

Related Questions

If I have a chunk of code like this: .hover( function () { hoverState($(#navbar
I have this string containing a large chunk of html and am trying to
I currently have this chunk of model code: class Book(models.Model): title = models.CharField(max_length=100) num_pages
I have this chunk of code which I found and implemented according to http://www.activexperts.com/activmonitor/windowsmanagement/scripts/networking/windowsfirewall/
I have this chunk of code which adds up all the players hurt on
Ok so i have this chunk of code <p id=number_of_stations class=text left><?php $_GET['count'] !=
I have trouble understanding this chunk of code: let sieve (p:xs) = p :
I have this chunk of code. It works, but I'm sure there is a
I have this chunk of code, one sample is $dispatch='TC_12' : foreach($this->dispatch as $dispatch){
I have this chunk of code here that draws a block with a one-character

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.