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

multi-threading: locking to return a value?

Is the lock in these two places needed for multi-threading? I thought
accessing a variable value should already be atomic. Thanks for your help or
comments.

bool stopping = false;

public bool Stopping
{
get
{
lock (stopLock)
{
return stopping;
}
}
}
public void Stop()
{
lock (stopLock)
{
stopping = true;
}
}

Nov 17 '05 #1
3 2856
What is stopLock?

Dale Preston

"Zeng" <Ze******@hotmail.com> wrote in message
news:um**************@TK2MSFTNGP14.phx.gbl...
Is the lock in these two places needed for multi-threading? I thought
accessing a variable value should already be atomic. Thanks for your help or comments.

bool stopping = false;

public bool Stopping
{
get
{
lock (stopLock)
{
return stopping;
}
}
}
public void Stop()
{
lock (stopLock)
{
stopping = true;
}
}

Nov 17 '05 #2
Yes they are necessary but not for an obvious reason.

The processor will attempt to cache information fso it doesn't have to re-read from physical memory and can maybe get the value from a register. But each thread gets its own copy of the registers to in this situation you have to ensure that physical memory writing occurs because you have two separate threads accessing the same block of memory. What the lock does is erect a *Memory Barrier* around the access to stopping which forces real writes to the memory. You can achieve the same thing by marking stopping as volatile - i.e.

volatile bool stopping = false;

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

Is the lock in these two places needed for multi-threading? I thought
accessing a variable value should already be atomic. Thanks for your help or
comments.

bool stopping = false;

public bool Stopping
{
get
{
lock (stopLock)
{
return stopping;
}
}
}
public void Stop()
{
lock (stopLock)
{
stopping = true;
}
}


[microsoft.public.dotnet.languages.csharp]
Nov 17 '05 #3
Zeng <Ze******@hotmail.com> wrote:
Is the lock in these two places needed for multi-threading? I thought
accessing a variable value should already be atomic.


Atomicity isn't all that's required though, unless you don't mind
getting stale data.

See http://www.pobox.com/~skeet/csharp/t...latility.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

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

Similar topics

4
by: OutsiderJustice | last post by:
Hi All, I can not find any information if PHP support multi-thread (Posix thread) or not at all, can someone give out some information? Is it supported? If yes, where's the info? If no, is it...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.