472,992 Members | 3,204 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,992 software developers and data experts.

Multi-Object SyncLock?

Hi,

I want to simultaneously SyncLock multiple objects, something like:

SyncLock objA, objB

' do stuff with objA and objB

End SyncLock

but so that a lock is only obtained if BOTH objects can be locked obtained
simultaneously.

I want to prevent possible deadlocks when multiple threads lock these
objects conditionally and out of order.

Is that possible and if so how?

Thanks,

Frank
May 30 '07 #1
10 4049

Hi Frank,
Something like this?
SyncLock objA

SyncLock objB

End SyncLock

End SyncLock


Robin
May 30 '07 #2
Something like this?
>
SyncLock objA
SyncLock objB
End SyncLock
End SyncLock
That could lead to a deadly embrace if another thread does this:

SyncLock objB
SyncLock objA
End SyncLock
End SyncLock

May 30 '07 #3
That could lead to a deadly embrace if another thread does this:
>
SyncLock objB
SyncLock objA
End SyncLock
End SyncLock
Deadlock indeed.

However, the only other thing I can think of is to encapsulate the objects
that require locking into one object and to lock that.

May 30 '07 #4
"Robin Tucker" <rt******@hotmail.comschrieb im Newsbeitrag
news:f3*******************@news.demon.co.uk...
>That could lead to a deadly embrace if another thread does this:

SyncLock objB
SyncLock objA
End SyncLock
End SyncLock

Deadlock indeed.

However, the only other thing I can think of is to encapsulate the objects
that require locking into one object and to lock that.
Yea, I was getting deadlocks.

The problem is that it was not just two items, but there is a list of items
and different threads iterate through the list and try to modify some item
and if needed a related item. That related item might itself be the related
item of another item being modified by another thread, hence the problem.

Well I found that I could at least create an ugly but workable workaround
using Monitor.TryEnter:

Dim worked As Boolean = False
While Not worked
SyncLock objA
worked = Threading.Monitor.TryEnter(objB, 100)
If worked Then
Try
' Do Stuff
Catch ex As Exception
Throw ex
Finally
Threading.Monitor.Exit(objB)
End Try
End If
End SyncLock
End While

But I don't like it one bit since it doesn't automatically release the lock
(hence I need the try catch finally).

Is there a better way?

In any case, thanks for the help!
May 30 '07 #5

Either way, you should have a public interface to access all of the objects
in one place. You can then lock all objects on this one common instance.
So create a new class, encapsulating the objects shared accross threads,
then lock on that. All you need to do is pass the accessor instance to each
thread and make sure the accessor instance contains references to the
objects the threads want to manipulate. Then you can:

SyncLock ( MyAccessor )

MyAccessor .Obj1.....

MyAccessor .Obj2.....

End SyncLock


May 30 '07 #6
The problem is that it was not just two items, but there is a list of items
and different threads iterate through the list and try to modify some item
and if needed a related item. That related item might itself be the related
item of another item being modified by another thread, hence the problem.
you could synclock with the object that represents your 'list of items', ie
the array or the collection or whatever. i know it is a blunt instrument
approach, but it will work. if you go this way, then thread 1 using objects
a and b will block thread 2 that wants to use (unrelated) objects c and d.
It is hard to imagine a case where this creates a problem.

May 30 '07 #7
"AMercer" <AM*****@discussions.microsoft.comschrieb im Newsbeitrag
news:DF**********************************@microsof t.com...
>The problem is that it was not just two items, but there is a list of
items
and different threads iterate through the list and try to modify some
item
and if needed a related item. That related item might itself be the
related
item of another item being modified by another thread, hence the problem.

you could synclock with the object that represents your 'list of items',
ie
the array or the collection or whatever. i know it is a blunt instrument
approach, but it will work. if you go this way, then thread 1 using
objects
a and b will block thread 2 that wants to use (unrelated) objects c and d.
It is hard to imagine a case where this creates a problem.
So simply, i can't believe i didn't think of it >_<;

Thank you!
May 30 '07 #8
"Robin Tucker" <rt******@hotmail.comschrieb im Newsbeitrag
news:f3*******************@news.demon.co.uk...
>
Either way, you should have a public interface to access all of the
objects in one place. You can then lock all objects on this one common
instance. So create a new class, encapsulating the objects shared accross
threads, then lock on that. All you need to do is pass the accessor
instance to each thread and make sure the accessor instance contains
references to the objects the threads want to manipulate. Then you can:

SyncLock ( MyAccessor )

MyAccessor .Obj1.....

MyAccessor .Obj2.....

End SyncLock

True & works! For some reason i didn't think of that ;)

Thank you!
May 30 '07 #9
Frank,
>I want to simultaneously SyncLock multiple objects, something like:

SyncLock objA, objB

' do stuff with objA and objB

End SyncLock
There doesn't have to be any relation between the object you lock on
and the object(s) you're going to work with inside the synchonized
block. You can lock on any object, as long as all threads that are
working with the data lock on the same object. Often it's a good idea
to have a completely separate object dedicated only for the locking,
it helps avoid this confusion.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 30 '07 #10
"Frank Osterberg" <ra*******@yahoo.comwrote:
The problem is that it was not just two items, but there is a list of
items and different threads iterate through the list and try to modify
some item and if needed a related item. That related item might itself be
the related item of another item being modified by another thread, hence
the problem.
You might take a look at:
http://www.coversant.net/Default.asp...=88&EntryID=40

In there, I go over some of the approaches I've used (and liked / didn't
like) for solving this problem.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
May 30 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.