Explain Atomic Instructions and TSL test and set lock

does anyone knows a clear explanation about Atomic Instructions and TSL test and set lock, I mean theorical explanation of this two concepts that i can have a better understanding with what is going on

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


a short explantion of atomic is..."from the point of view of other transactions, it either happens completely or not at all."

So basically you are not allowed to do only "half" a transaction... Like Crediting my checking account but forgetting to deduct the money from your account.. Both actions need to happen. It cant be that you credit my account and then figure out you dont have the balance to transfer the money to my account and abort then. (I prefer this example since here only the bank looses money and i get a few bucks extra ;) )

jsctechyAuthor Commented:
thanks for that clear explanation about atomic that really make a bit of sense to me better than having tons of websites telling me source codes where i can follow what are they trying to achive. Now there is an easy explanation for TSL

Thanks for that comment


Atomic is only one part of a transaction. An transaction needs to follow the acronym "ACID" ACID stands for "atomicity, consistency, isolation, and durability"

The other words make sense i hope if not ask again :)=

TSL is a instruction (usually implemented on some CPU:s) that is used to implement the critical section problem (critical sections are used to implement atomic actions, in turn). What it does is, it sets a variable to the value you pass to it, and returns the old one you replaced - both atomically. TSL means Test and Set Lock. Imagine you have two threads that you want synchronized, meaning that you donĀ“t want a piece of code in both of them to be able to run at once:

Thread1 {

 int currentMoney = get amount of money in account...


 set account balance to currentMoney - spentMoney;

Thread2 {
 int currentMoney = get amount of money in account


 set account balance to currentMoney + earnedMoney;

Now imagine that Thread1 stops where the three dots are (...) and Thread2 runs, and completes. Then thread1 still has the OLD value of the account balance, and will update the balance to what it remembered, causing a nasty bug and either you or the bank will lose money without a trace.

What you do in a situation like this, is you encluse the important code in critical sections, making the critical sections totally atomic in regard to eachother. This is done using mutual exclusion and lock variables, this is where TSL is used. A CSEntry function could look like: (CSEntry = Critical section entry):

CSEntry(lockVar) {

 while(TSL(lockVar) == 1) sleep();

And equally simple, CSEntry:

CSEntry() {

 lockvar = 0;

Hope this helps, and good luck ;)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.