{"id":5119,"date":"2014-03-30T19:02:48","date_gmt":"2014-03-30T19:02:48","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/is-malloc-thread-safe-collection-of-common-programming-errors\/"},"modified":"2014-03-30T19:02:48","modified_gmt":"2014-03-30T19:02:48","slug":"is-malloc-thread-safe-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/is-malloc-thread-safe-collection-of-common-programming-errors\/","title":{"rendered":"Is malloc thread-safe?-Collection of common programming errors"},"content":{"rendered":"<p>Question: &#8220;is malloc reentrant&#8221;?<br \/>\nAnswer: no, it is not. Here is one definition of what makes a routine reentrant.<\/p>\n<p>None of the common versions of malloc allow you to reenter it (e.g. from signal handler). Note that a reentrant routine may not use locks, and almost all malloc versions in existence do use locks (which makes them thread-safe), or global\/static variables (which makes them thread-unsafe <em>and<\/em> non-reentrant).<\/p>\n<p>All the answers so far answer &#8220;is malloc thread-safe?&#8221;, which is entirely different question. To <em>that<\/em> question the answer is <em>it depends<\/em> on your runtime library, and possibly on the compiler flags you use. On any modern UNIX you&#8217;ll get a thread-safe malloc by default. On Windows, use <code>\/MT<\/code>, <code>\/MTd<\/code>, <code>\/MD<\/code> or <code>\/MDd<\/code> flags to get thread-safe runtime library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Question: &#8220;is malloc reentrant&#8221;? Answer: no, it is not. Here is one definition of what makes a routine reentrant. None of the common versions of malloc allow you to reenter it (e.g. from signal handler). Note that a reentrant routine may not use locks, and almost all malloc versions in existence do use locks (which [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5119","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5119","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5119"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5119\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5119"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5119"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5119"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}