locking static/shared objects

G

Guest

I have an ASP.net 2.0 website. This website have a module; Public Module
myModule.
Is all the data in this module shared and not thread safe?

I would like explicitly create a shared object in an asp.net web page;
private shared lockkey string

SyncLock me
lockey = "COBOLCALL"
monitor.enter(lockey)
dosomething() 'calling a single thread dll in cobol
monitor.exit(lockey)
end SyncLock

Would the above code be thread safe?

Arne Garvander
Certified Geek
Professional Data Dude
 
B

bruce barker

yes, but you are using two locking techiques at the same time;

the synclock is threadsafe for any threads created during the request,
but not between two requests happening at the same time. this is becuas
the synclock is taken the instance of the request. at diffent request is
locking a different object.


if lockkey is a privte shared, then monitor lock is threadsafe. not sure
why you assign a value to the lockkey inline as opposed to its constructor.

this assumes that this is the only code that calls the routine.

but you could just as easily use (if i know my vb syntax):

private shared lockkey as string = ""

synclock lockkey
dosomething
end synclock

-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top