473,327 Members | 2,081 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,327 software developers and data experts.

Please save me from shooting myself in the head!

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????

Nov 19 '05 #1
2 1721
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" <Wi*************@discussions.microsoft.com> wrote in
message news:AB**********************************@microsof t.com...
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????

Nov 19 '05 #2
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" <Wi*************@discussions.microsoft.com> wrote in
message news:AB**********************************@microsof t.com...
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????


Nov 19 '05 #3

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

Similar topics

3
by: James | last post by:
Please help - getting very desperate! Sun, 12 October 2003 05:39 I have PHPDEV 4.2.3 from Firepages.com.au as the upgrade to 4.3.0 did not work. I also had an abortive download from PHP.NET as...
1
by: koen colen | last post by:
Hello group, I hope you guys can help me out, I am modifying a piece of code from Joe Norman, I did found the code here: http://www.intranet2internet.com/public/default.asp?PAGE=sscript&ID=3 ...
2
by: ASallade | last post by:
Hello, I've scoured my books and the web, but am still daunted, hopefully some of the users in this newsgroup will have advice for my problem. I am not an experienced javascript programmer,...
7
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
3
by: fiefie.niles | last post by:
I would like to save a web page to a file and have the hyperlinks work when I bring the file back up. If the web page has a hyperlink like the following <a href="OurWeb/News/abcFile.htm">, after...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.