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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T14:12:06+00:00 2026-05-13T14:12:06+00:00

Hey – I’m having an odd problem with a little toy program I’ve written,

  • 0

Hey – I’m having an odd problem with a little toy program I’ve written, to try out threads.
This is my code:

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

using std::cout;
using std::endl;

void *threadFunc(void *arg) {
    cout << "I am a thread. Hear me roar." << endl;

    pthread_exit(NULL);
}

int main() {
    cout << "Hello there." << endl;
    int returnValue;
    pthread_t myThread;

    returnValue = pthread_create(&myThread, NULL, threadFunc, NULL);

    if (returnValue != 0) {
        cout << "Couldn't create thread! Whoops." << endl;
        return -1;
    }

    return 0;
}

With the first cout in main not commented out, the thread prints fine.
However, without it, the thread doesn’t print anything at all.

Any help?

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

    Try this:

    #include <pthread.h>
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    void *threadFunc(void *arg) {
        cout << "I am a thread. Hear me roar." << endl;
    
        pthread_exit(NULL);
    }
    
    int main() {
        //cout << "Hello there." << endl;
        int returnValue;
        pthread_t myThread;
    
        returnValue = pthread_create(&myThread, NULL, threadFunc, NULL);
    
        if (returnValue != 0) {
            cout << "Couldn't create thread! Whoops." << endl;
            return -1;
        }
    
        pthread_join( myThread, NULL);
    
        return 0;
    }
    

    The difference between my code and yours is one line – pthread join. This suspends the main thread until the sub-thread has had chance to complete its actions.

    In your code, execution reaches the first cout and it’s processed. Then, you split off another thread and the main thread carries on until the end, which may or may not be reached before the secondary thread is tidied up. That’s where the odd behaviour comes in – what you are experiencing is the case where the main program finishes before the sub-thread has had a chance to, so the program has “returned” and the whole lot is cleaned up by the kernel.

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

Sidebar

Ask A Question

Stats

  • Questions 502k
  • Answers 502k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use array_diff print_r(array_diff($c, $A)); returns Array ( [2] => e… May 16, 2026 at 2:33 pm
  • Editorial Team
    Editorial Team added an answer I believe you can import from file system and point… May 16, 2026 at 2:33 pm
  • Editorial Team
    Editorial Team added an answer You have a couple options, but not with the ActionLink… May 16, 2026 at 2:32 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Hey all, my Computational Science course this semester is entirely in Java. I was
Hey, in the Programming Pearls book, there is a source code for setting, clearing
Hey having some trouble trying to maintain transparency on a png when i create
Hey all, I am not quite getting why this doesn't seem to be working.
Hey guys, I have a little button functionality that is not erring, but also
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey so what I want to do is snag the content for the first
Hey, I'm using Levenshteins algorithm to get distance between source and target string. also
Hey, I've been developing an application in the windows console with Java, and want
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my

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.