Solved

How to block static variable in threadpool?

Posted on 2008-10-08
9
492 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
9 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

772 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