How to store thread-specific data in c++ globally?

Member

by anissa , in category: General Help , 7 months ago

How to store thread-specific data in c++ globally?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by coty_beier , 7 months ago

@anissa 

In C++, you can use the thread_local keyword to declare thread-specific variables. Thread-local variables have their own separate storage for each thread, and their values are not shared between threads. Below is an example of how to store thread-specific data globally:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <thread>

// Global thread-local variable
thread_local int g_data;

// Function to print the thread-specific data
void printData() {
    std::cout << "Thread " << std::this_thread::get_id() << ": " << g_data << std::endl;
}

// Thread function
void threadFunction() {
    // Set thread-specific data
    g_data = std::this_thread::get_id();
  
    // Print thread-specific data
    printData();
}

int main() {
    // Create multiple threads
    std::thread t1(threadFunction);
    std::thread t2(threadFunction);
  
    // Wait for the threads to finish
    t1.join();
    t2.join();
  
    return 0;
}


In this example, the g_data variable is declared as thread_local, which means that each thread will have its own separate copy of the variable. The threadFunction sets the thread-specific data by assigning the current thread's ID to g_data and then calls the printData function to print the stored data.


When you run the example, you will see that each thread prints its own ID, which confirms that each thread has its own separate copy of g_data.


Note that the thread_local keyword is only available in C++11 and later versions. If you are working with an earlier version of C++, you may need to use platform-specific APIs or libraries to achieve the same functionality.

by margarett , 3 months ago

@anissa 

It's worth mentioning that in C++11, the thread_local keyword was added to provide a standard and portable way to define thread-local storage. However, if you are working with a compiler that doesn't support C++11 or later, you can use platform-specific APIs to achieve thread-specific data storage.


For example, on Windows operating systems, you can use the TlsAlloc, TlsGetValue, and TlsSetValue functions to manage thread-local storage. Here's an example of how you can use these functions to store thread-specific data globally:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <windows.h>

// Global index for thread-local storage
DWORD g_dataIndex = TlsAlloc();

void printData() {
    std::cout << "Thread " << GetCurrentThreadId() << ": " << TlsGetValue(g_dataIndex) << std::endl;
}

void threadFunction() {
    // Set thread-specific data
    TlsSetValue(g_dataIndex, static_cast<LPVOID>(reinterpret_cast<DWORD64>(GetCurrentThreadId())));

    // Print thread-specific data
    printData();
}

int main() {
    // Create multiple threads
    HANDLE t1 = CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)threadFunction, nullptr, 0, nullptr);
    HANDLE t2 = CreateThread(nullptr, 0, (LPTHREAD_START_ROUTINE)threadFunction, nullptr, 0, nullptr);

    // Wait for the threads to finish
    WaitForSingleObject(t1, INFINITE);
    WaitForSingleObject(t2, INFINITE);

    // Clean up
    CloseHandle(t1);
    CloseHandle(t2);

    // Free the thread-local storage index
    TlsFree(g_dataIndex);

    return 0;
}


In this example, we use the Windows API functions TlsAlloc, TlsGetValue, and TlsSetValue to manage thread-local storage. Each thread has its own copy of the data stored at the index returned by TlsAlloc, and this data can be accessed and modified by the thread-specific TlsGetValue and TlsSetValue functions.


Remember that if you are targeting multiple platforms or need your code to be portable, using C++11's thread_local keyword is the recommended approach. However, for platform-specific implementations, using the relevant APIs provided by the platform is necessary.