What is thread? How to create thread in win32 application?
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.
Hi. Very popular is explained in Wikipedia :)
http://en.wikipedia.org/wiki/Thread%5F%28computer%5Fscience%29
What about how to handle it, you can read by ex the
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.
http://en.wikipedia.org/wiki/Thread%5F%28computer%5Fscience)
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();
}
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.
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: http://stackoverflow.com/questions/331536/windows-threading-beginthread-vs-beginthreadex-vs-createthread-c