What is thread? How to create thread in win32 application?
5 Answers
Thread is a light weight process. A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently of everything else that might be happening. A thread is like a classic program that starts at point A and executes until it reaches point B. It does not have an event loop. A thread runs independently of anything else happening in the computer. Without threads an entire program can be held up by one CPU intensive task or one infinite loop, intentional or otherwise. With threads the other tasks that don't get stuck in the loop can continue processing without waiting for the stuck task to finish. Please go through this link for more detail and its comparision with process.
Creating thread is very easy for an example go through this....
This is a very example which creates thread i.e ThreadFun1
#include<windows.h>
#include<stdio.h>
#include<conio.h>
void __stdcall ThreadFun1()
{
printf("Hi This is my first thread.\n");
}
void main()
{
printf("Entered In Main\n");
HANDLE hThread;
DWORD threadID;
hThread = CreateThread(NULL, // security attributes ( default if NULL )
0, // stack SIZE default if 0
ThreadFun1, // Start Address
NULL, // input data
0, // creational flag ( start if 0 )
&threadID); // thread ID
printf("Other business in Main\n");
printf("Main is exiting\n");
CloseHandle(hThread);
getch();
}

- 6,459
- 10
- 35
- 53
Don't use CreateThread(), use _beginthreadex() instead if you are writing C/C++ programs.
_beginthreadex() will initialize the C/C++ runtime, but CreateThread() won't.

- 235
- 2
- 8
A thread is the context that currently occupies the CPU and is the part that is scheduled by Windows CE.
To create a thread use CreateThread
. You can read about more threading and process functions here.
This information is correct for Windows CE 6 as well.

- 3,952
- 4
- 27
- 47
Very popular is explained in Wikipedia :)
http://en.wikipedia.org/wiki/Thread_%28computer_science%29
What about how to handle it, you can read by ex the

- 28,010
- 62
- 221
- 374
All these answers suggest to use CreateThread()
That is simply poor advice.
Threads should generally be created with _beginthread()
or _beginthreadex()
to ensure the C/C++ runtime thread-local structures are appropriately initialised.
See the discussion on this question for further details: Windows threading: _beginthread vs _beginthreadex vs CreateThread C++
-
2That would be true had been a full Windows OS. Unfortunately, the OS at question is Windows CE. It does not have `_BeginThread`. – Shaihi Feb 07 '10 at 07:23
-
@Shaihi, the question does not specify Windows CE as a constraint to the answer. – polyglot Jul 18 '12 at 09:20
-
-
It appears the question has been retagged since I answered it, conveniently. WinCE is distinct to Win32, as is Win32 and Win64, see http://en.wikipedia.org/wiki/Windows_API#Versions – polyglot Oct 08 '12 at 16:21
-
I can take out my down vote if you update the question and add the relevant information that your answer is not correct in the case of `Windows CE`. – Shaihi Oct 08 '12 at 21:06