C++ language

adplus-dvertising
Multi Threading in C++
Previous Home Next

Multithreading is the ability of a program or an operating system process to manage its use by more than one user at a time and to even manage multiple requests by the same user without having multiple copies of the programming running in the computer.

On a single processor, multithreading is generally implemented by time-division multiplexing (as in multitasking), and the central processing unit (CPU) switches between different software threads.

With multithreading you are able to run multiple blocks of independent code (functions) parallel with your main() function. Consider a thread a function running independent and simultaneous with the rest of your code, and therefore not interfering with the the execution order of your 'main' program. Initially, all C++ programs contain a single, default thread. All other threads must be explicitly created by the programmer

simple program for serial coded

#include <iostream>
extern "C"
 {
    #include <unistd.h>
 }
using namespace std;
void function1();
void function2();
int main()
{
function1();
function2();
return 0;
}
void function1()
{
    cout << "hello..." << endl ;
    sleep(2); // sleep method is used for sleep for 2 second
}
void function2()
{
    cout << " ....world" << endl ;
}
Previous Home Next