Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7039817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:50:38+00:00 2026-05-28T01:50:38+00:00

I want to get the stack address of a thread through some function to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:50:39+00:00Added an answer on May 28, 2026 at 1:50 am

    First get the bottom of the stack and give read/write permission to it with the following code.

    pthread_attr_t attr;
    void * stackaddr;
    int * plocal_var;
    size_t stacksize;
    
    pthread_getattr_np(pthread_self(), &attr);
    pthread_attr_getstack( &attr, &stackaddr, &stacksize );
    
    printf( "stackaddr = %p, stacksize = %d\n", stackaddr, stacksize );
    
    plocal_var = (int*)mmap( stackaddr, 4096, PROT_READ | PROT_WRITE, 
              MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0 );
    // Now try to write something 
    *plocal_var = 4;
    

    and then you can get the thread ID, with the function get_thread_id() shown below. Note that calling mmap with size 4096 has the effect of pushing the boundary of the stack by 4096, that is why we subtract 4096 when getting the local variable address.

    int get_thread_id()
    {
        pthread_attr_t attr;
        char * stackaddr;
        int * plocal_var;
        size_t stacksize;
    
        pthread_getattr_np(pthread_self(), &attr);
        pthread_attr_getstack( &attr, (void**)&stackaddr, &stacksize );
    
        //printf( "stackaddr = %p, stacksize = %d\n", stackaddr, stacksize );
    
        plocal_var = (int*)(stackaddr - 4096);
    
        return *plocal_var;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to get a better understanding of what a stack is in
I want to alter the stack of a newly created remote thread, but upon
How do i get the address of the variable holding an event handler? e.g.
For some reason, I create my own stacks for all the threads in my
I have a linker command file that assigns the top address of the stack
I wrote a program which solves some kind of optimization problem. I have been
I want to get the size of the phone drive. I'm using Nokia-PC-Connectivity., and
I think I'm failing to understand some finer point of C++. I want to
I'm working on a lock free stack and queue data stracture where I can
i want to send mail through iphone sdk i know there are lots of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.