什么是线程?如何在win32应用程序中创建线程?

3 winapi

什么是线程?如何在win32应用程序中创建线程?

Abh*_*eet 8

线程是一个轻量级的过程.线程可以松散地定义为单独的执行流,它与可能发生的所有其他事件同时发生并独立发生.线程就像一个经典的程序,从A点开始执行,直到它到达B点.它没有事件循环.线程独立于计算机中发生的任何其他操作.没有线程,整个程序可以由一个CPU密集型任务或一个无限循环(有意或无意)来保持.对于线程,其他不会卡在循环中的任务可以继续处理,而无需等待卡住的任务完成.请仔细阅读此链接以获取更多详细信息及其与流程的比较.

http://en.wikipedia.org/wiki/Thread_(computer_science)

创建线程非常容易,例如通过这个....

这是一个创建线程即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();
}
Run Code Online (Sandbox Code Playgroud)