Solved

How to block static variable in threadpool?

Posted on 2008-10-08
9
488 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
Comment Utility
Can you put up your full code.
0
 
LVL 2

Author Comment

by:Dnx_7
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Author Comment

by:Dnx_7
Comment Utility
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
Comment Utility

>> 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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now