How to do Multithreading using C/C++

Multithreading is necessary for those programs which need to run two or more processes within a single program simultaneously. For example: when you play a game( say Mario)  in a computer, then you see two or more enemies are moving at the same time  or in case of Football game two or more than two players are moving at the same time. This phenomena is called Multithreading. In multithreading total time for a program is divided into small fraction of time, and at one fraction of time only one process runs. But the switching  time between one process to another is so small that our eyes cannot detect the difference between processes. This can be illustrated with the analogous of Tube Light. Suppose the frequency of voltage is 50Hz. Then a tube light which operates by an Alternating Current, lights on 50 times and lights off 50 times in a second. But we see it as if it is continuously glowing. In the similar way, a single process runs in a single time but we see as if all processes are running at the same time.

Honestly speaking C and C++ do not support multithreading themselves so far ( Note: C++0x is releasing soon which supports Multithreading) . But we can use  windows.h with C and C++ program which has functions for Multithreading. So for Linux user this tutorial won’t help you.  Lets discuss how to do a multithreading in windows with C and C++.

First of all you have to include windows.h in your program by writing #include<windows.h> at the beginning of the program. Then you declare the handles to handle the threading.

HANDLE hThread1;

HANDLE hThread2;

Declare the handles as many as your threading. I have two process so I declare two handles. To begin the threading, we have function as given below

hThread1 = (HANDLE) _beginthread(func1, 0, 0);

hThread2 = (HANDLE) _beginthread(func2, 0, 0);

_beginthread requires three arguments. First argument is function which you want to thread. The two other arguments are generally 0. So both functions func1 and func2 are executed at a single time.

If threading completes then thread can be ended by using function _endthread().

The complete source code for a example of threading and its source code is given below:

Source Code:

#include<stdio.h>

#include<conio.h>

#include<windows.h>


void massage1(int *param){

    int i = 0;

    while(1){

        printf("\nFrom function 1 i = %d", i);

        Sleep(1000);

        i++;

    }

    _endthread();


}


void massage2(int *param){

    int j = 0;

    while(1){

        printf("\nFrom function 2 j = %d", j);

        Sleep(1000);

        j++;

    }

    _endthread();

}


int main(){

    HANDLE handle1;

    HANDLE handle2;

    printf("Press any key to begin and end thread");

    getch();

    printf("\nThreading Begins");

    handle1 =  (HANDLE) _beginthread(massage1,0,0);

    handle2 =  (HANDLE) _beginthread(massage2,0,0);

    getch();

    printf("\nThreading Ends");

    return 0;

}

Output:

thread

SHARE How to do Multithreading using C/C++

You may also like...

2 Responses

  1. arslan says:

    I ran this code but it is making this error can u please tell what this means …. ?

    '_beginthread' : cannot convert parameter 1 from 'void (__cdecl *)(void)' to 'void (__cdecl *)(void *)'

  2. Anonymous says:

    hi
    Instead "int*" in "void massage1(int *param)"&"void massage2(int *param)" function, place "void*".
    void massage1(void* param)
    {
    }
    "KNOt"

Leave a Reply

Your email address will not be published.

Share