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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:18:06+00:00 2026-06-13T14:18:06+00:00

I am trying to create a basic thread from main by passing a function

  • 0

I am trying to create a basic thread from main by passing a function to _beginthread.
But My output is not getting completed.

I am getting the following output:

Starting thread
48
Main ends
I

Could someone please clarify what is wrong in the following code?

#include <iostream>
#include <process.h>
using namespace std;

void test(void *param)
{
    cout << "In thread function" << endl;
    Sleep(1000); // sleep for 1 second
    cout << "Thread function ends" << endl;
    _endthread();
}


int main()
{
    cout << "Starting thread" << endl;
    cout << _beginthread(test,0,NULL);
    cout << "Main ends" << endl;
    return 0;
}
  • 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-13T14:18:07+00:00Added an answer on June 13, 2026 at 2:18 pm

    Because return from the main will stop any threads in your application. You need to wait untill thread will stop. Simplest solution with global var – very bad example to be honest. You need to use wait functions on thread’s handle.

    #include <iostream>
    #include <process.h>
    using namespace std;
    
    bool threadFinished = false;
    
    void test(void *param)
    {
        cout << "In thread function" << endl;
        Sleep(1000); // sleep for 1 second
        cout << "Thread function ends" << endl;
        threadFinished = true;
        _endthread();
    }
    
    
    int main()
    {
        cout << "Starting thread" << endl;
    
        cout << _beginthread(test,0,NULL);
        while(!threadFinished)
        {
            Sleep(10);
        }
        cout << "Main ends" << endl;
        return 0;
    }
    

    How to use Wait functions:

    HANDLE hThread;
    hThread = (HANDLE)_beginthread( test, 0, NULL);
    WaitForSingleObject( hThread, INFINITE );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a basic database trigger that conditionally deletes rows from database1.table1
I am trying to just create a basic layout, but i am having trouble
basic question - I'm still learning Android - trying to create a seperate thread
I'm trying to set up a basic threaded class in C++, but I'm getting
I'm trying to create a basic chess board in C++ and output it. I
So I am trying to create a basic WinForm Application that uses SNMP, from
Im trying to create a basic AsyncTask to get a certificate from the key
I am trying to create a basic search function. I have a text field
I'm trying to create a basic shopping cart, having an issue with the product
Basically I'm trying to create a BASIC sorting program in ruby without using .sort

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.