Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual C++ Threads Simple Example

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;
}
like image 888
TechyHarry Avatar asked Oct 25 '12 06:10

TechyHarry


People also ask

Is C good for multithreading?

C does not contain any built-in support for multithreaded applications. Instead, it relies entirely upon the operating system to provide this feature. This tutorial assumes that you are working on Linux OS and we are going to write multi-threaded C program using POSIX.

Is threading possible in C?

Can we write multithreading programs in C? Unlike Java, multithreading is not supported by the language standard. POSIX Threads (or Pthreads) is a POSIX standard for threads. Implementation of pthread is available with gcc compiler.

How do I see all threads in Visual Studio?

While Visual Studio is in debug mode, select the Debug menu, point to Windows, and then select Threads.

Is Visual Studio multithreaded?

Visual Studio provides different tools for use in debugging multithreaded apps. For threads, the primary tools for debugging threads are the Threads window, thread markers in source windows, the Parallel Stacks window, the Parallel Watch window, and the Debug Location toolbar.


2 Answers

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 );
like image 62
kuperspb Avatar answered Sep 22 '22 12:09

kuperspb


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

like image 36
Wickkiey Avatar answered Sep 22 '22 12:09

Wickkiey