【问题标题】:What is thread?How to create thread in win32 application?什么是线程?如何在win32应用程序中创建线程?
【发布时间】:2010-12-29 19:27:41
【问题描述】:

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

【问题讨论】:

标签: winapi


【解决方案1】:

线程是一个轻量级的进程。线程可以松散地定义为一个单独的执行流,它与可能发生的所有其他事情同时发生并且独立于其他事情发生。线程就像一个经典程序,从 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();
}

【讨论】:

  • 调用同一个函数?
【解决方案2】:

如果您正在编写 C/C++ 程序,请不要使用 CreateThread(),而是使用 _beginthreadex()。

_beginthreadex() 将初始化 C/C++ 运行时,但 CreateThread() 不会。

【讨论】:

    【解决方案3】:

    线程是当前占用CPU的上下文,是Windows CE调度的部分。

    要创建线程,请使用CreateThread。您可以阅读更多线程和处理函数here

    此信息对于 Windows CE 6 也是正确的。

    【讨论】:

      【解决方案4】:

      维基百科中解释了非常流行:)

      http://en.wikipedia.org/wiki/Thread_%28computer_science%29

      那怎么处理呢,你可以去看看

      .NET multithreading (Alan Dennis) isbn=1930110545

      【讨论】:

        【解决方案5】:

        所有这些答案都建议使用CreateThread()

        这只是一个糟糕的建议。

        通常应该使用_beginthread()_beginthreadex() 创建线程,以确保正确初始化 C/C++ 运行时线程局部结构。

        有关更多详细信息,请参阅有关此问题的讨论:Windows threading: _beginthread vs _beginthreadex vs CreateThread C++

        【讨论】:

        • 如果是完整的 Windows 操作系统,那将是真的。不幸的是,有问题的操作系统是 Windows CE。它没有_BeginThread
        • @Shaihi,该问题并未将 Windows CE 指定为答案的约束。
        • "polyglot - 但它确实如此,因为它被标记为Windows CE
        • 自从我方便地回答后,该问题似乎已被重新标记。 WinCE 与 Win32 不同,Win32 和 Win64 也是如此,请参阅en.wikipedia.org/wiki/Windows_API#Versions
        • 如果您在Windows CE的情况下更新问题并添加您的答案不正确的相关信息,我可以取消我的反对票。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-01-11
        • 2010-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-04
        • 1970-01-01
        相关资源
        最近更新 更多