top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

Should we protect single assignment or if statement for threadsafety?

+1 vote
72 views

As far as I know single assignment should be atomic operation so should be OK but not sure.
Any thoughts?

posted Dec 11, 2014 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
In context of multithreading ?
Yes in context of multi-threading.

1 Answer

+1 vote

Thread safety is applicable in the context of multi-threaded programs.

You can't be sure about that, if it is a global or static variable, because it may be accessed by more than one
thread at a same time with different purpose(assume read and write).
Which may cause problem.

And If it is a local varible, than it is totally threadsafe because each thread has its own private copy.

Yes single assignment is not a threadsafe.
Lets understand this with example:

Lets assume a = 6 and b = 10
a = b //This statement is got divided into different statement in assembly language like this:
R1 = b ----> step 1 (R1 is a register insde the CPU, 1st b has to be stored in R1)
a = R1 ----> step 2 (Then R1 value whill be assign to a)

So before completion of the above steps (i.e step 1 and step 2), if another process try to access the value of a it may get the some other value.

In the aboe example: if process2 want to access the value of a while process1 is in step 1 i.e (R1 = b)

So the process2 will get value of a as 6, but we are expecting it to be 7.

answer Dec 11, 2014 by Arshad Khan
Thanks Arshad, but the question is in case of multithreaded environment thread safety will come into picture only if thread switch is possible between two statement. Now my question is if thread switch is possible in between of assignment or in if statement.
Sorry I am not getting this (if thread switch is possible in between of assignment or in if statement).

If you could provide with some code statement.
Say we have a statement like a=b+c then thread switch can happen between addition and assignment and other thread can get a unstable value of a. Now the question is if we have a statement a=b then if switch can not happen in between of assignment then other thread will receive a stable value ie. prior to statement or after to the statement.

Now question is in the single assignment operation can thread switch happen or not.
Hi Salil,
I have updated the ans, have a look.

Hope your doubt will be clear. :)
Make sense thanks for clearing my doubt.

I was in the impression that assignment must be atomic and should be completed in one machine cycle.
Similar Questions
+3 votes

My question is regarding the PC register value of which is giving to the created thread.

if I create a new process with fork() the PC value will be copied from parent to child as PC value is the next command in the code.

However if I create a new thread as following

#include <pthread.h>
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void), void *restrict arg)

with:

err = pthread_create(&thready, NULL, &mythread, NULL);

Now the question is where the created thread gets it's PC value. How does the PC value set to the first line of mythread code?

+1 vote

I know the basics of pthread but looking for more information on uses of pthread_detach library function.

0 votes

Lot of confusion and lot of scattered info, can someone help me by providing to the point response.

Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...