基础概念澄清

概念很重要

函数指针类型与变量

1
2
typedef void(*pFunc)(int); // 此处是一个指针类型
void(*pFunc)(int); // 此处是一个指针变量

标准文件描述符

1
2
3
0 ----- 标准输入
1 ----- 标准输出
2 ----- 标准错误

Win32 线程管理

  1. CreateThread –> Win32 API

    1
    2
    3
    4
    5
    6
    7
    Creates a thread to execute within the virtual address space of the calling process.
    To create a thread that runs in the virtual address space of another process,
    use the CreateRemoteThread function.

    其他相关:
    CreateRemoteThread OpenThread GetCurrentThread ExitThread
    GetExitCodeThread GetThreadPriority SetThreadPriority CloseHandle

    参考文档:
    https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createthread
    示例程序:
    https://docs.microsoft.com/en-us/windows/win32/procthread/creating-threads
    https://www.codeproject.com/articles/13557/creating-threads-using-the-createthread-api

  2. AfxBeginThread –> MFC

    1
    2
    3
    4
    AfxBeginThread creates a new CWinThread object, calls its CreateThread function to start executing the thread, 
    and returns a pointer to the thread. Checks are made throughout the procedure to make sure all objects are
    deallocated properly should any part of the creation fail. To end the thread,
    call AfxEndThread from within the thread, or return from the controlling function of the worker thread.

    示例程序:
    https://docs.microsoft.com/en-us/cpp/mfc/reference/csocket-class?view=msvc-160#attach
    官方文档:
    https://docs.microsoft.com/en-us/cpp/mfc/reference/application-information-and-management?view=msvc-160#afxbeginthread

  3. _beginthread _beginthreadex –> C run-time library (CRT)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    A thread in an executable that calls the C run-time library (CRT) 
    should use the _beginthreadex and _endthreadex functions
    for thread management rather than CreateThread and ExitThread;
    this requires the use of the multithreaded version of the CRT.

    The _beginthread function creates a thread
    that begins execution of a routine at start_address.
    The routine at start_address must use the __cdecl (for native code) or __clrcall (for managed code)
    calling convention and should have no return value.
    When the thread returns from that routine, it is terminated automatically.

    _beginthreadex resembles the Win32 CreateThread API more closely than _beginthread does.
    _beginthreadex differs from _beginthread in the following ways:

    1) _beginthreadex has three additional parameters: initflag, Security, and threadaddr.
    The new thread can be created in a suspended state, with a specified security,
    and can be accessed by using thrdaddr, which is the thread identifier.

    2) The routine at start_address that's passed to _beginthreadex must use the __stdcall (for native code)
    or __clrcall (for managed code) calling convention and must return a thread exit code.

    3) _beginthreadex returns 0 on failure, rather than -1L.

    4) A thread that's created by using _beginthreadex is terminated by a call to _endthreadex.

    _endthread automatically closes the thread handle, whereas _endthreadex does not.
    Therefore, when you use _beginthread and _endthread, do not explicitly close the thread handle by calling the Win32 CloseHandle API.
    This behavior differs from the Win32 ExitThread API.

    官方文档:
    https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/beginthread-beginthreadex?view=msvc-160
    参考网址:
    https://docs.microsoft.com/en-us/cpp/parallel/multithreading-support-for-older-code-visual-cpp?view=msvc-160
    https://docs.microsoft.com/en-us/cpp/parallel/multithreading-terminating-threads?view=msvc-160

  4. std::thread

    1

Linux 线程管理

留言