Skip to content

Instantly share code, notes, and snippets.

@tejainece
Last active January 3, 2016 17:49
Show Gist options
  • Save tejainece/8497878 to your computer and use it in GitHub Desktop.
Save tejainece/8497878 to your computer and use it in GitHub Desktop.
Do threads have their own stack and heap?
Main thread info
Stack variable address : 0x7fff70649a4c
main function address : 0x4006dd
printInfo function address : 0x4007b5
Global variable address : 0x601058
Static variable address : 0x601060
Thread 2 thread info
Stack variable address : 0x7f730be3be6c
main function address : 0x4006dd
printInfo function address : 0x4007b5
Global variable address : 0x601058
Static variable address : 0x601060
Thread 1 thread info
Stack variable address : 0x7f730c63ce6c
main function address : 0x4006dd
printInfo function address : 0x4007b5
Global variable address : 0x601058
Static variable address : 0x601060
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int g_testvar1 = 10;
void *print_message_function( void *ptr );
void printInfo(char *);
main()
{
pthread_t thread1, thread2;
const char *message1 = "Thread 1";
const char *message2 = "Thread 2";
int iret1, iret2;
printInfo("Main thread");
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
exit(0);
}
void *print_message_function( void *ptr )
{
char *tname = (char *) ptr;
printInfo(tname);
}
void printInfo(char *tname) {
int l_testvar1 = 0;
static int l_testvar2;
printf("\n%s info\n", tname);
printf("Stack variable address : %p\n", &l_testvar1);
printf("main function address : %p\n", main);
printf("printInfo function address : %p\n", printInfo);
printf("Global variable address : %p\n", &g_testvar1);
printf("Static variable address : %p\n", &l_testvar2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment