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 8526849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:23:24+00:00 2026-06-11T08:23:24+00:00

I am learning pthread, the following is my simple code: 1 #include <pthread.h> 2

  • 0

I am learning pthread, the following is my simple code:

  1 #include <pthread.h>
  2 #include <cstdlib>
  3 #include <iostream>
  4 #include <string>
  5 #include <sstream>
  6
  7 using namespace std;
  8
  9 struct ThreadParam
 10 {
 11     int thread_id;
 12     string message;
 13 };
 14
 15 string int2string(int i)
 16 {
 17     stringstream s;
 18     s << i;
 19     return s.str();
 20 }
 21
 22 void * Hello(void * param)
 23 {
 24     ThreadParam * threadParam = (ThreadParam*)param;
 25
 26     int tid = threadParam->thread_id;
 27     string msg = threadParam->message;
 28     string output = msg + " In thread " + int2string(tid);
 29
 30     cout << output << endl;
 31     pthread_exit(NULL);
 32 }
 33
 34 int main()
 35 {
 36     const int count = 30;
 37     ThreadParam params[count];
 38
 39     pthread_t threads[count];
 40     int rc;
 41     for(int t=0; t < count; t++)
 42     {
 43         cout << "In main: creating thread " << t << endl;
 44
 45         params[t].thread_id = t;
 46         params[t].message = "Hello " + int2string(t) + "!";
 47
 48         rc = pthread_create(&threads[t], NULL, Hello, (void*)&params[t]);
 49
 50         if(rc)
 51         {
 52             cout << "Error! Return code is " << rc << endl << flush;
 53             exit(-1);
 54         }
 55     }
 56     pthread_exit(NULL);
 57 }

I got segmentation fault after creating the last thread. The output is:

In main: creating thread 0
In main: creating thread 1
In main: creating thread 2
In main: creating thread 3
In main: creating thread 4
In main: creating thread 5
In main: creating thread 6
In main: creating thread 7
In main: creating thread 8
In main: creating thread 9
In main: creating thread 10
In main: creating thread 11
Hello 0! In thread 0
Hello 1! In thread 1
Hello 2! In thread 2
Hello 3! In thread 3
Hello 4! In thread 4
In main: creating thread 12
In main: creating thread 13
In main: creating thread 14
In main: creating thread 15
In main: creating thread 16
In main: creating thread 17
In main: creating thread 18
In main: creating thread 19
In main: creating thread 20
In main: creating thread 21
In main: creating thread 22
In main: creating thread 23
In main: creating thread 24
In main: creating thread 25
In main: creating thread 26
In main: creating thread 27
In main: creating thread 28
In main: creating thread 29
Segmentation fault

I cannot figure out why, any clue?

  • 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-06-11T08:23:25+00:00Added an answer on June 11, 2026 at 8:23 am

    Looking at your code I can tell that after you create the last thread you destroy the main one (the “creator” thread). This leads to the stack of that thread to be freed. But because you use pointers to the main thread stack memory (ThreadParam params[count]; to be precise; you pass pointers to it to pthread_create function) you then try to read the invalid data in other threads. The memory pages allocated for the main thread stack were marked as “unused” or “unallocated” after the destruction of the main thread so you get the segfault while trying to access them.

    UPD: As others said, you should use pthread_join function to wait for all threads to finish and then destroy the main thread.

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

Sidebar

Related Questions

Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I am learning about function pointers and thread in C. The following code will
Learning Objective-C and reading sample code, I notice that objects are usually created using
Learning some VBA. So far, I've constructed this piece of code which should allow
I have just started learning pthreads API and I am following the tutorial here
I was trying to implement read/write lock using mutex only (just for learning). Just
Learning jquery, so please be kind :) Using PHP, I have a table with
Learning CasperJS Trying to understand why the following is not displaying my results in
Learning to use Ruby Threads for transportability of code between different OS platforms. The
learning about loops (still a beginner) in VB.net. I have got the below code

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.