{"id":57,"date":"2008-03-22T10:33:29","date_gmt":"2008-03-22T17:33:29","guid":{"rendered":"http:\/\/www.venzon.org\/?p=57"},"modified":"2008-03-22T11:00:10","modified_gmt":"2008-03-22T18:00:10","slug":"multithreading-prototypes","status":"publish","type":"post","link":"http:\/\/www.venzon.org\/?p=57","title":{"rendered":"Multithreading Prototypes"},"content":{"rendered":"<p>I started thinking about how to implement multi-threading in a game engine. \u00c2\u00a0My idea is that if every subsystem is independent from each other one, then the updates can be done in parallel (so, each subsystem does its tick in a separate thread). \u00c2\u00a0It&#8217;s then up to the higher level system to collect and distribute information between threads as necessary, which would only be done when the threads aren&#8217;t\u00c2\u00a0executing. <!--more--><\/p>\n<p>I&#8217;ve\u00c2\u00a0created\u00c2\u00a0a\u00c2\u00a0demo\u00c2\u00a0showing\u00c2\u00a0two\u00c2\u00a0ways\u00c2\u00a0this\u00c2\u00a0can\u00c2\u00a0be\u00c2\u00a0done:\u00c2\u00a0\u00c2\u00a0one\u00c2\u00a0with\u00c2\u00a0a fork join\u00c2\u00a0for\u00c2\u00a0each\u00c2\u00a0tick,\u00c2\u00a0which\u00c2\u00a0is\u00c2\u00a0sort\u00c2\u00a0of\u00c2\u00a0slow\u00c2\u00a0because\u00c2\u00a0of\u00c2\u00a0all\u00c2\u00a0of\u00c2\u00a0the\u00c2\u00a0overhead\u00c2\u00a0of\u00c2\u00a0all those fork joins\u00c2\u00a0(and\u00c2\u00a0that&#8217;s\u00c2\u00a0on\u00c2\u00a0Linux;\u00c2\u00a0I\u00c2\u00a0can&#8217;t\u00c2\u00a0imagine\u00c2\u00a0how\u00c2\u00a0slow\u00c2\u00a0Win32\u00c2\u00a0would\u00c2\u00a0go),\u00c2\u00a0and\u00c2\u00a0a\u00c2\u00a0second\u00c2\u00a0that\u00c2\u00a0uses\u00c2\u00a0semaphores\u00c2\u00a0to\u00c2\u00a0make\u00c2\u00a0sure\u00c2\u00a0that\u00c2\u00a0the\u00c2\u00a0frames\u00c2\u00a0are\u00c2\u00a0synced\u00c2\u00a0between\u00c2\u00a0threads, which has virtually zero overhead.<\/p>\n<p>The\u00c2\u00a0SCons\u00c2\u00a0project\u00c2\u00a0is\u00c2\u00a0set\u00c2\u00a0up\u00c2\u00a0to\u00c2\u00a0compile\u00c2\u00a0the\u00c2\u00a0semaphore\u00c2\u00a0version.\u00c2\u00a0\u00c2\u00a0SDL\u00c2\u00a0is\u00c2\u00a0required\u00c2\u00a0as\u00c2\u00a0SDL\u00c2\u00a0threads\u00c2\u00a0are\u00c2\u00a0used.\u00c2\u00a0\u00c2\u00a0This\u00c2\u00a0should\u00c2\u00a0be\u00c2\u00a0completely portable to any system that SDL runs on.<\/p>\n<p>The\u00c2\u00a0command-line\u00c2\u00a0arguments\u00c2\u00a0are:<\/p>\n<p>.\/test-semaphores\u00c2\u00a0[multithreading]\u00c2\u00a0[outerloop]\u00c2\u00a0[innerloop]\u00c2\u00a0[tasks]<\/p>\n<p>multithreading\u00c2\u00a0can\u00c2\u00a0be\u00c2\u00a0yes\u00c2\u00a0or\u00c2\u00a0no:\u00c2\u00a0yes\u00c2\u00a0means\u00c2\u00a0use\u00c2\u00a0multiple\u00c2\u00a0threads\u00c2\u00a0to\u00c2\u00a0execute\u00c2\u00a0each\u00c2\u00a0task\u00c2\u00a0in\u00c2\u00a0parallel (one thread per task),\u00c2\u00a0no\u00c2\u00a0means\u00c2\u00a0execute\u00c2\u00a0each\u00c2\u00a0task\u00c2\u00a0serially &#8212; default is no<\/p>\n<p>outerloop is an integer: how many times to do a tick &#8212; default is 100<\/p>\n<p>innerloop\u00c2\u00a0is\u00c2\u00a0an\u00c2\u00a0integer:\u00c2\u00a0how many loops each task does,\u00c2\u00a0to\u00c2\u00a0emulate\u00c2\u00a0the\u00c2\u00a0time\u00c2\u00a0a\u00c2\u00a0real\u00c2\u00a0task\u00c2\u00a0(subsystem tick)\u00c2\u00a0would\u00c2\u00a0take &#8212; default is 10000<\/p>\n<p>tasks\u00c2\u00a0is\u00c2\u00a0an\u00c2\u00a0integer:\u00c2\u00a0how\u00c2\u00a0many\u00c2\u00a0subsystem ticks we emulate &#8212; default is 3<\/p>\n<p>Here&#8217;s\u00c2\u00a0a\u00c2\u00a0link\u00c2\u00a0to\u00c2\u00a0the\u00c2\u00a0<a HREF=\"http:\/\/venzon.net\/content\/dev\/misc\/multithreading_test.tar.bz2\">prototype<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I started thinking about how to implement multi-threading in a game engine. \u00c2\u00a0My idea is that if every subsystem is independent from each other one, then the updates can be done in parallel (so, each subsystem does its tick in a separate thread). \u00c2\u00a0It&#8217;s then up to the higher level system to collect and distribute [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-prototyping"],"_links":{"self":[{"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.venzon.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=57"}],"version-history":[{"count":0,"href":"http:\/\/www.venzon.org\/index.php?rest_route=\/wp\/v2\/posts\/57\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.venzon.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.venzon.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.venzon.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}