473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 4071

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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.