473,795 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is *all* thread cached data flushed at MemoryBarrier

Obviously wrapping a critical section around access to some set of
shared state variables flushes any cached data, etc so that the threads
involved don't see a stale copy. What I was wondering is *what*
exactly gets flushed. Does the compiler some how determine the data
that is accessible from that thread, and flush just that set? (Seems
unlikely to me). Is it all data cached in registers etc? Or am I
overthinking this and instead it's more along the lines that a memory
barrier is just invalidating pages of memory such that when another
thread goes to access that memory it checks first to see if that page
needs to be refetched from main memory?

Thanks for any insights,
Tom

Aug 9 '06 #1
20 1962
Hi,

I do not understand clearly what is your question. MemoryBarrier (according
to MSDN) is only significative in Itanium processors, not sure if the .net
is even ported to the itanium to be honest.

My suggestion is to try to see what is the equivalent in the unmanaged
world.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<NO***********@ lycos.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Obviously wrapping a critical section around access to some set of
shared state variables flushes any cached data, etc so that the threads
involved don't see a stale copy. What I was wondering is *what*
exactly gets flushed. Does the compiler some how determine the data
that is accessible from that thread, and flush just that set? (Seems
unlikely to me). Is it all data cached in registers etc? Or am I
overthinking this and instead it's more along the lines that a memory
barrier is just invalidating pages of memory such that when another
thread goes to access that memory it checks first to see if that page
needs to be refetched from main memory?

Thanks for any insights,
Tom

Aug 9 '06 #2
When a lock is performed (or Monitor enetr/exit) in implicit read and
write memory barrier is performed to assure that the current thread
does not look at a "stale" value (one that was in a
cache/register/etc). This is the reason (for example) that you cannot
perform a loop on a simple boolean, waiting for it to be changed by
another thread. The "watching" thread is likely to continue to loop
after the boolean has changed value because it is seeing a stale value.
My question is, when this memory barrier is performed, what is the set
of data that gets flushed or gets invalidated (forcing a readthrough)
or gets written-through, or whatever.

Tom

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

I do not understand clearly what is your question. MemoryBarrier (according
to MSDN) is only significative in Itanium processors, not sure if the .net
is even ported to the itanium to be honest.

My suggestion is to try to see what is the equivalent in the unmanaged
world.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

<NO***********@ lycos.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Obviously wrapping a critical section around access to some set of
shared state variables flushes any cached data, etc so that the threads
involved don't see a stale copy. What I was wondering is *what*
exactly gets flushed. Does the compiler some how determine the data
that is accessible from that thread, and flush just that set? (Seems
unlikely to me). Is it all data cached in registers etc? Or am I
overthinking this and instead it's more along the lines that a memory
barrier is just invalidating pages of memory such that when another
thread goes to access that memory it checks first to see if that page
needs to be refetched from main memory?

Thanks for any insights,
Tom
Aug 9 '06 #3
NO***********@l ycos.com wrote:
When a lock is performed (or Monitor enetr/exit) in implicit read and
write memory barrier is performed to assure that the current thread
does not look at a "stale" value (one that was in a
cache/register/etc). This is the reason (for example) that you cannot
perform a loop on a simple boolean, waiting for it to be changed by
another thread. The "watching" thread is likely to continue to loop
after the boolean has changed value because it is seeing a stale
value.
My question is, when this memory barrier is performed, what is the set
of data that gets flushed or gets invalidated (forcing a readthrough)
or gets written-through, or whatever.
It's defined by the hardware architecture. In the case of x86, the amount
of memory flushed is 0, becuase x86 processors have strong cache coherency
guarantees. In other architectures it will be different, but in all cases,
following a memory barrier, all writes issued before the barrier will be
visible to all CPUs. Whether that's done by cache invalidation, updating
other caches, etc., is defined by the hardware architecture and generally
not visible to the programmer.

-cd
Aug 9 '06 #4
OK.

So my example of watching a boolean is only unsafe on x86 if
instrucution (re)ordering is an issue, not because multiple threads
will see different values for that variable.

That's wasn't completely clear to me before.

Thanks!
Tom

Carl Daniel [VC++ MVP] wrote:
NO***********@l ycos.com wrote:
When a lock is performed (or Monitor enetr/exit) in implicit read and
write memory barrier is performed to assure that the current thread
does not look at a "stale" value (one that was in a
cache/register/etc). This is the reason (for example) that you cannot
perform a loop on a simple boolean, waiting for it to be changed by
another thread. The "watching" thread is likely to continue to loop
after the boolean has changed value because it is seeing a stale
value.
My question is, when this memory barrier is performed, what is the set
of data that gets flushed or gets invalidated (forcing a readthrough)
or gets written-through, or whatever.

It's defined by the hardware architecture. In the case of x86, the amount
of memory flushed is 0, becuase x86 processors have strong cache coherency
guarantees. In other architectures it will be different, but in all cases,
following a memory barrier, all writes issued before the barrier will be
visible to all CPUs. Whether that's done by cache invalidation, updating
other caches, etc., is defined by the hardware architecture and generally
not visible to the programmer.

-cd
Aug 9 '06 #5
Tom,

I have to ask, why are you using MemoryBarrier instead of the lock
statement?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<NO***********@ lycos.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Obviously wrapping a critical section around access to some set of
shared state variables flushes any cached data, etc so that the threads
involved don't see a stale copy. What I was wondering is *what*
exactly gets flushed. Does the compiler some how determine the data
that is accessible from that thread, and flush just that set? (Seems
unlikely to me). Is it all data cached in registers etc? Or am I
overthinking this and instead it's more along the lines that a memory
barrier is just invalidating pages of memory such that when another
thread goes to access that memory it checks first to see if that page
needs to be refetched from main memory?

Thanks for any insights,
Tom

Aug 9 '06 #6
To be honest, the original question was for informational purposes
only.

I one of those people that always wants to know the "why", not just the
"how".

Any place where I am forced to say to myself "I know if I do this it
will work, but I don't really completely know *why* it does" is a place
where I start buying books, downloading articles, and hitting Google.

On this topic, I've found dedicated books on advanced concurreny to be
thin at best in the .NET world. Java on the other, which has a less
feature-rich set of concurrency options, has a number of excellent
texts available. If anyone can recommend a few highly-detailed books
on the topic (NOT books with just a chapter or two on the topic),
please let me know!

Tom
Nicholas Paldino [.NET/C# MVP] wrote:
Tom,

I have to ask, why are you using MemoryBarrier instead of the lock
statement?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<NO***********@ lycos.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Obviously wrapping a critical section around access to some set of
shared state variables flushes any cached data, etc so that the threads
involved don't see a stale copy. What I was wondering is *what*
exactly gets flushed. Does the compiler some how determine the data
that is accessible from that thread, and flush just that set? (Seems
unlikely to me). Is it all data cached in registers etc? Or am I
overthinking this and instead it's more along the lines that a memory
barrier is just invalidating pages of memory such that when another
thread goes to access that memory it checks first to see if that page
needs to be refetched from main memory?

Thanks for any insights,
Tom
Aug 9 '06 #7
Tom,

While I can't really recommend any FULL books on the topic, I can tell
you that for the most part, you will want to use the lock statement (which
in turn is really a call to Monitor.Enter/Monitor.Exit) over MemoryBarrier.
Monitor.Enter/Monitor.Exit is specified in the spec as having to work, and
you should always be able to depend on that.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<NO***********@ lycos.comwrote in message
news:11******** **************@ 75g2000cwc.goog legroups.com...
To be honest, the original question was for informational purposes
only.

I one of those people that always wants to know the "why", not just the
"how".

Any place where I am forced to say to myself "I know if I do this it
will work, but I don't really completely know *why* it does" is a place
where I start buying books, downloading articles, and hitting Google.

On this topic, I've found dedicated books on advanced concurreny to be
thin at best in the .NET world. Java on the other, which has a less
feature-rich set of concurrency options, has a number of excellent
texts available. If anyone can recommend a few highly-detailed books
on the topic (NOT books with just a chapter or two on the topic),
please let me know!

Tom
Nicholas Paldino [.NET/C# MVP] wrote:
>Tom,

I have to ask, why are you using MemoryBarrier instead of the lock
statement?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

<NO*********** @lycos.comwrote in message
news:11******* *************** @b28g2000cwb.go oglegroups.com. ..
Obviously wrapping a critical section around access to some set of
shared state variables flushes any cached data, etc so that the threads
involved don't see a stale copy. What I was wondering is *what*
exactly gets flushed. Does the compiler some how determine the data
that is accessible from that thread, and flush just that set? (Seems
unlikely to me). Is it all data cached in registers etc? Or am I
overthinking this and instead it's more along the lines that a memory
barrier is just invalidating pages of memory such that when another
thread goes to access that memory it checks first to see if that page
needs to be refetched from main memory?

Thanks for any insights,
Tom

Aug 9 '06 #8
Tom,

No, I do not believe it is safe. And even if it technically were I
certainly wouldn't bank on it because you may later port the code to
another framework version or hardware platform.

Maybe I'm wrong, but as I understand it the x86 memory model only
guarentees that writes cannot move with respect to other writes, but it
doesn't make any guarentees about reads. So it seems to me that you're
example is unsafe. But, I bet you'd have a hard time reproducing the
issue in reality. You'd almost certainly have to have a SMP system to
see it.

Here are some excellent links regarding memory barriers the .NET
framework.

<http://blogs.msdn.com/cbrumme/archive/2003/05/17/51445.aspx>
<http://discuss.develop .com/archives/wa.exe?A2=ind02 03B&L=DOTNET&P= R375>
<http://www.yoda.arachs ys.com/csharp/threads/volatility.shtm l>
<http://msdn.microsoft. com/msdnmag/issues/05/10/MemoryModels/>

Brian

NO***********@l ycos.com wrote:
OK.

So my example of watching a boolean is only unsafe on x86 if
instrucution (re)ordering is an issue, not because multiple threads
will see different values for that variable.

That's wasn't completely clear to me before.

Thanks!
Tom
Aug 9 '06 #9
<NO***********@ lycos.comwrote:
So my example of watching a boolean is only unsafe on x86 if
instrucution (re)ordering is an issue, not because multiple threads
will see different values for that variable.

That's wasn't completely clear to me before.
Reordering *is* an issue, however. Memory barriers are about preventing
*effective* reordering, whether that's done by the JIT or due to caches
etc.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 9 '06 #10

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

Similar topics

15
10624
by: DanGo | last post by:
Hi All, I'm trying to get my head around synchronization. Documentation seems to say that creating a volatile field gives a memorybarrier. Great! But when I do a little performance testing Thread.MemoryBarrier is 40 times slower than accessing a volatile int. Also I don't see any significant difference between the time to increment a volatile int and a
0
1522
by: Timo | last post by:
I'm trying to make a thread safe object cache without locking. The objects are cached by the id of the data dict given in __new__. Objects are removed from the cache as soon as they are no longer referenced. The type of the data must be a Python dict (comes from an external source). Here's what I've got so far: import time
5
9031
by: hugo27 | last post by:
hugo 27, Oct 9, 2004 Ref Docs: c.l.c FAQ article 12.26 . www.digitalmars.com sitemap.stdio.fflush Reading these Docs I understand that fflush does not summarily destroy or discard the flushed data. But it is not clear, in the case of fflush(stdin), where stdin is default C keyboard buffer, what actually becomes of the data.
14
1843
by: Michi Henning | last post by:
Hi, I can't find a statement about this in the threading sections in the doc... Consider: class Class1 { Class1() { _val = 42;
21
13106
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the server is on one line (\r\n at end) and is formed as - each of which is seperated by a space. Arguments with spaces in them are enclosed in quotations. So, I'm able to open a connection to the server. When I send a message to
5
4176
by: collection60 | last post by:
Hi people, I am writing a library that will implement a binary file format. Well... I'm thinking, as a design, it would be nice to have my library able to promise to not use more than a certain amount of RAM. I could have a "SetCacheSize(long NewSize);" function. So as part of that promise, when required memory exceeds that limit, the extra data is saved to disk and flushed from RAM.
1
1104
by: MindClass | last post by:
I've to modifying a file, then I use a method imported that access to that file and has to read the new data, but they are not read ( as if the data were not flushed at the moment even using .close() explicitly). --------------------------------------- ... ... # If it is not installed, it looking for the line and insert it. if not is_application:
2
2157
by: lwhitb1 | last post by:
Does anyone have any input on setting up my CAB application so that the application is thread safe, and cached appropiately? I read that this can be managed through Services, and dynamic injection. On the contrary, I was told that this can be handled using Enterprise Library cached application block. Last, but not least, i read you can implement this at the class level, creating immutable classes, and caching them accordingly. Any...
29
9152
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: --Thread 1-- 1. Reads TCPIP Buffer 2. Adds Buffer to Queue (q.size() to check queue isn't full, and q.push_back(...)) 3. Signals Reading Thread Event & Goes Back to Wait for More Messages on TCPIP
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10438
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7540
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.