Solved

How to block static variable in threadpool?

Posted on 2008-10-08
9
495 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

634 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