C program example on how to create the threads in Windows system

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows Xp Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged.

To do: Creating threads in Windows system

To show: The various Windows threads and processes functions usage with C code sample

 

 

 

// For WinXp as a target

#define _WIN32_WINNT 0x0501

 

#include <windows.h>

#include <stdio.h>

#include <conio.h>

 

DWORD WINAPI MyThreadFunction(LPVOID lpParam)

{

printf("The parameter: %u.\n", *(DWORD*)lpParam);

return 0;

}

 

int main(void)

{

DWORD dwThreadId, dwThrdParam = 1;

HANDLE hThread;

int x;

 

// Let try a loop...

for(x = 1; x <= 10; x++)

{

hThread = CreateThread(

NULL, // default security attributes

0, // use default stack size

MyThreadFunction, // thread function

&dwThrdParam, // argument to thread function

0, // use default creation flags

&dwThreadId); // returns the thread identifier

 

// Check the return value for success. If something wrong...

if (hThread == NULL)

printf("CreateThread() failed, error: %d.\n", GetLastError());

//else, gives some prompt...

else

{

printf("It seems the CreateThread() is OK lol!\n");

printf("The thread ID: %u.\n", dwThreadId);

}

if (CloseHandle(hThread) != 0)

printf("Handle to thread closed successfully.\n");

} // end for loop...

return 0;

}

 

Output example:

 

It seems the CreateThread() is OK lol!

The parameter: 1.

The thread ID: 2768.

Handle to thread closed successfully.

It seems the CreateThread() is OK lol!

The thread ID: 2428.

The parameter: 1.

Handle to thread closed successfully.

It seems the CreateThread() is OK lol!

The thread ID: 2792.

The parameter: 1.

Handle to thread closed successfully.

It seems the CreateThread() is OK lol!

The thread ID: 2808.

The parameter: 1.

Handle to thread closed successfully.

It seems the CreateThread() is OK lol!

The thread ID: 1180.

The parameter: 1.

Handle to thread closed successfully.

It seems the CreateThread() is OK lol!

The thread ID: 1376.

Handle to thread closed successfully.

The parameter: 1.

It seems the CreateThread() is OK lol!

The thread ID: 2816.

The parameter: 1.

Handle to thread closed successfully.

It seems the CreateThread() is OK lol!

The thread ID: 1624.

The parameter: 1.

Handle to thread closed successfully.

It seems the CreateThread() is OK lol!

The thread ID: 2712.

The parameter: 1.

Handle to thread closed successfully.

It seems the CreateThread() is OK lol!

The thread ID: 1732.

The parameter: 1.

Handle to thread closed successfully.

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index