yesraaj
yesraaj

Reputation: 47980

Thread local storage used anywhere else?

Is thread local storage used anywhere else other than making global and static variables local to a thread?Is it useful in any new code that we write?

Upvotes: 2

Views: 545

Answers (4)

Matt Joiner
Matt Joiner

Reputation: 118660

I've only needed it for thread-specific error handling, and optimization (in C):

__thread int cpfs_errno;
static __thread struct Cpfs *g_cpfs;

In this example, this saves me passing a context pointer of struct Cpfs * through dozens of functions in which it never changes.

Upvotes: 0

Shailesh Kumar
Shailesh Kumar

Reputation: 6967

Thread specific singleton objects? A multi-threaded web server where each thread is handling one request, there is quite a good amount of possibility of some TLS data (like request URL or some database connections, essentially some resources intended to be used at any point during request handling if required) so that they can be easily accessed anywhere in the code when required.

Upvotes: 1

T.E.D.
T.E.D.

Reputation: 44814

These days errno is typically put in thread-local storage.

There are some situations (eg: shared libraries like DLLs that require startup code) where using thread-local storage can be a problem.

Upvotes: 0

Charles Salvia
Charles Salvia

Reputation: 53329

TLS can certainly be useful in new code. If you ever want a global variable which needs to be specific to each thread, (like errno in C/C++), thread-local-storage is the way to go.

Upvotes: 4

Related Questions