Threadsafe VB

G

Guest

I have a website written in VB.Net. I have a utility library where every
function is Shared and written in VB.Net. Now I wonder if my utility library
is thread safe.
In could rewrite my library in C# and use the lock keyword.
Or I could use J# and use the synchronized keyword.
What in heck, does a VB programmer do for threadsafety?
 
T

TDAVISJR

Public Static myVariable as Integer

SyncLock myVariable
myVariable = myVariable + 1
End SyncLock
 
G

Guest

Davis,
What if I need to synchronise more than one variable?
Can I simply nest a few synclocks?
Arne.
 
T

TDAVISJR

I don't see any reason why. You may want to do a search on SyncLock and see
what you find out. I haven't used it must so I'm not the expert on it
 
S

Scott Allen

Hi Arne:

You can nest SyncLocks - but it gets dangerous quickly if you are not
careful.

Let's say thread #1 takes a lock on object A, while thread #2 takes a
lock on object B. Both threads do some processing and then decide they
need some additional locks. Thread #1 tries to lock object B, while
thread #2 tries to lock object A. This is a deadlock scenario where
each thread is blocking and waiting for the other thread to release a
resource, but neither thread can release either.

A simple approach is to take a more granular lock. This limits
scalabiity somewhat but you'll have to measure and see if performance
under stress is acceptable.
 
K

Kevin Spencer

Locking is not the best option. Thread-Safety is. Why? Because if an object
is locked, every thread that wants access to it has to wait their turn. This
can lead to at the very least performance degradation, and at the most a
deadlock situation.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top