Visual C++ Threads Simple Example

66k views Asked by At

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;
}
3

There are 3 answers

6
kuperspb On BEST ANSWER

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 );
3
rkosegi On

You need to wait for thread to end using some synchronization primitives, or your program will call ExitProcess before thread finished his execution.

You may read about synchronization first to understand how to write multithreaded application.In your case you need single object wait functions.

See msdn example: http://msdn.microsoft.com/en-us/library/kdzttdcb%28v=vs.71%29.aspx

so, you main function should look something like this :

int main()
{
    HANDLE hThread;
    cout << "Starting thread" << endl;
    cout << (hThread = (HANDLE)_beginthread(test,0,NULL));
    WaitForSingleObject( hThread, INFINITE );
    cout << "Main ends" << endl;
    return 0;
}
0
Wickkiey On

You can use CreateThread method.

Simple example of multithreading.

#include <Windows.h>
#include <iostream>

using namespace std; 

DWORD WINAPI thread1(__in LPVOID lpParameter) {
    while (1) {
        std::cout << " From Thread 1 \n";
        Sleep(3000);
    }

}

DWORD WINAPI thread2(__in LPVOID lpParameter) {
    while (1) {
        std::cout << " From thread 2\n"; 
        Sleep(1000);

    }
}

int main()
{

    DWORD threadID1, threadID2; 
    HANDLE h1 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread1, 0, 0,&threadID1);
    HANDLE h2 = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)thread2, 0, 0, &threadID2);

    getchar();
    return 0;
}

where thread 1 prints for every 3 secs and thread2 prints for every sec.

You can allocate stack size if your process is bigger.

StackSize explanation