Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to block static variable in threadpool?

Posted on 2008-10-08
9
Medium Priority
?
498 Views
Last Modified: 2013-11-07
Hi experts,

i use a static (shared) variable accross all the application
the application launch dynamically threads in the threadpool...
whad i did is the launch x threadpools sequentially

i use monitor.enter(_sharedVar) but i have some strange problem

i have this scenario :

monitor.enter(_sharedVar)

'do process

if a then
monitor.exit(_sharedVar)
else
'Call async operation with callback
end if



private sub callbackFunction(xxx)
'do process
monitor.exit(_sharedVar)
end sub

the monitor.exit in the callback function seems to have difficulties to release the _sharedVar and other threads can access the _sharedVar without monitor.exit...

don't understand...

Regards
0
Comment
Question by:Dnx_7
  • 4
  • 3
7 Comments
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 22673713
Can you put up your full code.
0
 
LVL 2

Author Comment

by:Dnx_7
ID: 22676919
Sure!

    Private Shared WithEvents _oradServer As New ReTalkXMLAx

'call from WCF Client (it call a shared method and it works)
    Public Sub DoProcess(ByVal aOradActionList As List(Of Object)) Implements RVKernel.Interfaces.IOrad.DoProcess
        InitOradThreadPool(aOradActionList)
    End Sub


    Private Shared Sub InitOradThreadPool(ByVal aOradActionList As List(Of Object))

        Dim arEvent As New AutoResetEvent(False)
        Dim rwh As RegisteredWaitHandle
        Dim oa As OradAnim
        Dim op As OradParam

        If aOradActionList IsNot Nothing Then
            For Each obj As Object In aOradActionList
                Select Case True
                    Case TypeOf (obj) Is OradAnim
                        oa = DirectCast(obj, OradAnim)
                        rwh = ThreadPool.RegisterWaitForSingleObject(arEvent, AddressOf ProcessOradAction, oa, oa.Trigger, True)
                    Case TypeOf (obj) Is OradParam
                        op = DirectCast(obj, OradParam)
                        rwh = ThreadPool.RegisterWaitForSingleObject(arEvent, AddressOf ProcessOradAction, op, op.Trigger, True)
                End Select
            Next
        End If

    End Sub

 Private Shared Sub ProcessOradAction(ByVal Args As Object, ByVal TimeOut As Boolean)
monitor.enter(_sharedVar)
        Select Case True
            Case TypeOf (Args) Is OradAnim
'play anim async
Case TypeOf (Args) Is OradParam
monitor.exit(_sharedVar)
end select
end sub

private shared sub callBackAnim()
monitor.exit(_sharedVar)
end sub
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 22680188
Private Shared Sub ProcessOradAction(ByVal Args As Object, ByVal TimeOut As Boolean)
monitor.enter(_sharedVar)  <-- Enter lock for any paramter
        Select Case True
            Case TypeOf (Args) Is OradAnim
'play anim async
Case TypeOf (Args) Is OradParam
monitor.exit(_sharedVar) <--Leave lock only if an OradParam
end select
end sub


When Args is not a OradParam you ever release your lock.

Cheers,

Greg
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 2

Author Comment

by:Dnx_7
ID: 22681892
i agree but i always have (for sure)
2 object (oradanim or oradparam)

as you can see i lock the sharedVar in the "processOradAction"
i unlock the sharedVar one the case of oradParam instantly but i only unlock the sharedVar when the anim is "callbacked"

in fact, an "oradParam" can be set instantly but for an animation, i have to wait the end of this to unlock and relase the sharedVar

Regards
0
 
LVL 37

Expert Comment

by:gregoryyoung
ID: 22682339

>> i unlock the sharedVar one the case of oradParam instantly but i only unlock the sharedVar when the anim is "callbacked"
ah you were missing code to that part (you never actually schedule anything for callback)


Let's try something else .. what is it that you want to have happen here? Why are you writing code like this? I think there might be easier ways than (ab)using critical sections like this.

Cheers,

Greg
0
 
LVL 2

Author Comment

by:Dnx_7
ID: 22682966
in fact i send a list of oradAnim or oradParam from WCF

when i receive the list, for each object i enqueue a thread in the pool (because i need the timer of the threadpool functionality)

but sometimes i have to wait the anim ends before executing pending thread in the pool

what i don't understand is that the monitor doesn't lock the sharedVar accross all the thread...

regards
0
 
LVL 37

Accepted Solution

by:
gregoryyoung earned 2000 total points
ID: 22683071
well I am going to take a guess that this is the issue.


Monitors are scoped to threads.

so what happens is you acquire the lock ... it is scoped to the thread ... you then release the thread back to the thread pool (still holding the lock) ... it then goes to process something else ... it comes to the lock (still owns it) and continues happily along its way since it already owns the lock.


This seems like it would be better suited by processing a queue directly and/or using an event to notify threads that they can begin processing jobs again

Cheers,

Greg
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question