What code is thread-safe?

Source: Internet
Author: User

Transferred from: http://www.cnblogs.com/lidabo/archive/2013/04/10/3011518.html

For multi-threaded programming, a lot of people are unclear, write code when the lock is everywhere, the impact of performance does not say, but also easily inexplicable deadlock, there are people on the multi-threaded distance.

So learning multithreaded programming is not the most important to learn the API, but to understand what is multithreaded security code

From the example,

#include <windows.h>#include<process.h>LongGlobal1 =0;volatile LongGlobal2 =0;classmyclass{ Public: MyClass (): M (0)    {        ++L; }    intFunintv) {returnM+v;//-----------9    }    void Set(intv) {m= V;//-------------Ten    }    intm;}; MyClass Global_object; //-------------8unsignedint__stdcall Thread_fun1 (void*param) {    Static intStatic2 =0; StaticMyClass Static_object;//--------6    intLocal1 =0; ++local1;//-------1++static2;//-------2++global1;//-------3++global2;//-------4InterlockedIncrement (&AMP;GLOBAL1);//--------5Local1= Global_object.fun (LOCAL1);//----------7Global_object.Set(LOCAL1);//---------------    return 0;} unsignedint__stdcall Thread_fun2 (void*param) {    ++global1;//-------3++global2;//-------4InterlockedIncrement (&AMP;GLOBAL1);//--------5Global_object.Set(1);//-----------    return 0;}intMain () {HANDLE thread1= (HANDLE) _beginthreadex (0,0, &AMP;THREAD_FUN1,0,0,0);//Thread 1HANDLE thread2 = (HANDLE) _beginthreadex (0,0, &AMP;THREAD_FUN1,0,0,0);//Thread 2HANDLE thread3 = (HANDLE) _beginthreadex (0,0, &thread_fun2,0,0,0);//Thread 3WaitForSingleObject (Thread1,infinite);    WaitForSingleObject (Thread2,infinite);        WaitForSingleObject (Thread3,infinite); return 0;}

1. Local variables are safe for local use
Why? Because each thread has its own run stack, and the local variable is living in the stack, everyone does not interfere.
So code 1
int local1;
++local1;
It's safe.

2. Global native variable multithreading read-write is not secure
Global variables are in the heap
Long global1 = 0;
++global2;
+ + This operation is actually divided into two parts, one is to read, the other is to write
MOV Ecx,global
Add ecx,1
MOV global,ecx
So code 3 is not safe.

3. Function static variable multithreading read-write is also unsafe
2 of the same reason
So it's not safe at Code 2.

4.volatile can ensure that global shaping variables are multithreaded security?
No.
Volatile is merely a warning to compiler not to optimize this variable, but to take a value from memory every time, not from the register
So code 4 is not safe either.

5.InterlockedIncrement guaranteed atomicity of integer variable self-increment
So code 5 is safe.

is the initialization of a 6.function static object A multithreaded security?
No.
The famous Meyer Singleton is actually not thread-safe
Object & GetInstance ()
{
Static Object o;
return o;
}
May cause multiple initialization of the object
So code 6 is not safe.

7. On 32 machines, 4-byte shaping once assign is atomic
Like what
i = 10; Thread1
i=4; Thread2
does not cause the value of I to be in an unknown state, either 10 or 4


The key to writing a multithreaded security is to encapsulate it so that the data is protected from being accessed
Security:
Local variables > member variables > Global variables

What code is thread-safe?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.