CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2012
    Posts
    2

    problem in parallel.for loop

    hi i'm currently facing race condition in variable, i don't know how to solve this event..
    particulary in race condition,

    i have code that cut and replace if the letters has been found
    example i have a word "cat", if i found the letter "a" it will be removed to the word "cat"
    so the remaining would be "ct", repeat the process until it will completely done..

    so i have created it and already fully functioning in 1 thread..
    but when i transfer it on a parallel.for loop, it acts weird, i dig in deeper and i found out that the process is to fast
    and it overwrites the variable, so i try the synclock method, but it slows the process.. and i try the interlock but no good it acts weird because it doesnt process the ordering like "1.2.3.4.5", what i want to do is to preserve the speed without using synclock..

    so here's the code.


    Code:
    sqrt = Math.Ceiling(Math.Sqrt(13599))
            a = 0
            xchar2 = ""
           Parallel.For(1, sqrt + 1, Sub(o)                                
                                     Dim a, b, c, i, y, z As Long
                                     i = 2
                                     a = 0
                                     If o = 1 Then
                                         b = 1
                                         c = sqrt
                                     Else
                                         b = ((o * sqrt ) + 1) - sqrt
                                         c = (o * (sqrt ))
                                     End If
                                     For y = b To c
                                         a = y
                                         tmpChar = "cat"
                                         For z = 0 To i
                                             xmod(z) = a Mod 26
                                             a = ((a - a Mod 26) / 26)
                                             If InStr(tmpChar, numToletter(xmod(z))) = 0 Then
                                                 bListed1 = False
                                                 Exit For
                                             Else
                                                 tmpChar = Mid(tmpChar, 1, InStr(tmpChar, numToletter(xmod(z))) - 1) & Mid(tmpChar, InStr(tmpChar, numToletter(xmod(z))) + 1)
                                                 If z = 0 Then
                                                     If check2(numToletter(xmod(z))) Then
                                                         xchar2 = numToletter(xmod(z))
                                                         bListed1 = True
                                                     Else
                                                         xchar2 = ""
                                                         bListed1 = False
                                                         Exit For
                                                     End If
                                                 Else
                                                     If check2(numToletter(xmod(z))) Then
                                                         xchar2 &= numToletter(xmod(z))
                                                         bListed1 = True
                                                     Else
                                                         xchar2 = ""
                                                         bListed1 = False
                                                         Exit For
                                                     End If
                                                 End If
                                             End If
    
                                             '---
                                         Next
                                         If bListed1 Then
                                             Debug.Print(StrReverse(xchar2))
                                         End If
                                     Next
                                 End Sub)
    and this is the part where the race condition occur, it produces error that it should be equal to zero or greater than zero, because the variable is less than zero or null ( i guess )

    Code:
    tmpChar = Mid(tmpChar, 1, InStr(tmpChar, numToletter(xmod(z))) - 1) & Mid(tmpChar, InStr(tmpChar, numToletter(xmod(z))) + 1)

    i wonder how to implement the Interlocked method here??? i think of it as a preliminary solution, any suggestion/comments guys, any related topic on parallel.for loop that can be possible solve is a great big help.. thank you in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: problem in parallel.for loop

    You are concatenating to the same string. That won't work, as you can see. You want to shuffle a deck IN PARALLEL but removing a card SEPARATELY as well. Parallel is break it up into WORDS, or LINES, or PARAGRAPHS, and process them in parallel
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Aug 2012
    Posts
    2

    Re: problem in parallel.for loop

    hmm i forgot to mention, this is a permutation sir..it's working on 1 thread, but when i do a parallel to
    speed up the process it overwriting the variable, i don't know what's the right solution on this,
    i've already create the variable into array but no good at all, sir do you have any suggestion on how to work with this one??currently i've create a separate thread, 2 threads exactly that will devide the work, but it no good, the first thread need to be finish to process so that the second thread can continue, but parallel.for loop is awesome it's process while the first thread is processing it can execute another thread to be process, that's why i want to use this..now i'm trap on how to use this properly.. any suggestion sir??? thank you..
    Last edited by tang3li2; September 1st, 2012 at 04:09 PM.

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: problem in parallel.for loop

    MSDN
    Data parallelism refers to scenarios in which the same operation is performed concurrently (that is, in parallel) on elements in a source collection or array.
    In data parallel operations, the source collection is partitioned so that multiple threads can operate on different segments concurrently.
    If your loop requires values from previous iterations of the loop, then you cannot perform operations concurrently..

    Its as simple as that.. Redesign your code so that it does not reference values from previous iterations, or split out the code into multiple loops..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured