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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:48:53+00:00 2026-05-16T23:48:53+00:00

I have been working now for few days on a small C program which

  • 0

I have been working now for few days on a small C program which uses pthreads. I spent more or less all yesterday looking for a deadlock bug, but now I have found out that the problem is not really a deadlock bug. The following piece of code has exactly the same problem.

#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <unistd.h>
#define NTHREADS 507

pthread_mutex_t runningThreadsMutex;
pthread_cond_t runningThreadsCond;
int runningThreads = 0;

void* HelloWorld(void* arg) {
  sleep(1);

  pthread_mutex_lock(&runningThreadsMutex);
  runningThreads--;
  printf("End thread %d\n", runningThreads);
  pthread_cond_signal(&runningThreadsCond);
  pthread_mutex_unlock(&runningThreadsMutex);

  return NULL;
}

int main() {
  pthread_t thread;

  pthread_mutex_init(&runningThreadsMutex, NULL);
  pthread_cond_init(&runningThreadsCond, NULL);

  for (int i = 0; i < NTHREADS; ++i) {
    pthread_mutex_lock(&runningThreadsMutex);
    printf("Create thread %d\n", runningThreads++);
    pthread_mutex_unlock(&runningThreadsMutex);
    pthread_create(&thread, NULL, HelloWorld, NULL);
  //  pthread_detach(thread);
  }

  pthread_mutex_lock(&runningThreadsMutex);
  while(runningThreads > 0) {
    pthread_cond_wait(&runningThreadsCond, &runningThreadsMutex);
  }
  pthread_mutex_unlock(&runningThreadsMutex);
  return 0;
}

The code above seems to work nicely on my laptop (64 bit linux machine) for NTHREADS < 506. In that case it prints out something like this:

Create thread 0
Create thread 1
.
.
.
Create thread 505
End thread 505
End thread 504
.
.
.
End thread 0

And terminates as it should. However if I use NTHREADS >= 506, e.g. NTHREADS = 510 I get

Create thread 0
Create thread 1
.
.
.
Create thread 509
End thread 509
End thread 508
.
.
.
End thread 4

where it halts without ever terminating. So it seems like the last four (510-506=4) threads never terminate (or never start at all?).

I tried this code as well on an old 32 bit linux machine. There I get the same behaviour, except that it works well for NTHREADS < 382 but not for NTHREADS >= 382 (instead of 506).

When I googled for a solution I also found this question: http://bytes.com/topic/c/answers/728087-pthreads-limit, where someone is having the same problem when using pthread_join (which might be more natural when working with pthreads) but they don’t give any good explanation.

Can anyone explain to me what I am doing wrong and what is the fundamental problem with this code? I guess this must be some kind of limit on the number of allowed threads, but how should I deal with it?

  • 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-16T23:48:54+00:00Added an answer on May 16, 2026 at 11:48 pm

    You need to check the return value for pthread_create. If it is non-zero then the function failed to create the thread. A typical problem will be running out of memory for the stacks for the new threads. e.g. with a 1Mb stack per thread the system will need at least 510Mb of free memory to start 510 threads.

    Why are you running so many threads? Unless you have a massively parallel system with hundreds of processors then these threads will just compete for CPU time and other resources. You are probably better off with fewer threads (of the same order of magnitude as the number of processors in your system) that do the work in the most appropriate order.

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

Sidebar

Related Questions

So I have been working on this for a few days now, and I
I have been working on this issue for a few days now and can't
I have been working on a project for a few days now and have
I have been working on this frustrating bug for a few days now and
I have been working on this for a few days now and I have
I have been working with Zend for a few months now and am at
I have been working with Zend Framework for a few months now. I have
I have been working on this problem for 2 days now and it's an
I have been struggling with this problem for a few days now and I
I have been working with ASP.NET for a few years and am now working

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.