<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Memory Barriers Wrap-up</title><link>http://msmvps.com/blogs/kernelmustard/archive/2004/09/20/13836.aspx</link><description>Hello blogosphere! I hope everyone had a great time this weekend puzzling through the mysteries of memory barriers. Personally, I spent the weekend coding and reading about realtivity (a recent post by Raymond Chen got me re-re-re-re-re-started on physics</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Sleeve Sealers</title><link>http://msmvps.com/blogs/kernelmustard/archive/2004/09/20/13836.aspx#1727794</link><pubDate>Mon, 28 Sep 2009 06:17:29 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1727794</guid><dc:creator>Sleeve Sealers</dc:creator><description>&lt;p&gt;Many technological developments will give you, the customer, real advantages – using less film, using less energy and running faster production speeds reliably&lt;/p&gt;
&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1727794" width="1" height="1"&gt;</description></item><item><title>re: Memory Barriers Wrap-up</title><link>http://msmvps.com/blogs/kernelmustard/archive/2004/09/20/13836.aspx#13912</link><pubDate>Tue, 21 Sep 2004 15:25:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:13912</guid><dc:creator>Steve Dispensa</dc:creator><description>Steve:&lt;br&gt;&lt;br&gt;Perhaps you could blog about some book recommendations, or online resources where one could read more about this subject?&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=13912" width="1" height="1"&gt;</description></item><item><title>re: Memory Barriers Wrap-up</title><link>http://msmvps.com/blogs/kernelmustard/archive/2004/09/20/13836.aspx#13907</link><pubDate>Tue, 21 Sep 2004 13:39:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:13907</guid><dc:creator>Steve Dispensa</dc:creator><description>&lt;a target="_new" href="http://www-106.ibm.com/developerworks/java/library/j-jtp02244.html"&gt;http://www-106.ibm.com/developerworks/java/library/j-jtp02244.html&lt;/a&gt;&lt;br&gt;&lt;br&gt;Gives a great write up of the Java memory model, why it's broken and how they want to fix it...  &lt;br&gt;&lt;br&gt;Unsuprisingly there is a proposal for Read/write/both barriers.&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=13907" width="1" height="1"&gt;</description></item><item><title>re: JVM example</title><link>http://msmvps.com/blogs/kernelmustard/archive/2004/09/20/13836.aspx#13863</link><pubDate>Mon, 20 Sep 2004 19:46:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:13863</guid><dc:creator>Steve Dispensa</dc:creator><description>Hrmm, view source on that last post to view the indentation correctly -- it makes much more sense that way ;)&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=13863" width="1" height="1"&gt;</description></item><item><title>re: JVM example </title><link>http://msmvps.com/blogs/kernelmustard/archive/2004/09/20/13836.aspx#13862</link><pubDate>Mon, 20 Sep 2004 19:44:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:13862</guid><dc:creator>Steve Dispensa</dc:creator><description>Breaker 19, There's a big pile up on i-90: 933, 1053&lt;br&gt;&lt;br&gt;Yes, that's right... read the code and see why.  If you uncomment out the syncronized blocks it works just fine.  Code follows:&lt;br&gt;&lt;br&gt;public class Test&lt;br&gt;{&lt;br&gt;   public static boolean keepTruckn = true;&lt;br&gt;&lt;br&gt;   static public class T1&lt;br&gt;   extends Thread&lt;br&gt;   {&lt;br&gt;      int goods[] = null;&lt;br&gt;&lt;br&gt;      public T1( int goods[] )&lt;br&gt;      {&lt;br&gt;         this.goods = goods;&lt;br&gt;      }&lt;br&gt;&lt;br&gt;      public void run()&lt;br&gt;      {&lt;br&gt;         while ( keepTruckn )&lt;br&gt;         {&lt;br&gt;            //synchronized ( goods )&lt;br&gt;            //{&lt;br&gt;               goods[0]++;&lt;br&gt;               goods[1]++;&lt;br&gt;            //}&lt;br&gt;         }&lt;br&gt;      }&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   static public class T2&lt;br&gt;   extends Thread&lt;br&gt;   {&lt;br&gt;      int goods[] = null;&lt;br&gt;&lt;br&gt;      public T2( int goods[] )&lt;br&gt;      {&lt;br&gt;         this.goods = goods;&lt;br&gt;      }&lt;br&gt;&lt;br&gt;      public void run()&lt;br&gt;      {&lt;br&gt;         while ( true )&lt;br&gt;         {&lt;br&gt;            //synchronized ( goods )&lt;br&gt;            //{&lt;br&gt;               if ( (goods[0] != goods[1]) &amp;amp;&amp;amp; keepTruckn )&lt;br&gt;                  break;&lt;br&gt;            //}&lt;br&gt;         }&lt;br&gt;&lt;br&gt;         System.err.println(&amp;quot;Breaker 19, There's a big pile up on i-90: &amp;quot; + goods[0] + &amp;quot;, &amp;quot; + goods[1] );&lt;br&gt;         keepTruckn = false;&lt;br&gt;      }&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   public static void main( String args[] )&lt;br&gt;   {&lt;br&gt;      int goods[] = new int[2];&lt;br&gt;      goods[0] = goods[1] = 0;&lt;br&gt;&lt;br&gt;      T1 t1 = new T1( goods );&lt;br&gt;      T2 t2 = new T2( goods );&lt;br&gt;&lt;br&gt;      t1.start();&lt;br&gt;      t2.start();&lt;br&gt;&lt;br&gt;      try&lt;br&gt;      {&lt;br&gt;         t2.join();&lt;br&gt;         t1.join();&lt;br&gt;      }&lt;br&gt;      catch ( Exception e ) { }&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=13862" width="1" height="1"&gt;</description></item><item><title>JVM Memory Barrier</title><link>http://msmvps.com/blogs/kernelmustard/archive/2004/09/20/13836.aspx#13857</link><pubDate>Mon, 20 Sep 2004 18:47:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:13857</guid><dc:creator>Steve Dispensa</dc:creator><description>Java doesn't really go that close to the underlying architecture to be as specific as saying 'do all of this before anything else' as far as I know.  You would have to do something along the lines of a mutex or spinlock via the synchroized keyword in java.  In java, synchronized can be added to any method's declaration to make it 'mutexed' across each instance of the object.  For instance, if you make an object with two methods, each synchronized, and created one instance of the object.  When instantiated, any thread can only run one of those methods at a time.  In addition, additional instances of the object are only restricted by THEIR instance... meaning that both objects could run method 1 at the exact same time, but one object cannot run both of it's methods at the same time.&lt;br&gt;&lt;br&gt;This is, in my opinion, handy, yet limiting.  luckily, java allows you to use the syncronized keyword in another more useful way.  You can basically treat any Object (I say Object because Object is the base class for any object in java) as a mutex/spinlock.  If you want to use specific locks, just do synchronized (someObj) { code; } around any code that you want to be locked based on someObj.  Only one synchronized (someObj) { code; } block can run at a time for the intance of someObj in that scope.  If you want to do a process wide lock, declare a static Object outside your class instance (like at the top of a .java file) and do syncrhonized blocks off of it.  This is equivilant to mutexes in the java world.&lt;br&gt;&lt;br&gt;Another handy feature of java's threading setup is the fact that each object you use or define, as long as it's a real object like Integer and not a language literal like int, can be used to synchronize, notify, and wait on.  The Object base class has methods called wait, notify, notifyAll.  One version of the wait method takes a timeout also.  Notify will cause one thread that is waiting to wake up.  NotifyAll will wake them all up.  Very handy indeed.  &lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=13857" width="1" height="1"&gt;</description></item><item><title>re: Memory Barriers Wrap-up</title><link>http://msmvps.com/blogs/kernelmustard/archive/2004/09/20/13836.aspx#13851</link><pubDate>Mon, 20 Sep 2004 18:05:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:13851</guid><dc:creator>Steve Dispensa</dc:creator><description>I always manage to forget something... sigh...&lt;br&gt;&lt;br&gt;The same issues show up in usermode code, by the way.  I didn't mean to imply that this was kernel-mode-only.  In fact, the SDK has MemoryBarrier() for the same reason.  For those of you who invest your time in the .Nyet silliness, there's even Thread.MemoryBarrier for you.  &lt;br&gt;&lt;br&gt;Anyone know how to do this in a JVM? :-)&lt;br&gt;&lt;br&gt;The sdk documentation idiotically says that you only have to use memory barriers &amp;quot;if you know your code will be running on multiprocessor architectures using weak-ordering CPUs&amp;quot;.  Hmm... Let me see... that seems to include all hyperthreaded P4s, doesn't it?  &lt;br&gt;&lt;br&gt;Even more hmmm... I have no idea if the two virtual CPUs that are exposed by an HT P4 have the same memory barrier issues.  Theoretically Intel could keep the entire physical chip self-consistent (in fact, there are statements to that effect in the manuals, but they're in a different context).&lt;br&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=13851" width="1" height="1"&gt;</description></item></channel></rss>