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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:28:56+00:00 2026-06-15T03:28:56+00:00

I have this code My scope was: the program creates MAX_THREAD threads, three in

  • 0

I have this code

My scope was: the program creates MAX_THREAD threads, three in this case, each thread prints Thread-ID and exits.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>

#define MAX_THREAD 3

void *thr_func(void *arg);

int main(void) {
    pthread_t thr[MAX_THREAD];
    int i, thr_err;

    /* I expected three threads ... but there is only one */
    for (i=0; i<MAX_THREAD; i++) {

        printf("thread %d: - ", i);

        if ((thr_err = pthread_create(&thr[i],NULL, thr_func, NULL)) != 0) {
            fprintf(stderr, "Err. pthread_create() %s\n", strerror(thr_err));
            exit(EXIT_FAILURE);
        }

        if (pthread_join(thr[i], NULL) != 0) {
            fprintf(stderr, "Err. pthread_join() %s\n", strerror(errno));
            exit(EXIT_FAILURE);
        }
    }

    return(EXIT_SUCCESS);
}

void *thr_func(void *arg)
{
    pthread_t tid = pthread_self();
    printf("TID %lu - Address 0x%x\n", tid, (unsigned int)pthread_self());

    pthread_exit((void*)0);
}

output is:

thread 0: - TID 3075976048 - Address 0xb757ab70
thread 1: - TID 3075976048 - Address 0xb757ab70
thread 2: - TID 3075976048 - Address 0xb757ab70

I don’t understand why there is only one thread!

I have a doubt on this declaration:

 pthread_t thr[MAX_THREAD];

Can i create an array of three threads or this is only one thread ????

SOLVED

New code (I’ve just put pthread_joiun() outside of for loop)

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/syscall.h>

#define MAX_THREAD 3

void *thr_func(void *thr_num);

int main(void) {
    pthread_t thr[MAX_THREAD];
    int i, thr_err;

    for (i=0; i<MAX_THREAD; i++) {

        if ((thr_err = pthread_create(&thr[i],NULL, thr_func, (void*)i)) != 0) {
            fprintf(stderr, "Err. pthread_create() %s\n", strerror(thr_err));
            exit(EXIT_FAILURE);
        }
    }

    for (i=0; i<MAX_THREAD; i++) {
        if (pthread_join(thr[i], NULL) != 0) {
            fprintf(stderr, "Err. pthread_join() %s\n", strerror(errno));
            exit(EXIT_FAILURE);
        }
    }

    return(EXIT_SUCCESS);
}

void *thr_func(void *thr_num)
{
    pthread_t tid;

    if ((tid = syscall(SYS_gettid)) == -1) {
        fprintf(stderr, "Err. syscall() %s\n", strerror(errno));
        exit(EXIT_FAILURE);
    }

    printf("thread '%d' - TID %lu - Address 0x%x\n",
            (int)thr_num, tid, (unsigned int)tid);

    pthread_exit((void*)0);
}

Output is:

thread '1' - TID 8780 - Address 0x224c
thread '0' - TID 8779 - Address 0x224b
thread '2' - TID 8781 - Address 0x224d

Addresses and thread-ID now are different.

  • 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-15T03:28:58+00:00Added an answer on June 15, 2026 at 3:28 am

    You are likely getting the same TID and address precisely because you pthread_join() each thread before starting the next one. The pthreads library seems to be a bit lazy about reclaiming the associated data structures (probably for efficiency), so the next thread you spawn just uses the same data structures the previous one did. Try writing two loops, one to create the threads, then another one to do the pthread_join()s after all the threads have been created.

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

Sidebar

Related Questions

I have this code in Transaction.cs using (TransactionScope scope = new TransactionScope()) { //
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
I have the following code: //Comp454 program 2 #include <iostream> #include <string> #include <fstream>
In my program I have currently a piece of code that looks like this
I have this code <div id=main style=background:#aaaaaa;float:left;height:160px;margin:5px;position:relative;display:block;width:630px;> <div id=1 class=item style=background:#ffaacc;float:left;width:200px;height:150px;margin:5px;position:absolute;left:0px;top:0px;> </div> <div id=2
I have this code : void Main() { System.Timers.Timer t = new System.Timers.Timer (1000);
I have this code for changing the image of a button: - (void)mouseEntered:(NSEvent *)event
I have this code in XAML <Grid x:Name=LayoutRoot Background=Transparent> <Grid.RowDefinitions> <RowDefinition Height=Auto/> <RowDefinition Height=*/>
I have this code: EditText value = ( EditText )findViewById( R.id.editbox ); Integer int_value
I have this code: ie1.link(:text, /Exception:/) It is producing an error message which I

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.