Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get a thread's stack address from pthread_self()

I want to get the stack address of a thread through some function to which we can pass pthread_self(). Is it possible? The reason I am doing this is because I want to write my own assigned thread identifier for a thread somewhere in its stack. I can write near the end of the stack (end of the stack memory and not the current stack address. We can ofcourse expect the application to not get to the bottom of the stack and therefore use space from there).

In other words, I want to use the thread stack for putting a kind of thread local variable there. So, do we have some function like the following provided by pthread?

stack_address = stack_address_for_thread( pthread_self() );

I can use the syntax for thread local variables by gcc for this purpose, but I'm in a situation where I can't use them.

like image 251
MetallicPriest Avatar asked Jan 17 '23 21:01

MetallicPriest


1 Answers

Probably it's better to use pthread_key_create and pthread_key_getspecific and let the implementation worry about those details.

A good example of usage is here:

pthread_key_create

Edit: I should clarify -- I'm suggesting you use the libpthread provided method of creating thread-local information, instead of rolling your own by pushing something onto the end of the stack where it's possible your information could be lost.

like image 127
tdenniston Avatar answered Jan 30 '23 07:01

tdenniston