If I compile and run the code below
1 #include <iostream> 2 #include <pthread.h> 3 #include <cstdio> 4 #include <cstdlib> 5 6 #define NTHREADS 4 7 #define N 100 8 #define MEGEXTRA 1000000 9 10 using namespace std; 11 12 pthread_attr_t attr; 13 14 void *doWork (void *threadid) { 15 double A[N][N]; 16 int tid = *(reinterpret_cast<int *>(threadid)); 17 size_t myStackSize; 18 pthread_attr_getstacksize (&attr, &myStackSize); 19 cout << "Thread " << tid << ": stack size = " << myStackSize << " bytes" << endl; 20 21 for (int i = 0; i < N; i++) { 22 for (int j = 0; j < N; j++) { 23 A[i][j] = ((i * j) / 3.452) + (N - 1); 24 } 25 } 26 27 pthread_exit (NULL); 28 } 29 30 int main () { 31 pthread_t threads[NTHREADS]; 32 size_t stackSize; 33 34 pthread_attr_init (&attr); 35 pthread_attr_getstacksize (&attr, &stackSize); 36 37 cout << "Default stack size = " << static_cast<long>(stackSize) << endl; 38 39 stackSize = sizeof(double) * N * N + MEGEXTRA; 40 cout << "Amount of stack needed per thread = " << static_cast<long>(stackSize) << endl; 41 42 pthread_attr_setstacksize (&attr, stackSize); 43 cout << "Creating threads with stack size = " << static_cast<long>(stackSize) << endl; 44 45 int i[NTHREADS]; 46 for (int j = 0; j < NTHREADS; j++) { 47 sleep(1); 48 i[j] = j; 49 50 int rc = pthread_create(&threads[j], &attr, doWork, reinterpret_cast<void *>(&i[j])); 51 if (rc) { 52 cout << "Error Code: " << rc << endl; 53 exit (-1); 54 } 55 } 56 57 cout << "Created " << NTHREADS << " threads" << endl; 58 pthread_exit(NULL); 59 }
I get the following output:
Default stack size = 8388608 Amount of stack needed per thread = 1080000 Creating threads with stack size = 1080000 Thread 0: stack size = 1080000 bytes Thread 1: stack size = 1080000 bytes Thread 2: stack size = 1080000 bytes Created 4 threads Thread 3: stack size = 1080000 bytes
but if I comment on a dream (1); on line 47, I get the following output
Default stack size = 8388608 Amount of stack needed per thread = 1080000 Creating threads with stack size = 1080000 Created 4 threads Thread 3: stack size = 1080000 bytes Thread 2: stack size = 1080000 bytes Thread 1: stack size = 1080000 bytes Thread 9251904: stack size = 1080000 bytes
Can anyone explain what is happening? Why am I getting the wrong sleep conclusion (1)?
this is what i use to compile code
g++ -Wall -Wextra -O2 -ggdb -pthread 5.cpp -o 5
source share