Thread-Local Storage TLS

Source: Internet
Author: User

The C + + runtime provides TLS (thread-local storage), which can be associated with the executing thread when multithreading is not yet produced. The Strtok () function is a good example. Along with it are strtok_s (), _tcstok_s (), and so on, in fact _tcs is another form of WCS, representing the wide character store, _s is a security function defined by Microsoft, usually more than one parameter of the normal function. Take _tcstok_s () as an example,

int main (int argc, char* argv[])
{

wchar_t source[] = L "192.168.255.255";
wchar_t doc[] = L ".";
wchar_t* Next_token;
wchar_t* Dest = _tcstok_s (Source, Doc, &next_token); strtok_s
int i = 1;
while (Dest! = NULL)
{
printf ("dest[%d]:%s\r\n", I, Dest);
Dest = _tcstok_s (NULL, Doc, &next_token);
i++;
}
return 0;
}

If the prompt function is not known, add the header file #include <tchar.h>

The code is simple, with the source string "." Separates the separators and outputs them. Note When you call the _tcstok_s function the second time, the first argument is null, because the string is saved in its own static variable the first time it is called, and the saved address can be referenced later.

But in multithreaded programming, the first thread calls _tcstok_s, and another thread may call it before it is called again. This causes the content to be overwritten for the first time. This will use the contents of TLS.

1. Static TLS

#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace Std;

__declspec (thread) int value = 0;

DWORD WINAPI ThreadProc (LPVOID Param);

int main (int argc, char* argv[])
{
Value = 1;
HANDLE threadhandle = CreateThread (null, 0, threadproc, NULL, 0, NULL);

WaitForSingleObject (Threadhandle, INFINITE);

cout << "main" << value << Endl;
return 0;
}

DWORD WINAPI ThreadProc (LPVOID Param)
{
Value = 10;
cout << value << Endl;
return 0;
}

__declspec (thread) This modifier tells the compiler to place this value in the. TLS segment and, if not written, in the. Data section.

Results:

Defines the __declspec (thread) output of 1 main

Undefined __declspec (thread) output 10 main

2. Dynamic TLS

#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace Std;

int __index[3] = {0};
Char Name[3][20] = {{"qwe"},{"ASD"},{"Zxc"}};
DWORD WINAPI ThreadProc (LPVOID Param);

int main (int argc, char* argv[])
{
int i = 0;
DWORD Threadid[3] = {0};
HANDLE Threadhandle[3] = {0};
for (; i < 3; i++)
{
__index[i] = TlsAlloc (); To retrieve a bit flag, to find a free flag, is actually a predetermined index
if (__index[i] <= tls_minimum_available)
{
Threadhandle[i] = CreateThread (NULL, 0, ThreadProc, (LPVOID) (Name[i]), 0, &threadid[i]);
}
}
WaitForMultipleObjects (3, Threadhandle, TRUE, INFINITE);
return 0;
}

DWORD WINAPI ThreadProc (LPVOID Param)
{
char* key = new CHAR[20];
memcpy (Key, (char*) Param, 20);
TlsSetValue (__index[0], key); Associating a value with an index, note that when Microsoft implements it, it sacrifices error checking, even if the wrong index is assigned
for (int i = 0; i <; i++)
{
Sleep (1);
printf ("%s\r\n", (char*) TlsGetValue (__index[0])); Returns the value in the index

}

Free (key);
return 0;
}

In general, these two types of TLS are more useful when creating DLLs.

Thread-Local Storage TLS

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.