473,785 Members | 2,553 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating a Shared Lock

Is there a way to create a shared lock using the Monitor class or any other
synchronization class in .NET?

So I have one resource that may have multiple threads using it at once but a
second thread that when called must have exclusive access and cause the
other threads to wait.

I can't figure out how to do this with .Net.

Thanks.
Mar 17 '06 #1
5 4068
Hello, Bob!

BB> So I have one resource that may have multiple threads using it at once
BB> but a second thread that when called must have exclusive access and
BB> cause the other threads to wait.

BB> I can't figure out how to do this with .Net.

I believe you can use lock(resource) { } in the thread in order to access that resource.
or you can write wrapper around resource and inside wrapper have lock(resource) { }.

Can you be more specific about what those thread are doing with resource and what is the role of
"second thread"?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Mar 17 '06 #2
Bob,

You could use the ReaderWriterLoc k. Using this lock threads that have
acquired a reader lock all get access to the shared resource, but a
thread that acquires a writer lock gets exclusive access.

I usually avoid the ReaderWriterLoc k because it is slower in most
scenarios and more complicated to use correctly. But, I wanted to at
least throw that out here as a possible option.

Brian

Bob Bins wrote:
Is there a way to create a shared lock using the Monitor class or any other
synchronization class in .NET?

So I have one resource that may have multiple threads using it at once but a
second thread that when called must have exclusive access and cause the
other threads to wait.

I can't figure out how to do this with .Net.

Thanks.


Mar 17 '06 #3
Hi Bob. That is actually what a Monitor if for. Only one thead at a time
can own the lock on the Monitor object and any others will Q waiting for it
to free in ~FIFO order. Each thread must lock on the same object for this
to work.

--
William Stacey [MVP]

"Bob Bins" <ms****@hotmail .com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
| Is there a way to create a shared lock using the Monitor class or any
other
| synchronization class in .NET?
|
| So I have one resource that may have multiple threads using it at once but
a
| second thread that when called must have exclusive access and cause the
| other threads to wait.
|
| I can't figure out how to do this with .Net.
|
| Thanks.
|
|
Mar 17 '06 #4
No thats not suffecient. I am writing a test application and I need to
syncronize my database access. I need one funciton to execute at the rate
around 100 times a seconds. This funcation will be called at the same time
from multiple threads. A second thread that is called once every second
needs to lock so the other function does not get called while its running.

In SQL you would do this by creating a shared lock on the first function
then a exclusive lock on the second. This allows many threads to have
shared access but once a thread has an exclusive lock any shared locks will
wait.

It sounds like the ReaderWriterLoc k is what I will need. I didn't even no
that class existed.
"William Stacey [MVP]" <wi************ @gmail.com> wrote in message
news:eN******** ******@tk2msftn gp13.phx.gbl...
Hi Bob. That is actually what a Monitor if for. Only one thead at a time
can own the lock on the Monitor object and any others will Q waiting for
it
to free in ~FIFO order. Each thread must lock on the same object for this
to work.

--
William Stacey [MVP]

"Bob Bins" <ms****@hotmail .com> wrote in message
news:%2******** ********@tk2msf tngp13.phx.gbl. ..
| Is there a way to create a shared lock using the Monitor class or any
other
| synchronization class in .NET?
|
| So I have one resource that may have multiple threads using it at once
but
a
| second thread that when called must have exclusive access and cause the
| other threads to wait.
|
| I can't figure out how to do this with .Net.
|
| Thanks.
|
|

Mar 17 '06 #5
| No thats not suffecient.

Not so fast. It can be more efficient then a RW depending of what is really
going on (which may or may not be what you think is going on as you need to
look deeper when dealing with locks).

| I am writing a test application and I need to
| syncronize my database access. I need one funciton to execute at the rate
| around 100 times a seconds. This funcation will be called at the same
time
| from multiple threads. A second thread that is called once every second
| needs to lock so the other function does not get called while its running.
|
| In SQL you would do this by creating a shared lock on the first function
| then a exclusive lock on the second. This allows many threads to have
| shared access but once a thread has an exclusive lock any shared locks
will
| wait.

Monitor allows this. Effectively it is read or write lock but exclusive.
On my laptop I get 10 million locks in 364ms, so contention for the lock is
not going to be your bottle neck unless your holding the lock for a long
time in each "reader" thread. A RW lock is about 5 times slower then a
monitor, so you have to factor that in. You could end up with more
contention and slower overall solution. On a single CPU, there will not be
hardly any contention for the monitor if you can get out of the lock ~within
the thread time slice.

You going to be making DB network calls 100 times a second from multiple
readers? You sure that is even going to scale? If readers hold the lock
for more then 1 second, then your writer(s) are going to block anyway. You
also will have fairness issues. If a reader has the lock, new readers will
get a reader lock, pushing back new writers in time - so you can starve your
writer(s).

Could you instead use a reader queue and just have one reader thread making
requests as fast as possible? Would need to know more about the needs.

| It sounds like the ReaderWriterLoc k is what I will need. I didn't even no
| that class existed.

It may be what you need, but I would be careful. It could also not perform
as expected. A refactor of the design could prove more efficient.
Mar 17 '06 #6

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

Similar topics

0
2192
by: Nick Marden | last post by:
I am trying to use threads::shared to make a thread-safe object. It appears to be the case that copying a blessed-then-shared reference to another shared variable keeps the underlying structure intact (e.g., hashref & its contents) but forgets the blessedness. This makes my program pretty useless because I need to be able to copy or pass my lockable object(s) at various times in my program, without losing their identity as blessed...
96
6251
by: BadPony | last post by:
Anyone using Peoplesoft on a Federated UDB (shared nothing)Environment on Open System Platforms? Preferably AIX, but any war stories would be good. TEA EB-C
2
3741
by: Paul Wu | last post by:
From what I understand, in ASP.NET, each HTTP requests is serviced by a separate thread. So if my code uses a static Class with shared members and properties, I can manage concurrent access by using something like the Monitor or ReaderWriterLock class. It is rather difficult to simulate multiple threads in a debug environment so I am hoping someone can enlighten me by telling me what happens in the following scenario? A request is made to...
3
2550
by: Henri | last post by:
1) Do Shared methods always need to be synchronised to be thread-safe? 2) Why must SyncLock be used with a reference type? I can't get the relation between threads and a type. Thanks for your help. Henri
9
5405
by: Bob Day | last post by:
VS 2003, vb.net , sql msde... I have an application with multiple threads running. Its a telephony application where each thread represents a telephone line. For code that would be the same for each thread, I put in Shared methods as below. It is only now that I am realizing the complexity of multiple threads accessing shared methods. And, quite honestly, I am very confused. I have tried System.Threading.Monitor.Enter, Synclock,...
4
10129
by: Thomas F. O'Connell | last post by:
In using pg_dump to dump an existing postgres database, I get the following: pg_dump: WARNING: out of shared memory pg_dump: attempt to lock table <table name> failed: ERROR: out of shared memory HINT: You may need to increase max_locks_per_transaction. postgresql.conf just has the default of 1000 shared_buffers. The database itself has thousands of tables, some of which have rows
8
7680
by: Flack | last post by:
Hey guys, In my app I have a bitmap where drawing is done and in the form's paint method I show the bitmap: private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { if(MyBitmap != null) { Graphics g = e.Graphics;
1
2450
by: nass | last post by:
hello everyone.. i am not sure what is wrong - never liked strings anyway! my problem is that i am trying to save a QCstring in a shared memory segment and i get a segmentation fault.. here is the case statement that causes the trouble.. i could post the whole procedure but there is no point. similar case statements as the following one exisst in the procedure that save in the same memory segment (but at dif offest) floats, Q_UINT8 and...
7
8844
by: atlaste | last post by:
Hi, I have two different things I'd like to discuss here. Both are about cross-process synchronization of shared resources. The resources that are shared are: (1) an object writing to a file and (2) a file. I threat them as two different problems for various reasons. The first problem involves the object writing to a file. The problem might be best explained by thinking of a single log file that's appended by different programs that...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10324
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10090
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6739
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.