473,385 Members | 1,486 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,385 software developers and data experts.

Does lock{} work across methods?

I have a class that ahs several methods. It pulls from a queue so it
has to be singleton at that point. The question I have is: does the
lock{} statement work across methods? If I have A(), B(), and C() and
I do this:
A()
{
lock{
SomeWork();
}
}

B()
{
lock{
Some();
Other();
Work();
}
}

Will A() be locked if a thread is currently in B()? Thanks for the
help.

Tom P.
Jul 17 '08 #1
2 1961
On Jul 17, 5:04*pm, "Tom P." <padilla.he...@gmail.comwrote:
I have a class that ahs several methods. It pulls from a queue so it
has to be singleton at that point. The question I have is: does the
lock{} statement work across methods? If I have A(), B(), and C() and
I do this:
A()
{
* lock{
* * * * *SomeWork();
* * *}

}

B()
{
* * *lock{
* * * * * *Some();
* * * * * *Other();
* * * * * *Work();
* * * }

}

Will A() be locked if a thread is currently in B()? Thanks for the
help.
If both locks are on the same object, and the calls are on two
different threads, then, yes, a call to A on one thread will be locked
while another thread is executing B.
Jul 17 '08 #2
Tom P. <pa***********@gmail.comwrote:
I have a class that ahs several methods. It pulls from a queue so it
has to be singleton at that point. The question I have is: does the
lock{} statement work across methods? If I have A(), B(), and C() and
I do this:
A()
{
lock{
SomeWork();
}
}

B()
{
lock{
Some();
Other();
Work();
}
}

Will A() be locked if a thread is currently in B()? Thanks for the
help.
That entirely depends on what you put in the bit you've missed out. The
"lock" statement works on a reference - it's not

lock {
code
}

it's

lock(reference) {
code
}

If you use the same reference in the two methods, then the lock will
indeed hold across methods.

See http://pobox.com/~skeet/csharp/threads for more info.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 17 '08 #3

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

Similar topics

4
by: Vinay C | last post by:
Hi, Can anyone clear me that, when should we use go for mutex, and in which situation should we opt for monitor, lock, semaphone and other objects, in a multithreaded application for synchronization...
1
by: Dave Crypto | last post by:
Hi there, SUMMARY: 1) When I use phpadmin to export a large database, the file created on my local PC never containes all the tables and information. It seems to max out at about 10mb. 2)...
5
by: Seeker | last post by:
Hello, I've read conflicting posts about . Does it or does it not lock the entire object? In my simple test it appears to block just the method but I wouldn't exactly call my meager test...
2
by: Fernando Rodríguez | last post by:
Hi, I've been reading the multithreading tutorial at http://www.yoda.arachsys.com/csharp/threads/locking.shtml (thanks Jon :-) and there's one thing I don't understand. Why do have to pass a...
4
by: Scott Johnson | last post by:
Hi I am converting some code from C# to VB.NET and I have come across a command that I can't find the VB equivalent. The C# command is 'lock' and I think it is used to lock a data type from...
8
by: nytimescnn | last post by:
I've read some discuession about lock() for thread-safe. I am wondering what will be the differce between below two code segment? Code 1: class A { private static Object padlock = new...
1
by: Ian | last post by:
I've just discovered the msclr::lock class in the C++ Support Library online documentation. This seems like a much cleaner way to implement thread protection than using...
20
by: Kurt | last post by:
Below is a class that can accessed from multiple threads and I want the class to be thread safe. I have a private timer member whose interval can be changed by different threads. Which is the correct...
6
by: Curious | last post by:
I have an arraylist used in three separate methods. In method #1 (event method), some items are removed from the arraylist if certain conditions are met; In method #2 (event method),...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.