{"id":785,"date":"2012-02-24T14:02:20","date_gmt":"2012-02-24T14:02:20","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=785"},"modified":"2022-02-17T06:24:03","modified_gmt":"2022-02-17T06:24:03","slug":"making-class-variables-and-objects-thread-safe","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/threads\/making-class-variables-and-objects-thread-safe","title":{"rendered":"Making Class Variables and Objects Thread Safe"},"content":{"rendered":"<h4>Simple Locking Approach<\/h4>\n<pre><code>using namespace System::Threading;<\/code><\/pre>\n<h5>Create a flag<\/h5>\n<pre><code>\tprivate: bool ClassObjectsLocked;<\/code><\/pre>\n<h5>In your constructor set it up<\/h5>\n<pre><code>\tClassObjectsLocked = false;<\/code><\/pre>\n<h5>Then in functions which want to alter values but may be on a different thread<\/h5>\n<pre><code>\r\n\twhile (ClassObjectsLocked)\r\n\t\tThread::Sleep(1);\r\n<\/code><\/pre>\n<h5>In functions which want to read values<\/h5>\n<pre><code>\r\n\tClassObjectsLocked = true;\r\n\t...\r\n\tClassObjectsLocked = false;\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Simple Locking Approach using namespace System::Threading; Create a flag private: bool ClassObjectsLocked; In your constructor set it up ClassObjectsLocked = false; Then in functions which want to alter values but may be on a different thread while (ClassObjectsLocked) Thread::Sleep(1); In functions which want to read values ClassObjectsLocked = true; &#8230; ClassObjectsLocked = false;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-785","post","type-post","status-publish","format-standard","hentry","category-threads"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/785","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=785"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/785\/revisions"}],"predecessor-version":[{"id":1023,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/785\/revisions\/1023"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}