<div dir="ltr"><span style="color:rgb(32,33,36);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:12.8px;text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline"><span style="color:rgb(34,34,34);font-family:sans-serif;font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial;float:none;display:inline">Thank you so much! That was quite a thorough explanation. I understood now what you were trying to imply.</span><div style="color:rgb(34,34,34);font-family:sans-serif;font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">So, I'm going to do some research on how to make my code bulletproof. In the mean time, can you suggest something on how to make this happen? .<br></div><div style="color:rgb(34,34,34);font-family:sans-serif;font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">I'll get back to you, once I've figured out this thing. </div><div style="color:rgb(34,34,34);font-family:sans-serif;font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial"><br></div><div style="color:rgb(34,34,34);font-family:sans-serif;font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">Thank you again,</div><div style="color:rgb(34,34,34);font-family:sans-serif;font-size:small;background-color:rgb(255,255,255);text-decoration-style:initial;text-decoration-color:initial">Balraj.</div></span><br></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Jul 5, 2018 at 11:37 AM Balraj Singh <<a href="mailto:balraj.singh@zemosolabs.com">balraj.singh@zemosolabs.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Thank you so much! That was quite a thorough explanation. I understood now what you were trying to imply.<div>So, I'm going to do some research on how to make my code bulletproof. In the mean time, can you suggest something on how to make this happen? .<br></div><div>I'll get back to you, once I've figured out this thing. </div><div><br></div><div>Thank you again,</div><div>Balraj.</div></div><br><div class="gmail_quote"><div dir="ltr">On Wed, Jul 4, 2018 at 6:48 PM Dennis Buteyn <<a href="mailto:dennis.buteyn@xorcom.com" target="_blank">dennis.buteyn@xorcom.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div text="#000000" bgcolor="#FFFFFF">
<p><font face="Liberation Sans">Volatile only hints the compiler not
to perform read optimizations, to assume that the value is
changed by some external process. This keyword was designed for
use in hardware drivers where memory-mapped devices would alter
values in-memory of which the compiler was not aware of. The
volatile keyword however later found a place in multi-processor
systems as well where memory is shared between execution units.
The important thing to understand however is that it only
disabled read operations. It does not provide any safety in
regards to concurrent writes. This is what both me and Richard
were trying to explain to you.</font></p>
<p><font face="Liberation Sans">When two concurrent updates are
performed on the same variable (making it volatile does not
matter) the final value will be that of whichever update came
last. Since no control mechanism is in place to ensure proper
ordering, this is no different than throwing dice.</font></p>
<p><font face="Liberation Sans">To ensure proper ordering of writes
and to prevent modification by other execution units you must
use locks, of which you have none.<br>
</font></p>
<p><font face="Liberation Sans">The reasons why you may be getting
somewhat sane output is because:<br>
</font></p>
<ol>
<li><font face="Liberation Sans">The system you are testing on is
not a true SMP system.</font></li>
<li><font face="Liberation Sans">Process affinity restrictions
apply, forcing your application to run exclusively on a single
execution unit.</font></li>
<li><font face="Liberation Sans">Your test calls are not actually
concurrent.</font></li>
<li><font face="Liberation Sans">Your test calls are too slow.<br>
</font></li>
<li><font face="Liberation Sans">One of the many locks that
Asterisk uses to prevent corruption of its internal data
structures is making your changes work.</font></li>
<li><font face="Liberation Sans">Luck.</font></li>
</ol>
<font face="Liberation Sans">I understand you are getting "a" unique
ID, what you fail to understand is that it will not be the one you
are looking for.<br>
<br>
Compile the following code on gcc with default options and watch
it crash instantly despite looking "correct", you will understand
what we are trying to tell you.<br>
<br>
</font><tt>#include <pthread.h></tt><tt><br>
</tt><tt>#include <stdio.h></tt><font face="Liberation Sans"><br>
</font><tt><br>
</tt><tt>static volatile char * shared_var = NULL;</tt><tt><br>
</tt><tt><br>
</tt><tt>void * threadfunc(void * ptr) {</tt><tt><br>
</tt><tt> for (;;) {</tt><tt><br>
</tt><tt> shared_var = "mystring";</tt><tt><br>
</tt><tt> char c = *shared_var;</tt><tt><br>
</tt><tt> shared_var = NULL;</tt><tt><br>
</tt><tt> }</tt><tt><br>
</tt><tt> return NULL;</tt><tt><br>
</tt><tt>}</tt><tt><br>
</tt><tt><br>
</tt><tt>int main() {</tt><tt><br>
</tt><tt> pthread_t t1, t2;</tt><tt><br>
</tt><tt> pthread_create(&t1, NULL, threadfunc, NULL);</tt><tt><br>
</tt><tt> pthread_create(&t2, NULL, threadfunc, NULL);</tt><tt>
// comment this line to make it "work".<br>
</tt><tt> pthread_join(t1, NULL);</tt><tt><br>
</tt><tt> pthread_join(t2, NULL);</tt><tt><br>
</tt><tt>}</tt><font face="Liberation Sans"><br>
<br>
</font>
<pre class="m_-2730194890129403624m_1467212998880714760moz-signature" cols="72">Dennis Buteyn
Xorcom Ltd</pre>
<div class="m_-2730194890129403624m_1467212998880714760moz-cite-prefix">On 07/04/2018 03:40 PM, Balraj Singh
wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">I know the working of static variables in
Multi-threaded environment, but I never used static variables (
please check the code sent in one of my previous reply ) to save
the unique-id. I'm using volatile keyword which is completely
different from static variable ( one of the storage classes in c
), and the usage of volatile keyword here is to tell the
compiler to not perform optimisation ( while compilation ) on
this variable. This is useful in multi-threaded environment, as
in global variable should be declared volatile[1] in this type
of environment for shared global variables.
<div><br>
</div>
<div>Regarding the second comment about unique-id being whatever
at some point of time : let me clear this thing, I already
stated in my last reply that I'm getting unique-id ( belonging
to a call ) in logs correctly even in concurrent calls (
tested with more than 1 call ).</div>
<div><br>
</div>
<div>Please correct me if I'm wrong here. </div>
<div>@Richard Mudgett : Please validate the changes I did. </div>
<div><a href="https://docs.google.com/presentation/d/11LC-_e1EGL3X-n7gU_Z2uSNvPX41t6LVngO9AxBi4wo/edit?usp=sharing" target="_blank">[1]</a></div>
<div><br>
</div>
<div>Thank you all.</div>
<div><br>
</div>
</div>
<br>
<fieldset class="m_-2730194890129403624m_1467212998880714760mimeAttachmentHeader"></fieldset>
<br>
</blockquote>
<br>
</div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="m_-2730194890129403624gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><b><font color="#666666">Thanks, </font></b><div><span style="background-color:rgb(255,255,255)"><b><font color="#666666">Balraj.</font></b></span></div></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><b><font color="#666666">Thanks, </font></b><div><span style="background-color:rgb(255,255,255)"><b><font color="#666666">Balraj.</font></b></span></div></div></div>