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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:57:57+00:00 2026-05-23T14:57:57+00:00

I wrote a small program to see the amount of overhead there is to

  • 0

I wrote a small program to see the amount of overhead there is to create a thread.

Here is the program (I wrote it quickly so it isn’t the best):

#include <iostream>
#include <pthread.h>

void * lala(void * cake) {
 int * hi = (int *)cake;
 std::cout << *hi << '\n';
}

int main(void) {
 pthread_t thread;
 for (int i = 0;i < 10000;i = i + 1) {
  pthread_create(&thread,0,lala,&i);
 }
}

It basically launches 10000 threads and passes them their thread number, and each thread outputs its number.

The output changes each time I run the program, however I noticed that there was one part that never changed:

At the end of the output, I always find this:

...
9994
9995
9996
9997
9998
9999
0

Which means that the first thread finishes last…

So, my friends, does anyone have a possible explanation for this phenomenon?

  • 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-23T14:57:57+00:00Added an answer on May 23, 2026 at 2:57 pm

    First thing, you are passing the address-of a local variable to the thread, which is constantly changing. So, when the thread gets time to read it, the content of i would already be changed.
    Why can’t you just pass i instead of &i, where variable is just of 4-bytes (i.e. fits in pointer)?

    Secondly, you shouldn’t be concerned how OS schedules your threads.

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

Sidebar

Related Questions

I wrote this small C++ program and built it(Release) #include<iostream> int main(){ std::cout<<Hello World;
I wrote this small C program: #include <stdio.h> #include <stdlib.h> int main() { FILE
I wrote a small program, that creates files at an interval of 1 minute.
I wrote a small program to test accessing a widget parent's slot. Basically, it
So I wrote a project-management program for a small business using Microsoft Access 2007.
Hey there. I'm trying to write a small program that will read the four
I wrote a small program using a custom indexOf function but wanted to dismiss
I wrote a small program to test GCC's options. int main() { int a=0;
I wrote a small program to find how the exit() function works in Linux.
I wrote a small program with Boost in c++. It works fine and so

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.