{"id":345,"date":"2010-04-14T16:30:32","date_gmt":"2010-04-14T15:30:32","guid":{"rendered":"http:\/\/www.frozax.com\/blog\/?p=345"},"modified":"2015-04-04T21:49:48","modified_gmt":"2015-04-04T21:49:48","slug":"multi-threading-xna-simple","status":"publish","type":"post","link":"https:\/\/www.frozax.com\/blog\/2010\/04\/multi-threading-xna-simple\/","title":{"rendered":"Yes, multi-threading in XNA\/C# can be that simple!"},"content":{"rendered":"<p>Multi-threading is often causing problems for multiple reasons: synchronization, deadlocks, concurrent access to memory&#8230; But with my recent experience with multi-threading in XNA\/C#, you can really do simple and efficient optimizations.<\/p>\n<p>While looking for possible performance improvements in <a href=\"http:\/\/www.frozax.com\/x360\">Spring Up Harmony for Xbox 360<\/a> (<a href=\"http:\/\/www.frozax.com\/games\/spring-up-harmony\">PC<\/a>), I have seen a simple situation really suited for multi-threading. You can see that situation with the following screenshot of my <a href=\"http:\/\/www.frozax.com\/blog\/2010\/04\/onscreen-profiling-xna\/\">in-game profiler<\/a> (click to enlarge):<br \/>\n<a href=\"http:\/\/www.frozax.com\/blog\/wp-content\/uploads\/2010\/04\/Frozax-4.png\" target=\"_blank\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-346\" title=\"Frozax-4\" src=\"http:\/\/www.frozax.com\/blog\/wp-content\/uploads\/2010\/04\/Frozax-4-300x168.png\" alt=\"Frozax-4\" width=\"300\" height=\"168\" \/><\/a><br \/>\nI have two tasks with performance issues since I started to run the game on Xbox 360 : the dynamics of the background effect and the computation of the estimation of the launch of the ball (running a simplified physics simulation multiple times, through Box2D.XNA). On this screenshot, these tasks are called VelGrid.U(9) and Launchers.U(14). The first part of VelGrid.U (Comp.Balls,10) uses the data of the physics simulation but DynaColLines(11) doesn&#8217;t. Therefore, the VelGrid.DynaColLines and Launchers.U are totally independent with each others. No data is shared between these code sections. Perfect candidates for my initiation to multi-threading in XNA! \ud83d\ude42<\/p>\n<p style=\"text-align: left;\">The <a href=\"http:\/\/www.albahari.com\/threading\/\">e-book found here<\/a> (recommended in multiple threading articles) is really awesome and helped me find the algorithm I need to use. I want to start the Launchers.U at the same time than VelGrid.DynaColLines and wait until both are finished. I know this is not optimal, but it allows me to simplify the problem and avoid so many threading pitfalls.<\/p>\n<p style=\"text-align: left;\">I just have to use two <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.threading.autoresetevent%28VS.95%29.aspx\">AutoResetEvent<\/a>s. The Launchers.U section is created in a thread and waits for a &#8220;Go&#8221; signal from VelGrid. After doing its job, it sets a Ready signal so that VelGrid knows when to carry on after doing its own stuff. Two different cases can happen, depending on which section finishes before the other.<\/p>\n<p style=\"text-align: left;\">Here are screenshots of the in-game profiler showing the differences (taken on X360). Every code section prefixed by a * means it&#8217;s executed on the thread.<\/p>\n<p style=\"text-align: left;\"><em><strong><span style=\"text-decoration: underline;\">Case 1<\/span><\/strong><\/em> : The code on the main thread completed before the code on the worker thread:<\/p>\n<p><a href=\"http:\/\/www.frozax.com\/blog\/wp-content\/uploads\/2010\/04\/thread_slower.jpg\" target=\"_blank\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-medium wp-image-350\" title=\"thread_slower\" src=\"http:\/\/www.frozax.com\/blog\/wp-content\/uploads\/2010\/04\/thread_slower-300x168.jpg\" alt=\"thread_slower\" width=\"300\" height=\"168\" \/><\/a><br \/>\nThe Velgrid section has to wait for the Launchers to complete, shown with the red arrow.<\/p>\n<p style=\"text-align: left;\"><em><strong><span style=\"text-decoration: underline;\">Case 2<\/span><\/strong><\/em> : the code on the main thread completed after the code on the worker thread:<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.frozax.com\/blog\/wp-content\/uploads\/2010\/04\/thread_faster.jpg\" target=\"_blank\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter  size-medium wp-image-349\" title=\"thread_faster\" src=\"http:\/\/www.frozax.com\/blog\/wp-content\/uploads\/2010\/04\/thread_faster-300x168.jpg\" alt=\"thread_faster\" width=\"300\" height=\"168\" \/><\/a><\/p>\n<p style=\"text-align: left;\">The thread is idle while the main thread still works (red arrow).<\/p>\n<p style=\"text-align: left;\">With this simple threading mechanism (coded and tested in a few hours only), I have been able to optimize the game. In the basic test level shown here, instead of 2.3 + 3.1 = 5.4 milliseconds spent on the main thread for both features, the game now spends 3.6 milliseconds, running two threads at the same time (33% gain). In this test scene, the Velgrid.DynaColLines is &#8220;free&#8221;.<\/p>\n<p style=\"text-align: left;\">Since I took these screenshots, I have made a few more changes : now, the whole Box2D update used while the game is in motion is threaded too.<\/p>\n<p style=\"text-align: left;\">Last important thing to know, on Xbox 360, you must choose which core to use for your thread. Just read the msdn page about the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.xna.net_cf.system.threading.thread.setprocessoraffinity.aspx\">SetProcessorAffinity<\/a>, everything you need to know is here. You can see in this article that I still have a few cores available \ud83d\ude42<\/p>\n<p style=\"text-align: left;\">In conclusion, if you have independent sections of code that take some time to run, you can improve that very quickly using this method.<\/p>\n<p style=\"text-align: left;\">\n<div class=\"addtoany_share_save_container addtoany_content_bottom\"><div class=\"a2a_kit a2a_kit_size_32 addtoany_list a2a_target\" id=\"wpa2a_1\"><a class=\"a2a_button_twitter\" href=\"http:\/\/www.addtoany.com\/add_to\/twitter?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2010%2F04%2Fmulti-threading-xna-simple%2F&amp;linkname=Yes%2C%20multi-threading%20in%20XNA%2FC%23%20can%20be%20that%20simple%21\" title=\"Twitter\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_facebook\" href=\"http:\/\/www.addtoany.com\/add_to\/facebook?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2010%2F04%2Fmulti-threading-xna-simple%2F&amp;linkname=Yes%2C%20multi-threading%20in%20XNA%2FC%23%20can%20be%20that%20simple%21\" title=\"Facebook\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_google_plus\" href=\"http:\/\/www.addtoany.com\/add_to\/google_plus?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2010%2F04%2Fmulti-threading-xna-simple%2F&amp;linkname=Yes%2C%20multi-threading%20in%20XNA%2FC%23%20can%20be%20that%20simple%21\" title=\"Google+\" rel=\"nofollow\" target=\"_blank\"><\/a><a class=\"a2a_button_reddit\" href=\"http:\/\/www.addtoany.com\/add_to\/reddit?linkurl=https%3A%2F%2Fwww.frozax.com%2Fblog%2F2010%2F04%2Fmulti-threading-xna-simple%2F&amp;linkname=Yes%2C%20multi-threading%20in%20XNA%2FC%23%20can%20be%20that%20simple%21\" title=\"Reddit\" rel=\"nofollow\" target=\"_blank\"><\/a>\n<script type=\"text\/javascript\"><!--\nwpa2a.script_load();\n\/\/--><\/script>\n<\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Multi-threading is often causing problems for multiple reasons: synchronization, deadlocks, concurrent access to memory&#8230; But with my recent experience with multi-threading in XNA\/C#, you can really do simple and efficient optimizations. While looking for possible performance improvements in Spring Up Harmony for Xbox 360 (PC), I have seen a simple situation really suited for multi-threading.&hellip; <a class=\"more-link\" href=\"https:\/\/www.frozax.com\/blog\/2010\/04\/multi-threading-xna-simple\/\">Continue reading <span class=\"screen-reader-text\">Yes, multi-threading in XNA\/C# can be that simple!<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,12,15,16,20,21],"tags":[95,100,104,113,114,124,140,141],"_links":{"self":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/345"}],"collection":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":2,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"predecessor-version":[{"id":1704,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/posts\/345\/revisions\/1704"}],"wp:attachment":[{"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.frozax.com\/blog\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}