开发者

What is thread?How to create thread in win32 application?

What is thread? How to create thread in win32 applicatio开发者_如何学编程n?


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_(computer_science)

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.


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.


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

.NET multithreading (Alan Dennis) isbn=1930110545


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++

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜