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

  • SEARCH
  • Home
  • 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 6932241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:40:51+00:00 2026-05-27T11:40:51+00:00

I am trying to understand how stack allocation and alignment works with pthreads on

  • 0

I am trying to understand how stack allocation and alignment works with pthreads on a Linux x86_64 system with gcc and what data the system stores on the stack. I know that you can configure the stack memory using pthread_attr_setstack. I’ve done this in a test program that does the following:

1) recursively calls itself and updates an uninitialized array that is allocated on the stack

2) prints out the value of the first array element, the last element, and rsp

From this I’ve been able to observe how rsp gets incremented (in my test program I noticed some of the recursive calls were inlined by the compiler). I’ve also been able to see that adding TLS memory (with __thread variables) causes the first value of rsp to be lower. So it looks like TLS variables are allocated on the top of the stack.

However, what I’m not sure about is what else is there. It looks to me like the first page of the stack is reserved for the system in some way because none of the stack variables that I allocate end up in that region. Even if I don’t use any __thread variables the variables that I do instantiate do not appear to be allocated in the first page (I set the stack memory so that it is page aligned).

So my question is: what else, if anything, is on the stack for a pthread besides TLS data and stack variables?

  • 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-27T11:40:52+00:00Added an answer on May 27, 2026 at 11:40 am

    On Linux NPTL:

    The very top of the stack contains the TCB. This is also known as struct pthread or pthread_t. It’s a bit hairy due to all the weird glibc system-specific defines, but basically it contains things like:

    • The TLS header (this part does not include actual TLS values)
    • Various flags
    • A linked list for tracking cached stacks
    • TID (and stack in-use flag)
    • PID
    • Robust mutex tracking information
    • pthread_cleanup and stack unwind information
    • Cancellation state
    • Even more flags
    • A certain number of TLS slots used by pthread_setspecific – if you exceed this number, the rest are allocated on the heap
    • Various locks
    • Tracking information for pthread_join
    • The thread’s return value
    • Scheduling policies (from the thread attributes)
    • The start routine and thread argument
    • Stack size tracking data
    • etc etc

    This is mostly initialized in pthread_create (__pthread_create_2_1 in nptl/pthread_create.c if you’re following along in the eglibc source), before the new thread actually starts running.

    Below the TCB is statically allocated __thread variables – or, at least, those the linker could identify at startup time. The dynamic linker initializes a l_tls_offset field in the linker map to tell the NPTL code how much space to reserve. Note that libraries loaded after program startup won’t be part of this – see the __thread ABI spec for details.

    Below the __thread variables is the stack. The top of this stack is the start_thread() code, so it’ll still be a ways before it actually executes user code (but not too much further).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to understand how the stack works, so I've decided teach myself
Just trying to understand that - I have never used it before. How is
I've been trying to understand how Ruby blocks work, and to do that I've
I'm trying to understand the space requirements for a Mergesort, O(n). I see that
Dear SQL Gurus from Stack Overflow: Environment: Oracle I'm trying to understand why I
Good day everyone! I am trying to understand how buffer overflow works. Right now,
I am trying to better understand how the stack and heap work. I have
I'm trying to understand and XPath that was sent to me for use with
I am trying to understand low-level memory manager in C especially Stack. As I
I'm using Core Data in my first iPhone application and trying to understand NSFetchedResultsController.

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.