{"id":351,"date":"2008-06-15T16:52:54","date_gmt":"2008-06-15T20:52:54","guid":{"rendered":"http:\/\/www.rakkar.org\/blog\/?p=351"},"modified":"2008-06-15T16:52:54","modified_gmt":"2008-06-15T20:52:54","slug":"system-for-inline-nonblocking-functions","status":"publish","type":"post","link":"https:\/\/rakkar.org\/blog\/index.php\/2008\/06\/15\/system-for-inline-nonblocking-functions\/","title":{"rendered":"System for inline nonblocking functions"},"content":{"rendered":"<p>\t\t\t\tOne of the most annoying things with database calls is creating a thread to process the call.<\/p>\n<p>Here&#8217;s some psuedocode to illustrate the issue:<\/p>\n<p>void QueryDB() {<br \/>\nCreateThread(SelectResults, callback);<br \/>\n}<br \/>\nint SelectResults(Callback *callback) {<br \/>\nresult = Process(&#8220;select * from db&#8221;);<br \/>\ncallback(result);<br \/>\n}<br \/>\nvoid Callback(Result *result) {<br \/>\nprintf(&#8220;result is %i&#8221;, result);<br \/>\n}<\/p>\n<p>Doing the same thing inline is much easier. It&#8217;s one function rather than 3, and you also have context about the call.<\/p>\n<p>void QueryDB() {<br \/>\nresult = Process(&#8220;select * from db&#8221;);<br \/>\nprintf(&#8220;result is %i&#8221;, result);<br \/>\n}<\/p>\n<p>However, it blocks your entire program.<\/p>\n<p>I came up with an &#8220;InlineFunctorProcessor&#8221; system that has the benefits of both. It allows you to do blocking calls all within the context of one function, but runs the blocking call in a thread while your program continues running.<\/p>\n<p>printf(&#8220;Enter file to read: &#8220;);<br \/>\nReadFileFunctor *rff = new ReadFileFunctor;<br \/>\ngets(rff->fn);<br \/>\n\/\/ Nonblocking, but processes as easily as if it were<br \/>\nYieldOnFunctor(rff);<br \/>\nif (rff->data)<br \/>\n\tprintf(&#8220;Got file data\\n%s\\n&#8221;, rff->data);<br \/>\nelse<br \/>\n\tprintf(&#8220;Open failed\\n&#8221;);<br \/>\ndelete rff;<\/p>\n<p>The way the system works is through recursion, thus saving prior calls on the stack. The yield function calls the main application update loop &#8211; or any other function you want to call while the function is processing. That function is responsible for calling InlineFunctorProcessor::UpdateIFP(), which if it returns true, the function should also return.<\/p>\n<p>Here&#8217;s the code for the base class:<br \/>\n<a HREF=\"http:\/\/raknetjenkinsso.svn.sourceforge.net\/viewvc\/raknetjenkinsso\/trunk\/Source\/InlineFunctor.h?view=markup\">Header<\/a><br \/>\n<a HREF=\"http:\/\/raknetjenkinsso.svn.sourceforge.net\/viewvc\/raknetjenkinsso\/trunk\/Source\/InlineFunctor.cpp?view=markup\">Source<\/a><\/p>\n<p>Here&#8217;s a solution that demonstrates the functionality<br \/>\n<a HREF=\"downloads\/InlineNonblocking.zip\">Download<\/a>\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most annoying things with database calls is creating a thread to process the call. Here&#8217;s some psuedocode to illustrate the issue: void QueryDB() { CreateThread(SelectResults, callback); } int SelectResults(Callback *callback) { result = Process(&#8220;select * from db&#8221;); callback(result); } void Callback(Result *result) { printf(&#8220;result is %i&#8221;, result); } Doing the same thing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/351"}],"collection":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=351"}],"version-history":[{"count":0,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/351\/revisions"}],"wp:attachment":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}