|
lock is a syntactic sugar for Monitor.Enter and Monitor.Exit .
From the doc
Use Enter to acquire the Monitor on the object passed as the parameter. If another thread has executed an Enter on the object, but has not yet executed the corresponding Exit, the current thread will block until the other thread releases the object.
the_jat wrote: I think the nutshell author was wrong when he said that the lock protected the variables
I don't think so. When the variable modification is done inside a lock block, it is thread safe.
|
|
|
|
|
Thanks!
Since the variables are private in the code, I think the author would have used properties and used the lock in the set part, (as the object locker is static), so that no other thread can modify the variables in the mean time.
Do you think that would work?
Here is the code again:
class ThreadSafe
{
static object locker = new object( );
static int val1, val2;
static void Go( )
{
lock (locker)
{
if (val2 != 0) Console.WriteLine (val1 / val2);
val2 = 0;
}
}
}
Thanks again!
|
|
|
|
|
the_jat wrote: I think the nutshell author was wrong when he said that the lock protected the variables
Yes, it would be more accurate to say something along the lines: "when the code correctly encloses any statement that accesses (for read or write) a shared variable with a lock, it achieves thread-safety for that variable." So the lock is used to achieve safety, it does not provide it by itself.
So the variable gets protected with a lock, not by a lock.
Luc Pattyn [Forum Guidelines] [My Articles]
I use ListBoxes for line-oriented text output (not TextBoxes), and PictureBoxes for pictures (not drawings).
modified on Friday, June 10, 2011 12:22 PM
|
|
|
|
|
the_jat wrote: static void Go( )
I suppose it is
public static void Go( )
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: I suppose it is...
The author didn't use the "public" keyword, the code posted is correct.
Thanks
|
|
|
|
|
OK, so how may you use it (i.e. How may Go be ever called?)?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
CPallini wrote: OK, so how may you use it (i.e. How may Go be ever called?)?
Though the author would know better, but one could also do something like:
public void GoGo()
{
........
........
if(....)
ThreadSafe.Go();
else
GoGoGo();
........
}
|
|
|
|
|
Hence you must call GoGo() in order to gain access to Go() , thus, provided GoGo() doesn't touch the variables, you've a thread safe method.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
the_jat wrote: but still one thread can(I guess) modify the static variables as the other is executing the if statement
This will not happen as if condition and modifying variable is in the lock. Only one thread will enter into the lock block.
|
|
|
|
|
Thanks for the reply, but
N a v a n e e t h wrote: This will not happen as if condition and modifying variable is in the lock
Did you mean the "modifying variable" or the "statement that modifies the variable"?
Can a lock prevent public static variables of a public class from being modifed by other class running other threads?
Thanks!
|
|
|
|
|
The lock itself only prevents the modification of the variable if every statement that modifies the variable is within a lock block, where the lock is using the same lock object.
|
|
|
|
|
Just an addition.
Think of a house containing your code. The frontdoor has a doorman, who keeps an eye that only one person is in the house at the same time. Inside the house is a box, containing a number (this is the static variable). If everybody who wants to look into the box, or put sth else in the box is using the front door to enter the house everything is okay. But if the house has a backdoor (the architect/programmer didn't pay attention to this) the doorman doesn't know if there is more than one person in the house. So it is possible that two or more persons get to that box and modify the content.
To avoid this, you could assure that there is no backdoor, or you order another doorman (another lock-construct) for the backdoor.
Hope this associativity makes it a bit clearer.
Regards
Sebastian
P.S.: The doorman doesn't know what is inside the house, he is just protecting the front door.
|
|
|
|
|
Thanks a lot!
I finally got it!
|
|
|
|
|
Can I programmatic enumerate running processes?
Thanks
dev
|
|
|
|
|
|
|
thanks mate that's what I was lookin for
dev
|
|
|
|
|
Yes That is possible , you need to use System.Diagnostics Namespace
to use this namespace you should add the following line of code
using System.Diagnostics;
at the top of you application
Then You can use Process.GetProcesses() function to get all the current running process
foreach (Process tempProcess in Process.GetProcesses())
{
listBox1.Items.Add(tempProcess.ToString());
}
This is a small application where i add list of process to a list box on my form , the name of list box is listBox1 .
Let me know if this was what you want .
-Regards
Bharat Jain
bharat.jain.nagpur@gmail.com
|
|
|
|
|
Hello all,
here i am facing some problem in implementing tcp/ip in my web space.
The application is like this.
In the web space, server program has to be executed which listens for the tcp clients. Once we connect to the corresponding server ip and port number from client, server has to accept the client and display the messages sent from the client.
Now the problem is
though i connect from the client by specifying approproiate server ip and port number, server is unable to accept it.
The code i have used in the server program is as follows.
Int32 port = pno;
IPAddress localAddr = IPAddress.Parse("<server ip>");
server = new TcpListener(localAddr, port);
server.Start();
while (true)
{
client = server.AcceptTcpClient();
}
Plese let me know if i am doing any thing wrong here.
I can provide any other information if required.
PS: the same thing in working in LAN
Thanks in advance.
|
|
|
|
|
Hi,
how does you know that the "server is unable to accept" the request? Do you receive an error message? If yes, please provide more information about this.
If it is working within the LAN, maybe it is a firewall problem? Maybe the server is not accepting requests on the port specified.
Regards
Sebastian
|
|
|
|
|
Good Morning guys,
I will need your help on this. i have Backups on a Folder on my C drive on this Path with a .BAK file extension and i want to restore them.
"C:\DATABASE\"
Now i want to go to the Folder and look for all the File names and Display them in the DropDownlist. Am doing this for a ASP.NET Page. i thought it would post it here , because its C# type of question.
Thank you
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswam@its.co.za
|
|
|
|
|
This should do it:
Directory.GetFiles(@"c:\database").ToList().ForEach(n => TheDrowDown.Items.Add(Path.GetFileName(n));
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
hi Guffa
i have not done IO before. So what is the name space ? Does this code supposed to be on one line ? If so i get Errors.
The "Tolist() " is not recognised,
The Foreach not working
.ForEach(n => cmbrestore.Items.Add(Path.GetFileName(n)); <br />
Thanks
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswam@its.co.za
|
|
|
|
|
Vuyiswa Maseko wrote: i have not done IO before. So what is the name space ?
System.IO.
Vuyiswa Maseko wrote: Does this code supposed to be on one line ?
Yes. However, C# is not a line based language, so you can write it in several lines if you like.
Vuyiswa Maseko wrote: The "Tolist()" is not recognised,
The Foreach not working
You need framework 3.5 for those. If you don't have that you have to loop through the names some other way.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
hi Guffa
Thanks , i Found a Solution
DirectoryInfo dir = new DirectoryInfo(@"C:\DATABASES\");
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
cmbrestore.Items.Add(Convert.ToString(file));
}
Thank you
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/ASP.NET/SQL7/2000/2005/2008
http://www.vuyiswamaseko.tiyaneProperties.co.za
vuyiswam@its.co.za
|
|
|
|