473,796 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Suggestion for C#

I have a suggestion for C#
I would like reader/writer locks to be built in to the language.
When you want to aquire a loct on an object o you write
lock(o)
{
...//critical region
}

I would like to be able to write:
readlock(o)
{
...//critical region that only reads shared data
}

Behind the scenes, the first time readlock(o) was called, the locking
mechanism on o would be upgraded to a reader/writer lock and any active,
queued or future locks on o acqired by using lock(o) would become writer
locks.
That way there would be no difference for the programmer between usind a
reader/write lock and a normal lock, all you would need to know would be
that if you want to read and write shared data you use lock(o) and if you
only want to read shared data you just use readlock(o).

It may be feasible to downgrade the reader/writer lock to a normal lock
again if only lock(o) has been used for a long time with no readlock(o)'s
beeing called.

Reader/writer locks are very common and this approach would not force the
programmer to choose between a normal lock or a reader/writer lock, they
would be the same. Furthermore with my suggestion there would be no need for
creating a reader/writer lock object and creating try finally blocks etc.
The syntax is much easier to use.

If this is not the right place to post my suggestion, where do I send my
suggestion?

Kind Regards,
Allan Ebdrup
May 31 '07
20 2193
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** *************@p 47g2000hsd.goog legroups.com...
On May 31, 10:33 am, "Allan Ebdrup" <ebd...@noemail .noemailwrote:
>I see what you mean, I do use reader/writer locks for caching data that
is
read very often in somewhat heavy operations and very seldom written.

But how long does fetching the data take anyway, and how much
concurrency do you expect? If it's quick, it could well be that using
a ReaderWriter lock is still more expensive than a plain exclusive
monitor lock.
In one case fetching the data and updating the data (writer lock) takes
several minutes
Reading the data is called from many threads and the readlock is held for
several seconds on each thread.
>I still like the idea of not having to choose wether you use a normal
lock or
a reader/writer lock, and just upgrading the locking mecanism if needed.

It would be a nice idea if it were feasible - but I don't *think* it
is.
Don't be so pessimistic, almost anything is possible.
I don't think it could upgrade an existing, held lock into a reader/
writer lock. With *very* carefully defined semantics it might be
possible to work something out, but I'm not entirely sure. I don't
think it's worth the extra work though, personally.
That's why I want MS to do the work ;-), it only has to be done once.

Kind Regards,
Allan Ebdrup
May 31 '07 #11
On May 31, 11:31 am, "Allan Ebdrup" <ebd...@noemail .noemailwrote:
But how long does fetching the data take anyway, and how much
concurrency do you expect? If it's quick, it could well be that using
a ReaderWriter lock is still more expensive than a plain exclusive
monitor lock.

In one case fetching the data and updating the data (writer lock) takes
several minutes
Reading the data is called from many threads and the readlock is held for
several seconds on each thread.
Right. I'd say that's a fairly unusual situation - I rarely need to
hold locks for that long.
It would be a nice idea if it were feasible - but I don't *think* it
is.

Don't be so pessimistic, almost anything is possible.
Not within C# itself. If there were a CLR change as well, it might be
possible. That's clearly a much bigger task though, and I don't think
it's worth the effort.
I don't think it could upgrade an existing, held lock into a reader/
writer lock. With *very* carefully defined semantics it might be
possible to work something out, but I'm not entirely sure. I don't
think it's worth the extra work though, personally.

That's why I want MS to do the work ;-), it only has to be done once.
I think there are other features I'd be much more interested in MS
working on, to be honest.

Jon

May 31 '07 #12
"Moty Michaely" <Mo*****@gmail. comwrote in message
news:11******** **************@ h2g2000hsg.goog legroups.com...
On May 31, 12:09 pm, "Jon Skeet [C# MVP]" <s...@pobox.com wrote:
>Where possible, the language should (IMO) avoid having dependencies on
the framework. There are exceptions, but they should be tightly
controlled.

I would say that the language should try to avoid as much as it can
having dependencies of any kind :).
I agree that adding the feature to the language was a bad idea.
But I still like the idea of it beeing possible to upgrade a lock from a
monitor to a ReaderWriterLoc k if needed.
I guess I don't see why more work wasn't put into having those two locking
mechanisms behave more simular, seems to me that a ReaderWriterLoc k might as
well work on an object like a Monitor and might as well have Pulse so they
are more simular. Maybe I'm missing something but would Pulse on a
ReaderWriterLoc k not make sense? And would a ReaderWriterLoc k that works on
an object like Monitor not make sense?

Kind Regards,
Allan Ebdrup
May 31 '07 #13
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** *************@h 2g2000hsg.googl egroups.com...
I think there are other features I'd be much more interested in MS
working on, to be honest.
You could always suggest it to Anders next time you do lunch... ;-)
--
http://www.markrae.net

May 31 '07 #14
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:11******** *************@p 47g2000hsd.goog legroups.com...
On May 31, 10:33 am, "Allan Ebdrup" <ebd...@noemail .noemailwrote:
>I still like the idea of not having to choose wether you use a normal
lock or
a reader/writer lock, and just upgrading the locking mecanism if needed.

It would be a nice idea if it were feasible - but I don't *think* it
is.
Well differentiating between a normal lock and a read/write lock could be
arranged easily enough. Something like lock(obj), readLock(obj),
writeLock(obj). Read and write locks could only be done on a custom
'ReadWriteLock' class. The code itself for a (albeit very VERY basic)
read/wrtie lock would just be a counter incremented for each reader
currently locking.
May 31 '07 #15
Dave <fa*******@dodo .com.auwrote:
It would be a nice idea if it were feasible - but I don't *think* it
is.

Well differentiating between a normal lock and a read/write lock could be
arranged easily enough. Something like lock(obj), readLock(obj),
writeLock(obj). Read and write locks could only be done on a custom
'ReadWriteLock' class. The code itself for a (albeit very VERY basic)
read/wrtie lock would just be a counter incremented for each reader
currently locking.
But Allan's suggestion requires "upgrading" from a cheap mutex lock to
a reader/writer lock - and that's the tricky bit, IMO.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 31 '07 #16
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote:
On May 31, 9:50 am, "Allan Ebdrup" <ebd...@noemail .noemailwrote:
>Reader/writer locks are very common

I can't remember the last time I used one, and I've done plenty of
multi-threaded code.
At the other end of the spectrum, I use ReaderWriter locks quite a bit.

Our stuff runs on alot of multi-core and multi-processor machines, and we
actually end up with alot of lock contention as a result. I believe in most
multi-threaded apps, true lock contention is actually pretty rare and
locking is more for preventing corner cases and race conditions - so
Monitors work very well.

When I ran the profiler on an 16 processor (64GB of Memory) IA64 box, it
quickly became apparent that Monitor's were not going to cut it any longer.
We then switched our key datastructures over to use ReaderWriterLoc ks, and
things improved dramatically.

One of my frustrations here is the lack of a good overall solution. When I
was doing this profiling, I tried 3 solutions for locking around our various
stacks and queues:
Solution 1 - Monitors
Solution 2 - ReaderWriterLoc ks
Solution 3 - Lock Free Code

On a small box (1 or 2 processors) we saw:
- Monitors were clearly the best.
- ReaderWriterLoc ks were ok.
- Lock Free Stuff was slowest.

On meduim boxes (4 or 8 processors) we saw:
- Monitors were ok.
- ReaderWriterLoc ks were better.
- Lock Free Stuff was somewhere in between.

On big boxes (16 processors) we saw:
- Monitors Sucked
- ReaderWriterLoc k was ok.
- Lock Free was awesome.

(These results were across a mixture of x86, x64 and IA64 machines. I really
wish I had been more methodical in recording my results, as it would have
made a great blog entry....)

These results were very tied to our use cases - adding (and removing) data
to a queue for processing, checking for cache hits, expiring a cache,
updating a chache, and some other related cases.

There were 4 or 5 spots that really stood out as "Contention here!", and
this is where changing the lock structures made a difference. For the vast
majority of the locking we do, Monitors are the solution.

I've always wanted to "Write a smart locking class, that checks the
processor count at runtime, then uses the right lock & datastructure."

--
Chris Mullins, MCSD.NET, MCPD:Enterprise , Microsoft C# MVP
http://www.coversant.com/blogs/cmullins
May 31 '07 #17
I should mention the scales I'm talking about as well - if you're dealing
with small scale applications, everything I'm talking about is a waste of
time and you should just use whatever locking mechanism you and your team
understand the best (typically Monitors).

In the tests I described in the last post, we had 250,000 TCP connections
coming into a single server, with each connection actively sending and
receiving traffic.

This traffic is XMPP traffic, which means UTF-8 to UTF-16 encoding changes,
Xml Stanza ReAssembly (due to receive chunking), Xml Processing, Z-Lib
Compression, TLS Encryption, and a boatload of processing (Are my friends
online? Send me their status. Is anyone registred to be looking for me? Send
them my status. Do I have any offline messages pending? Send 'em over. Do I
have any offline events pending? Send them too. Gimme all my friends
Avatar's [even if they're offline, it should still be the newest avatar they
published], etc.)

I'm really looking forward to the networking improvements that Orcas brings.
The new Overlapped datastructures alone should give us a 50%+ boost in
scalability on comparable hardware....

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

"Chris Mullins [MVP]" <cm******@yahoo .comwrote in message
news:er******** ******@TK2MSFTN GP02.phx.gbl...
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote:
>On May 31, 9:50 am, "Allan Ebdrup" <ebd...@noemail .noemailwrote:
>>Reader/writer locks are very common

I can't remember the last time I used one, and I've done plenty of
multi-threaded code.

At the other end of the spectrum, I use ReaderWriter locks quite a bit.

Our stuff runs on alot of multi-core and multi-processor machines, and we
actually end up with alot of lock contention as a result. I believe in
most multi-threaded apps, true lock contention is actually pretty rare and
locking is more for preventing corner cases and race conditions - so
Monitors work very well.

When I ran the profiler on an 16 processor (64GB of Memory) IA64 box, it
quickly became apparent that Monitor's were not going to cut it any
longer. We then switched our key datastructures over to use
ReaderWriterLoc ks, and things improved dramatically.

One of my frustrations here is the lack of a good overall solution. When I
was doing this profiling, I tried 3 solutions for locking around our
various stacks and queues:
Solution 1 - Monitors
Solution 2 - ReaderWriterLoc ks
Solution 3 - Lock Free Code

On a small box (1 or 2 processors) we saw:
- Monitors were clearly the best.
- ReaderWriterLoc ks were ok.
- Lock Free Stuff was slowest.

On meduim boxes (4 or 8 processors) we saw:
- Monitors were ok.
- ReaderWriterLoc ks were better.
- Lock Free Stuff was somewhere in between.

On big boxes (16 processors) we saw:
- Monitors Sucked
- ReaderWriterLoc k was ok.
- Lock Free was awesome.

(These results were across a mixture of x86, x64 and IA64 machines. I
really wish I had been more methodical in recording my results, as it
would have made a great blog entry....)

These results were very tied to our use cases - adding (and removing) data
to a queue for processing, checking for cache hits, expiring a cache,
updating a chache, and some other related cases.

There were 4 or 5 spots that really stood out as "Contention here!", and
this is where changing the lock structures made a difference. For the vast
majority of the locking we do, Monitors are the solution.

I've always wanted to "Write a smart locking class, that checks the
processor count at runtime, then uses the right lock & datastructure."

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

May 31 '07 #18
Chris Mullins [MVP] <cm******@yahoo .comwrote:
"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote:
On May 31, 9:50 am, "Allan Ebdrup" <ebd...@noemail .noemailwrote:
Reader/writer locks are very common
I can't remember the last time I used one, and I've done plenty of
multi-threaded code.

At the other end of the spectrum, I use ReaderWriter locks quite a bit.

Our stuff runs on alot of multi-core and multi-processor machines, and we
actually end up with alot of lock contention as a result. I believe in most
multi-threaded apps, true lock contention is actually pretty rare and
locking is more for preventing corner cases and race conditions - so
Monitors work very well.

When I ran the profiler on an 16 processor (64GB of Memory) IA64 box, it
quickly became apparent that Monitor's were not going to cut it any longer.
We then switched our key datastructures over to use ReaderWriterLoc ks, and
things improved dramatically.
Are you hoping to move to .NET 3.5 any time soon? I'd expect that with
the "slim" reader/writer lock, it should get better again.

Interesting reading about your experiences with other solutions - not
entirely surprising, but annoying, as you say...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 31 '07 #19
I hope so, but it's really up in the air at this point.

The problem is that our Client and Server are both built on top of the same
SDK - and that SDK needs to be able to run anywhere .Net 2.0 is installed.
The SDK is what sits on top of the sockets, reads the Xml, and turns Xml
Stanzas into instances of .Net classes. This needs to keep running on top of
the .Net 2.0 CLR, as that's installed in a lot of spots.

For the "server" portions of the codebase (the stuff that sits on top of the
SDK), switching to .Net 3.5 will happen as soon as Orcas goes live. The
networking improvements alone will justity it. I'm really hoping we can
continue to use the same code base, and see the improvements. Ideally the
same 2.0 assemblies, when run in the 3.5 CLR, will get the networking
improvements (specifically, the improvements to the Overlapped
datastructures, which is what really hobbled us last time).

I also want to go the route Richter went in his Power Threading library -
he's got an abstraction layer for his locks, so it's really easy to switch
out the lock types, with ReaderWriterLoc kSlim being one of the first things
to try.

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

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************@m snews.microsoft .com...
Chris Mullins [MVP] <cm******@yahoo .comwrote:
>"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote:
On May 31, 9:50 am, "Allan Ebdrup" <ebd...@noemail .noemailwrote:
Reader/writer locks are very common

I can't remember the last time I used one, and I've done plenty of
multi-threaded code.

At the other end of the spectrum, I use ReaderWriter locks quite a bit.

Our stuff runs on alot of multi-core and multi-processor machines, and we
actually end up with alot of lock contention as a result. I believe in
most
multi-threaded apps, true lock contention is actually pretty rare and
locking is more for preventing corner cases and race conditions - so
Monitors work very well.

When I ran the profiler on an 16 processor (64GB of Memory) IA64 box, it
quickly became apparent that Monitor's were not going to cut it any
longer.
We then switched our key datastructures over to use ReaderWriterLoc ks,
and
things improved dramatically.

Are you hoping to move to .NET 3.5 any time soon? I'd expect that with
the "slim" reader/writer lock, it should get better again.

Interesting reading about your experiences with other solutions - not
entirely surprising, but annoying, as you say...

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

May 31 '07 #20

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

Similar topics

11
2081
by: John Wellesz | last post by:
Hello, It would be great if there was an option to tell PHP to let the user manage all the HTTP headers instead of sending what it thinks is good for the programmer... For example when you write: header("Status: 200 OK"); header("Location: /my_internal_redirected_page.php");
5
1937
by: John | last post by:
Hi: I'd like to implement a simple map, which is a 2-D plane with many points, e.g., 100. The points are not evenly distributed, i.e., some points may have two neighbor points; some may have 5 or 6 neighbor points. Could anyone suggest me a data structure for it. Thanks in advance. John
10
2625
by: Paulo Jan | last post by:
Hi all: Let's say I'm designing a database (Postgres 7.3) with a list of all email accounts in a certain server: CREATE TABLE emails ( clienteid INT4, direccion VARCHAR(512) PRIMARY KEY, login varchar(128) NOT NULL,
7
1973
by: J.Marsch | last post by:
I don't know whether this is the appropriate place to give product feedback, but here goes: I would love to see some kind of diagnostic to let me know when implicit boxing has occurred. We have had a few instances where one developer or another was working on code, and did not realize that by passing their value type to a method that accepted an object parameter (or in many cases, a delegate).
2
1202
by: vinay | last post by:
I have a scenario, need your suggestion.. Our clients are already using the forms authentication where we check the User/Pwd from SQL svr Database. We also have some SETTINGS for the user saved in the database(helps us to restrict the user access some things) . We wanted to implement AD authentication for the same and also keep the existing SQL SVr User DB. My Q would be how to MAP the LDAP user to the SQL svr User, so we can get
13
1631
by: sandeep chandra | last post by:
Hey guys, I am new to this group.. i never know wot s going on in this group.. but wot made be brought here is cpp.. guys am currently a part of onw reaserch ... am new to everything.. i can prog in c and a bit in cpp.. to say ..am a fair programmer... there is a new problem in my research work.. which if solved , gives me a gud tool to develop my prog in a much better way.. lemme describe my prob... i have a routine in...
17
1924
by: Jedrzej Miadowicz | last post by:
I recently (re)discovered data binding in Windows Forms thanks to its advances in Visual Studio 2005. As I looked a little deeper, however, I realize that it still suffers from an irksome tendency to stick a whole bunch of literal strings in my code. Quite frankly, I consider them a plague imposed on developers by the RAD designers that come with Visual Studio. The problem with using literal strings to refer to controls, data sources,...
4
1212
by: John Salerno | last post by:
I apologize for the slightly off-topic nature, but I thought I'd just throw this out there for anyone working on text editors or IDEs with auto-completion. I think it should be a feature, when an item is selected for auto-completion in a drop-down box, that pressing the spacebar (in addition to tab or enter) will automatically finish the word and add a space. This is how Microsoft's new IDEs for .NET work, and I found it very helpful to...
0
9531
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
10459
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
10187
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
10018
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5446
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4120
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
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.