[Archaeological stickers] Thread Local Storage Example 2

Source: Internet
Author: User
The following descriptions and examples are excerpted from the Internet, but the content has been sorted and edited.

"What is the function of Thread Local Storage? It is mainly used to avoid conflicts caused by multiple threads accessing the same global variable or static variable at the same time, especially when multiple threads need to modify this variable at the same time. To solve this problem, we can use the TLS mechanism to provide a copy of the variable value for every thread that uses the global variable. Each thread can change its own copy independently, it does not conflict with copies of other threads. From the thread perspective, it seems that every thread has the variable completely. From the perspective of global variables, it is like a global variable is cloned into multiple copies, and each copy can be changed independently by a thread ."

"TLS can also be used for thread synchronization, which can be used to solve access conflicts to the same variable in multiple threads. In a common synchronization mechanism, multiple threads use object locking to securely access the same variable. At this time, the variable is shared by multiple threads. Using this synchronization mechanism, you need to carefully analyze when to read and write the variable and when to lock an object, there are many locks for releasing this object. All this is because multiple threads share resources, and the addition of synchronization locks is also a bottleneck that may affect performance. TLS solves multi-thread concurrent access from another perspective. It maintains a copy of the variable bound to the thread for each thread, thus isolating data from multiple threads, every thread has its own copy of the variable, so there is no need to synchronize the variable. TLS provides thread-safe shared objects.CodeThe entire unsafe variable can be encapsulated into TLS, or the thread-specific state of the object can be encapsulated into TLS ."

Example 1: Use Win32 API to implement TLS Use Win32 API to implement TLS
# Include " Stdafx. h "
# Include " Windows. h "
# Include " Iostream "

Using   Namespace STD;

Typedef Struct   {
DWORD dwfrequency;
DWORD dwduration;
} Soundparams;

DWORD g_ntlsindex; // !!

Void Paramsalloc ()
{
Soundparams * Params ;
Params = (Soundparams * ) New Soundparams;

Params -> Dwfrequency = Gettickcount () &   0x00000fff ;
Params -> Dwduration = 100 ;

Cout < " Usage frequency: " < Params -> Dwfrequency < " \ T latency: " < Params -> Dwduration < Endl;

Tlssetvalue (g_ntlsindex, Params ); // !!
}

Void Paramsfree ()
{
Soundparams*Params;
Params=(Soundparams*) Tlsgetvalue (g_ntlsindex );//!!

DeleteParams;
}

Void Tobeep ( Void )
{< br> soundparams * Params ;
Params = (soundparams * ) tlsgetvalue (g_ntlsindex); /// !!
:: beep ( Params -> dwfrequency, Params -> dwduration );
}

Void Soundthread ()
{
Paramsalloc ();

For (DWORD I = 0 ; I < 8 ; I ++ ) {
Tobeep ();
Sleep (1000);
}

Paramsfree ();
}

Int _ Tmain ( Int Argc, _ tchar * Argv [])
{
Handle soundhandles [ 3 ];
DWORD dwthreadid;
DWORD dwcount;

G_ntlsindex = Tlsalloc (); // !!

For (Dwcount = 0 ; Dwcount < 3 ; Dwcount ++ ) {< br> soundhandles [dwcount] = createthread (null, 0 ,
(lpthread_start_routine) soundthread, 0 , 0 ,< br> & dwthreadid );
sleep ( 1500 );
}

Waitformultipleobjects ( 3 , Soundhandles, true, infinite );

Tlsfree (g_ntlsindex ); // !!

Return   0 ;
}

Example 2: Use the compiler to support TLSImplement TLS with compiler support
# Include " Stdafx. h "
# Include " Windows. h "
# Include " Iostream "

Using   Namespace STD;

Typedef Struct   {
DWORD dwfrequency;
DWORD dwduration;
} Soundparams;

_ Declspec (thread) soundparams * G_pparams; // !!

Void Soundthread ()
{
G_pparams = (Soundparams * ) New Soundparams;
G_pparams -> Dwfrequency = Gettickcount () &   0x00000fff ;
G_pparams -> Dwduration = 100 ;

Cout < " Usage frequency: " < G_pparams -> Dwfrequency < " \ T latency: " < G_pparams -> Dwduration < Endl;

For (DWORD I = 0 ; I < 8 ; I ++ ) {
: Beep (g_pparams->Dwfrequency, g_pparams->Dwduration );
Sleep (1000);
}

Delete g_pparams;
}

Int _ Tmain ( Int Argc, _ tchar * Argv [])
{
Handle soundhandles [ 3 ];
DWORD dwthreadid;
DWORD dwcount;

For (Dwcount = 0 ; Dwcount < 3 ; Dwcount ++ ) {< br> soundhandles [dwcount] = createthread (null, 0 ,
(lpthread_start_routine) soundthread, 0 , 0 ,< br> & dwthreadid );
sleep ( 1500 );
}

Waitformultipleobjects ( 3 , Soundhandles, true, infinite );

Return   0 ;
}

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.