Link to home
Start Free TrialLog in
Avatar of Dnx_7
Dnx_7Flag for Belgium

asked on

How to block static variable in threadpool?

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
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Can you put up your full code.
Avatar of Dnx_7

ASKER

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
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
Avatar of Dnx_7

ASKER

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

>> 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
Avatar of Dnx_7

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial