Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Please save me from shooting myself in the head!

Reply
Thread Tools

Please save me from shooting myself in the head!

 
 
=?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=
Guest
Posts: n/a
 
      10-18-2005
I'm dying here... I have an object that I use to access objects in the
session state. I may be reading and writing at the same time, so I am trying
to make the accessors thread safe using ReaderWriter locks. Here's a
stripped down example of my class:

Class SessionLogic{
private static ReaderWriterLock myObjectLock = new ReaderWriterLock();

....

public MyObject MyObject{
get
{
MyObject foo = null;
myObjectLock.AcquireReaderLock(5000);
try
{
foo = this.session["MyObject"] as MyObject;
try
{
LockCookie monster = myObjectLock.UpgradeToWriterLock(5000);
try
{
foo = new MyObject();
this.session["MyObject"] = foo;
}
finally
{
myObjectLock.DowngradeFromWriterLock(ref monster);
}
}
catch (Exception ex)
{
string message = ex.Message; // just so I can see
}
finally
{
myObjectLock.ReleaseReaderLock();
}
return foo;
}
}
}

This structure is almost word for word out of MS' documentation on
ReaderWriterLock. Yet whenever I run it, I get an error: "Attempt to
release mutex not owned by caller." What the... Its the ONLY CALLER! What
is happening here????

 
Reply With Quote
 
 
 
 
Bruce Barker
Guest
Posts: n/a
 
      10-18-2005
not sure why you are doing this. add/getting references from session are
thread safe. so there is no need for the locking code. also asp.net
serializes access to the session by only running one request at a time for
the same session.

now to the problem with your code, you use a global lock so any thread
accessing the session object (even though the session objects are different
for threads processing diffent sessions) must take a lock and release. the
lock is created under the creditials of the user of the first request, so
other users will not be able to access it unless you change the security of
the mutex, to allow read/write from anyone. turn off authenication to see
this.

-- bruce (sqlwork.com)


"William Sullivan" <(E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
> I'm dying here... I have an object that I use to access objects in the
> session state. I may be reading and writing at the same time, so I am
> trying
> to make the accessors thread safe using ReaderWriter locks. Here's a
> stripped down example of my class:
>
> Class SessionLogic{
> private static ReaderWriterLock myObjectLock = new ReaderWriterLock();
>
> ...
>
> public MyObject MyObject{
> get
> {
> MyObject foo = null;
> myObjectLock.AcquireReaderLock(5000);
> try
> {
> foo = this.session["MyObject"] as MyObject;
> try
> {
> LockCookie monster = myObjectLock.UpgradeToWriterLock(5000);
> try
> {
> foo = new MyObject();
> this.session["MyObject"] = foo;
> }
> finally
> {
> myObjectLock.DowngradeFromWriterLock(ref monster);
> }
> }
> catch (Exception ex)
> {
> string message = ex.Message; // just so I can see
> }
> finally
> {
> myObjectLock.ReleaseReaderLock();
> }
> return foo;
> }
> }
> }
>
> This structure is almost word for word out of MS' documentation on
> ReaderWriterLock. Yet whenever I run it, I get an error: "Attempt to
> release mutex not owned by caller." What the... Its the ONLY CALLER!
> What
> is happening here????
>



 
Reply With Quote
 
 
 
 
=?Utf-8?B?V2lsbGlhbSBTdWxsaXZhbg==?=
Guest
Posts: n/a
 
      10-19-2005
Thank you, my head now remains whole.

"Bruce Barker" wrote:

> not sure why you are doing this. add/getting references from session are
> thread safe. so there is no need for the locking code. also asp.net
> serializes access to the session by only running one request at a time for
> the same session.
>
> now to the problem with your code, you use a global lock so any thread
> accessing the session object (even though the session objects are different
> for threads processing diffent sessions) must take a lock and release. the
> lock is created under the creditials of the user of the first request, so
> other users will not be able to access it unless you change the security of
> the mutex, to allow read/write from anyone. turn off authenication to see
> this.
>
> -- bruce (sqlwork.com)
>
>
> "William Sullivan" <(E-Mail Removed)> wrote in
> message news:(E-Mail Removed)...
> > I'm dying here... I have an object that I use to access objects in the
> > session state. I may be reading and writing at the same time, so I am
> > trying
> > to make the accessors thread safe using ReaderWriter locks. Here's a
> > stripped down example of my class:
> >
> > Class SessionLogic{
> > private static ReaderWriterLock myObjectLock = new ReaderWriterLock();
> >
> > ...
> >
> > public MyObject MyObject{
> > get
> > {
> > MyObject foo = null;
> > myObjectLock.AcquireReaderLock(5000);
> > try
> > {
> > foo = this.session["MyObject"] as MyObject;
> > try
> > {
> > LockCookie monster = myObjectLock.UpgradeToWriterLock(5000);
> > try
> > {
> > foo = new MyObject();
> > this.session["MyObject"] = foo;
> > }
> > finally
> > {
> > myObjectLock.DowngradeFromWriterLock(ref monster);
> > }
> > }
> > catch (Exception ex)
> > {
> > string message = ex.Message; // just so I can see
> > }
> > finally
> > {
> > myObjectLock.ReleaseReaderLock();
> > }
> > return foo;
> > }
> > }
> > }
> >
> > This structure is almost word for word out of MS' documentation on
> > ReaderWriterLock. Yet whenever I run it, I get an error: "Attempt to
> > release mutex not owned by caller." What the... Its the ONLY CALLER!
> > What
> > is happening here????
> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Canon PowerShot S400 'Shooting mode dial' not working... help please PWalker Digital Photography 2 08-07-2005 09:42 PM
Thought I'd save time by not doing them myself mark_digital Digital Photography 3 05-29-2004 05:16 PM
Please Help! java.mail trouble shooting for MMS sending over mm3 Andrea Maschio Java 0 12-05-2003 02:45 PM
Someone please save me from myself!!! ASP.Net templates?? Rob Meade ASP .Net 4 12-03-2003 03:01 PM
Re: Please advise on shooting "floppy" dolls ConTactMe Digital Photography 1 07-11-2003 04:32 AM



Advertisments