{"id":9999,"date":"2018-08-31T09:00:56","date_gmt":"2018-08-31T13:00:56","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=9999"},"modified":"2018-08-17T12:35:40","modified_gmt":"2018-08-17T16:35:40","slug":"safer-semaphore","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2018\/08\/31\/safer-semaphore\/","title":{"rendered":"Safer Semaphore"},"content":{"rendered":"<div xmlns:mwsh=\"http:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\n   <introduction><\/p>\n<p><a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495\">Sean<\/a>&#8216;s pick this week is <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/67848\">Safer wrapper for Windows-API Semaphore<\/a> by <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2103960\">Igor<\/a>.\n      <\/p>\n<p>   <\/introduction><\/p>\n<p>A while back, I wrote a <a href=\"https:\/\/blogs.mathworks.com\/pick\/2017\/02\/10\/semaphore-posix-and%E2%80%8B-windows\/\">blog post<\/a> about the Windows POSIX Semaphore function.  This had provided me with a way to make sure that certain processes had finished inside of <a href=\"https:\/\/www.mathworks.com\/help\/distcomp\/parallel-for-loops-parfor.html\">parallel <tt>for<\/tt>-loops<\/a> to avoid resource contention issues.\n   <\/p>\n<p>At the bottom, there was a caution to:<\/p>\n<pre>Use try\/catch around the operations you&#8217;re doing. If the process fails,\r\nyou want to release the semaphore to unblock the other workers. This can\r\ncause a hang if an error occurs because the loop will never complete as\r\nother workers are held.  If you reach this state, force kill the parallel\r\npool from the task manager.<\/pre>\n<p>Igor has developed a class that wraps around the semaphore to provide a timeout in case of this happening!<\/p>\n<p>It does a few other things too:<\/p>\n<div>\n<ul>\n<li>Take key as a character name rather than a number.<\/li>\n<li>Make unique key names.<\/li>\n<li>Set the maximum number of users in case the resource can have more than one user at once.<\/li>\n<\/ul><\/div>\n<p>And to top that off, Igor provided a nice demo of the functionality:<\/p>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">demo_semaphore<\/pre>\n<pre style=\"font-style:oblique\">i=1 \t time=0000.048 \t p1 \r\ni=3 \t time=0000.054 \t p1 \r\ni=1 \t time=0005.050 \t p2 - before wait \r\ni=3 \t time=0005.054 \t p2 - before wait \r\ni=1 \t time=0005.051 \t p3 - token granted \r\ni=1 \t time=0006.056 \t p4 - token released \r\ni=3 \t time=0005.055 \t p3 - token granted \r\ni=3 \t time=0006.071 \t p4 - token released \r\ni=1 \t time=0011.057 \t p5 - finished \r\ni=3 \t time=0011.079 \t p5 - finished \r\ni=5 \t time=0011.061 \t p1 \r\ni=5 \t time=0016.071 \t p2 - before wait \r\ni=2 \t time=0011.080 \t p1 \r\ni=2 \t time=0016.086 \t p2 - before wait \r\ni=5 \t time=0016.072 \t p3 - token granted \r\ni=5 \t time=0017.076 \t p4 - token released \r\ni=2 \t time=0016.089 \t p3 - token granted \r\ni=2 \t time=0017.092 \t p4 - token released \r\ni=5 \t time=0022.078 \t p5 - finished \r\ni=2 \t time=0022.094 \t p5 - finished \r\ni=4 \t time=0022.078 \t p1 \r\ni=4 \t time=0027.089 \t p2 - before wait \r\ni=7 \t time=0022.098 \t p1 \r\ni=7 \t time=0027.105 \t p2 - before wait \r\ni=4 \t time=0027.090 \t p3 - token granted \r\ni=4 \t time=0028.092 \t p4 - token released \r\ni=7 \t time=0027.106 \t p3 - token granted \r\ni=7 \t time=0028.121 \t p4 - token released \r\ni=4 \t time=0033.105 \t p5 - finished \r\ni=7 \t time=0033.121 \t p5 - finished \r\ni=8 \t time=0033.109 \t p1 \r\ni=8 \t time=0038.110 \t p2 - before wait \r\ni=6 \t time=0033.121 \t p1 \r\ni=6 \t time=0038.125 \t p2 - before wait \r\ni=8 \t time=0038.111 \t p3 - token granted \r\ni=8 \t time=0039.127 \t p4 - token released \r\ni=6 \t time=0038.126 \t p3 - token granted \r\ni=6 \t time=0039.143 \t p4 - token released \r\ni=8 \t time=0044.133 \t p5 - finished \r\ni=6 \t time=0044.148 \t p5 - finished \r\n<\/pre>\n<p>My only suggestion for Igor would be to use a check on the number of input arguments with <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/nargin.html\"><tt>nargin<\/tt><\/a> rather than <tt>exist(____, 'var')<\/tt> because the latter requires introspection <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/techniques-for-improving-performance.html#buwj1nn\">hurting performance<\/a>.\n   <\/p>\n<h3>Comments<a name=\"3\"><\/a><\/h3>\n<p>Give it a try and let us know what you think <a href=\"http:\/\/blogs.mathworks.com\/pick\/?p=9999#respond\">here<\/a> or leave a <a href=\"http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/67848#comments\">comment<\/a> for Igor.\n   <\/p>\n<p><script language=\"JavaScript\">\n<!--\n\n    function grabCode_f9c288c23955456d9cd838bc3e5cdd6b() {\n        \/\/ Remember the title so we can use it in the new page\n        title = document.title;\n\n        \/\/ Break up these strings so that their presence\n        \/\/ in the Javascript doesn't mess up the search for\n        \/\/ the MATLAB code.\n        t1='f9c288c23955456d9cd838bc3e5cdd6b ' + '##### ' + 'SOURCE BEGIN' + ' #####';\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f9c288c23955456d9cd838bc3e5cdd6b';\n    \n        b=document.getElementsByTagName('body')[0];\n        i1=b.innerHTML.indexOf(t1)+t1.length;\n        i2=b.innerHTML.indexOf(t2);\n \n        code_string = b.innerHTML.substring(i1, i2);\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\n\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \n        \/\/ in the XML parser.\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\n        \/\/ doesn't go ahead and substitute the less-than character. \n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\n\n        author = 'Sean de Wolski';\n        copyright = 'Copyright 2018 The MathWorks, Inc.';\n\n        w = window.open();\n        d = w.document;\n        d.write('\n\n<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\n\n\\n');\n      \n      d.title = title + ' (MATLAB code)';\n      d.close();\n      }   \n      \n-->\n<\/script><\/p>\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><a href=\"javascript:grabCode_f9c288c23955456d9cd838bc3e5cdd6b()\"><span style=\"font-size: x-small;        font-style: italic;\">Get<br \/>\n            the MATLAB code<br \/>\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><\/p>\n<p>      Published with MATLAB&reg; R2018b<\/p>\n<\/div>\n<p><!--\nf9c288c23955456d9cd838bc3e5cdd6b ##### SOURCE BEGIN #####\n%% Safer Wrapper for Windows-API Semaphore\n%\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/3208495 Sean>'s\n% pick this week is\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/67848 Safer wrapper\n% for Windows-API Semaphore> by\n% <http:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/2103960 Igor>.\n% \n\n%% \n%\n% A while back, I wrote a\n% <https:\/\/blogs.mathworks.com\/pick\/2017\/02\/10\/semaphore-posix-and%E2%80%8B-windows\/\n% blog post> about the Windows POSIX Semaphore function.  This had provided\n% me with a way to make sure that certain processes had finished inside of\n% <https:\/\/www.mathworks.com\/help\/distcomp\/parallel-for-loops-parfor.html\n% parallel |for|-loops> to avoid resource contention issues.\n%\n% At the bottom, there was a caution to:\n%\n%  Use try\/catch around the operations you\u00e2\u20ac&#x2122;re doing. If the process fails,\n%  you want to release the semaphore to unblock the other workers. This can\n%  cause a hang if an error occurs because the loop will never complete as\n%  other workers are held.  If you reach this state, force kill the parallel\n%  pool from the task manager.\n%\n% Igor has developed a class that wraps around the semaphore to provide a\n% timeout in case of this happening!\n%\n% It does a few other things too: \n% \n% * Take key as a character name rather than a number.\n% * Make unique key names.\n% * Set the maximum number of users in case the resource can have more than\n% one user at once.\n%\n% And to top that off, Igor provided a nice demo of the functionality:\n%\n\ndemo_semaphore\n\n%%\n% My only suggestion for Igor would be to use a check on the number of\n% input arguments with\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/nargin.html |nargin|> rather\n% than |exist(__, 'var')| because the latter requires introspection which\n% <https:\/\/www.mathworks.com\/help\/matlab\/matlab_prog\/techniques-for-improving-performance.html#buwj1nn\n% hurts performance>.\n\n%% Comments\n% \n% Give it a try and let us know what you think\n% <http:\/\/blogs.mathworks.com\/pick\/?p=9999#respond here> or leave a\n% <http:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/67848#comments\n% comment> for Igor.\n%\n \n\n##### SOURCE END ##### f9c288c23955456d9cd838bc3e5cdd6b\n--><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/pick\/files\/safersemaphore.png\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div>\n<p>Sean&#8216;s pick this week is Safer wrapper for Windows-API Semaphore by Igor.<\/p>\n<p>A while back, I wrote a blog post about the Windows POSIX Semaphore function.  This had provided me&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2018\/08\/31\/safer-semaphore\/\">read more >><\/a><\/p>\n","protected":false},"author":87,"featured_media":10007,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9999"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/87"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=9999"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9999\/revisions"}],"predecessor-version":[{"id":10015,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/9999\/revisions\/10015"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media\/10007"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=9999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=9999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=9999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}