Products
Services
Contact Us

Scripts: Cplus :: C++ threads :: Library Article #5

Developer's Section

Creating a Thread in C++
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (937)
Favorites:

Learn how to create a Thread in C++

In this article we will show you how to create a Windows Thread in C++. The following routine will generate 4 threads using the CreateThread() method. Then, these 4 threads will output a process number id in the console window.

 Code Snippet 1


#include <iostream>
#include <sstream>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <windows.h> 
#include <fstream> 
#include <map>
#include <vector>

using namespace std;

#include "conio.h"


int pid = 0; // the process number


DWORD WINAPI Child_Process( LPVOID Param) // child processes
{   
    int p_id = pid++; // the process id
    cout << "\nI am Thread #:" << p_id << endl;
}

// start the parent process (Server)
void startParentThread() {

// create 4 child processes    
    HANDLE handlers[4] = { hThread1, hThread2, hThread3, hThread4,};

    for(int i = 0; i < 4; i++) {
        handlers[i] = CreateThread(NULL,0,Child_Process,&ThrdParam,0
                                                     ,&ThreadId[i] );    
    }

}


int main() // main function
{
    cout << "Creating Threads:" << endl;

    startParentThread(); // call to parent process
    
    getch();
    
    return 0;
    
};



See other Scripts in C++ threads

Submit Your Scripts:

If you would like to have your C++ scripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]