<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>My Experiences With Thread Safe Code on Programmer's Insight</title><link>https://blog.ganapathy.xyz/tags/my-experiences-with-thread-safe-code/</link><description>Recent content in My Experiences With Thread Safe Code on Programmer's Insight</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Sun, 17 Mar 2013 09:00:20 -0700</lastBuildDate><atom:link href="https://blog.ganapathy.xyz/tags/my-experiences-with-thread-safe-code/index.xml" rel="self" type="application/rss+xml"/><item><title>My experiences with Thread safe code - 1</title><link>https://blog.ganapathy.xyz/2013/03/writing-thread-safe-code.html</link><pubDate>Fri, 15 Mar 2013 13:23:00 -0700</pubDate><guid>https://blog.ganapathy.xyz/2013/03/writing-thread-safe-code.html</guid><description>&lt;div&gt;&#10;In this post, let me share my views and experiences about things to be done while coding in a distributed environment with respect to thread safety.&lt;br&gt;&#10;&lt;br&gt;&#10;Persons&#10; who had taken a course on operating systems would be knowing about thread safe code. For those who doesn't know anything about it, &#10;as always let me give a gist.&lt;br&gt;&#10;&lt;br&gt;&#10;What is thread?&lt;br&gt;&#10;Thread is a light weight process.&lt;br&gt;&#10;&lt;br&gt;&#10;What is process?&lt;br&gt;&#10;A process is a program in execution.&lt;br&gt;&#10;&lt;br&gt;&#10;For&#10; example : Consider microsoft word. When you open it, the process starts&#10; running i.e this program is in execution. This program has lots of &#10;features like spell check, auto correct etc which are independent &#10;threads running along with it.&lt;br&gt;&#10;&lt;br&gt;&#10;In a single threaded &#10;process, only one flow of control exist.&amp;nbsp; The code executed by these &#10;processes&amp;nbsp; need not be thread-safe. In multithreaded programs, the same &#10;functions and the&#10;same resources may be accessed concurrently by several flows of control.&lt;br&gt;&#10;&lt;br&gt;&#10;Let me explain thread-safeness&amp;nbsp; through an example.&lt;br&gt;&#10;&lt;br&gt;&#10;Consider the case where there are group of people who come together to perform a particular task.&lt;br&gt;&#10;Each people has a role i.e they have some boundaries/memory which they don't cross.&lt;br&gt;&#10;Ex : Hadoop/GFS(Chubby)/Open Stack etc. &lt;br&gt;&#10;&lt;br&gt;&#10;To illustrate still more, consider a bathroom.&lt;br&gt;&#10;Only one person can use it at a time. Other persons should wait for their turn.&lt;br&gt;&#10;&lt;i&gt;&lt;br&gt;&lt;/i&gt;&#10;&lt;i&gt;A thread-safe code protects shared resources from concurrent&#10;access by locks.&amp;nbsp;&lt;/i&gt;&lt;br&gt;&#10;&lt;i&gt;Thread safety concerns only the implementation of&#10;a function and does not affect its external interface.&lt;/i&gt;&lt;br&gt;&#10;&lt;br&gt;&#10;For further info please read &lt;a href="http://pic.dhe.ibm.com/infocenter/aix/v7r1/index.jsp?topic=%2Fcom.ibm.aix.genprogc%2Fdoc%2Fgenprogc%2Fwriting_reentrant_thread_safe_code.htm" rel="noopener noreferrer"&gt;this.&lt;/a&gt; &lt;br&gt;&#10;&lt;br&gt;&#10;So having defined some basics of thread safe function, let me share my experiences on it.&lt;br&gt;&#10;&lt;br&gt;&#10;If you had read my previous post on &lt;a href="http://ganumurali.blogspot.com/2013/03/my-views-and-experience-with-design.html" rel="noopener noreferrer"&gt;design patterns&lt;/a&gt;&#10; , I would have spoken about something called service and session. I was&#10; writing a separate library in my previous concern where the objective was to &#10;provide some features like calculating current time and &#10;attaching to message based on customer specified version.&lt;br&gt;&#10;&lt;br&gt;&#10;This&#10; &#10;library written would be loaded by the service using dlopen and would be&#10; accessed for each and every session. So for calculating the current &#10;time, I was looking for some system calls which would return me the &#10;time/date etc and I came across this function called &lt;a href="http://www.cplusplus.com/reference/ctime/localtime/" rel="noopener noreferrer"&gt;localtime&lt;/a&gt;. I was happy, used it &amp;amp; finished my implementation and was waiting for code review comments from my technical lead.&lt;br&gt;&#10;&lt;br&gt;&#10;To my shock, the implementation was not supposed to be done in the way I had done.&lt;br&gt;&#10;My usage was proper. I have used proper libc function which is a tested code.&lt;br&gt;&#10;&lt;br&gt;&#10;But then why?&lt;br&gt;&#10;&lt;br&gt;&#10;I missed seeing the below one in&amp;nbsp; the &lt;a href="http://www.cplusplus.com/reference/ctime/localtime/" rel="noopener noreferrer"&gt;local time&lt;/a&gt; function.&lt;br&gt;&#10;&lt;br&gt;&#10;/***********************&lt;br&gt;&#10;The function accesses the object pointed by timer.&lt;br&gt;&lt;br&gt;The function also accesses and modifies a shared internal object, which may introduce data races on concurrent calls to &lt;a href="http://www.cplusplus.com/gmtime" rel="noopener noreferrer"&gt;gmtime&lt;/a&gt; and localtime. Some libraries provide an alternative function that avoids this data race: localtime_r.&lt;br&gt;&#10;&#10;***********************/&lt;br&gt;&#10;&lt;i&gt;I should have used localtime_r function () . Why ?&lt;/i&gt;&lt;br&gt;&#10;&lt;br&gt;&#10;Because there can be multiple parallel sessions running in the server and there is a potential chance of multiple sessions accessing this library at same time leading to corruption.&lt;br&gt;&#10;I was thankful as it was caught in review stage else it would have leaked after deployment.&lt;br&gt;&#10;&lt;br&gt;&#10;So the lesson learnt from this&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;b&gt;&lt;i&gt;"If&#10; you are programming in a multithreaded environment and you are looking &#10;for system functions defined in libc kind of libraries, confirm whether &#10;it is thread safe or not and then use it".&lt;/i&gt;&lt;/b&gt;&lt;br&gt;&#10;&lt;br&gt;&#10;However there are many others which i would post gradually. I just glossed over some things in this post.&lt;br&gt;&#10;&lt;br&gt;&#10;Thanks for reading. Lets conclude it for now about my view/experience on writing thread-safe programs.&lt;br&gt;&#10;&lt;br&gt;&#10;Please post any comments/suggestions so that I can improve.&lt;br&gt;&#10;&amp;nbsp; &lt;br&gt;&#10;&lt;br&gt;&lt;/div&gt;</description></item></channel></rss>