Creating Threads

本文介绍如何使用CreateThread函数创建并启动线程,演示了通过传递参数到线程函数来执行特定任务的方法,并展示了如何等待线程结束及清理资源。
Creating Threads

The CreateThread function creates a new thread for a process. The creating thread must specify the starting address of the code that the new thread is to execute. Typically, the starting address is the name of a function defined in the program code (for more information, see ThreadProc). This function takes a single parameter and returns a DWORD value. A process can have multiple threads simultaneously executing the same function.

The following is a simple example that demonstrates how to create a new thread that executes the locally defined function, MyThreadFunction.

The calling thread uses the WaitForMultipleObjects function to persist until all worker threads have terminated. The calling thread blocks while it is waiting; to continue processing, a calling thread would use WaitForSingleObject and wait for each worker thread to signal its wait object. Note that if you were to close the handle to a worker thread before it terminated, this does not terminate the worker thread. However, the handle will be unavailable for use in subsequent function calls.

#include <windows.h>
#include <tchar.h>
#include <strsafe.h>

#define MAX_THREADS 3
#define BUF_SIZE 255

DWORD WINAPI MyThreadFunction( LPVOID lpParam );
void ErrorHandler(LPTSTR lpszFunction);

// Sample custom data structure for threads to use.
// This is passed by a void pointer so it can be any data type
// that can be passed using a single void pointer (LPVOID).
typedef struct MyData {
    int val1;
    int val2;
} MYDATA, *PMYDATA;

int _tmain()
{
    PMYDATA pDataArray[MAX_THREADS];
    DWORD   dwThreadIdArray[MAX_THREADS];
    HANDLE  hThreadArray[MAX_THREADS]; 

    // Create MAX_THREADS worker threads.

    for( int i=0; i<MAX_THREADS; i++ )
    {
        // Allocate memory for thread data.

        pDataArray[i] = (PMYDATA) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
                sizeof(MYDATA));

        if( pDataArray[i] == NULL )
        {
           // If the array allocation fails, the system is out of memory
           // so there is no point in trying to print an error message.
           // Just terminate execution.
            ExitProcess(2);
        }

        // Generate unique data for each thread to work with.

        pDataArray[i]->val1 = i;
        pDataArray[i]->val2 = i+100;

        // Create the thread to begin execution on its own.

        hThreadArray[i] = CreateThread( 
            NULL,                   // default security attributes
            0,                      // use default stack size  
            MyThreadFunction,       // thread function name
            pDataArray[i],          // argument to thread function 
            0,                      // use default creation flags 
            &dwThreadIdArray[i]);   // returns the thread identifier 


        // Check the return value for success.
        // If CreateThread fails, terminate execution. 
        // This will automatically clean up threads and memory. 

        if (hThreadArray[i] == NULL) 
        {
           ErrorHandler(TEXT("CreateThread"));
           ExitProcess(3);
        }
    } // End of main thread creation loop.

    // Wait until all threads have terminated.

    WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE);

    // Close all thread handles and free memory allocations.

    for(int i=0; i<MAX_THREADS; i++)
    {
        CloseHandle(hThreadArray[i]);
        if(pDataArray[i] != NULL)
        {
            HeapFree(GetProcessHeap(), 0, pDataArray[i]);
            pDataArray[i] = NULL;    // Ensure address is not reused.
        }
    }

    return 0;
}

DWORD WINAPI MyThreadFunction( LPVOID lpParam ) 
{ 
    HANDLE hStdout;
    PMYDATA pDataArray;

    TCHAR msgBuf[BUF_SIZE];
    size_t cchStringSize;
    DWORD dwChars;

    // Make sure there is a console to receive output results. 

    hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
    if( hStdout == INVALID_HANDLE_VALUE )
        return 1;

    // Cast the parameter to the correct data type.
    // The pointer is known to be valid because 
    // it was checked for NULL before the thread was created.
 
    pDataArray = (PMYDATA)lpParam;

    // Print the parameter values using thread-safe functions.

    StringCchPrintf(msgBuf, BUF_SIZE, TEXT("Parameters = %d, %d/n"), 
        pDataArray->val1, pDataArray->val2); 
    StringCchLength(msgBuf, BUF_SIZE, &cchStringSize);
    WriteConsole(hStdout, msgBuf, (DWORD)cchStringSize, &dwChars, NULL);

    return 0; 
} 

void ErrorHandler(LPTSTR lpszFunction) 
{ 
    // Retrieve the system error message for the last-error code.

    LPVOID lpMsgBuf;
    LPVOID lpDisplayBuf;
    DWORD dw = GetLastError(); 

    FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        dw,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );

    // Display the error message.

    lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, 
        (lstrlen((LPCTSTR)lpMsgBuf)+lstrlen((LPCTSTR)lpszFunction)+40)*sizeof(TCHAR)); 
    StringCchPrintf((LPTSTR)lpDisplayBuf, 
        LocalSize(lpDisplayBuf) / sizeof(TCHAR),
        TEXT("%s failed with error %d: %s"), 
        lpszFunction, dw, lpMsgBuf); 
    MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK); 

    // Free the error-handling buffer allocations.

    LocalFree(lpMsgBuf);
    LocalFree(lpDisplayBuf);
}

The MyThreadFunction function avoids the use of the C run-time library (CRT), as many of its functions are not thread-safe, particularly if you are not using the multithreaded CRT. If you would like to use the CRT in a ThreadProc function, use the_beginthreadex function instead.

It is risky to pass the address of a local variable if the creating thread exits before the new thread, because the pointer becomes invalid. Instead, either pass a pointer to dynamically allocated memory or make the creating thread wait for the new thread to terminate. Data can also be passed from the creating thread to the new thread using global variables. With global variables, it is usually necessary to synchronize access by multiple threads. For more information about synchronization, see Synchronizing Execution of Multiple Threads.

The creating thread can use the arguments to CreateThread to specify the following:

  • The security attributes for the handle to the new thread. These security attributes include an inheritance flag that determines whether the handle can be inherited by child processes. The security attributes also include a security descriptor, which the system uses to perform access checks on all subsequent uses of the thread's handle before access is granted.
  • The initial stack size of the new thread. The thread's stack is allocated automatically in the memory space of the process; the system increases the stack as needed and frees it when the thread terminates. For more information, see Thread Stack Size.
  • A creation flag that enables you to create the thread in a suspended state. When suspended, the thread does not run until the ResumeThread function is called.

You can also create a thread by calling the CreateRemoteThread function. This function is used by debugger processes to create a thread that runs in the address space of the process being debugged.


Send comments about this topic to Microsoft

Build date: 8/7/2008


CreateThread Function

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.

Syntax
HANDLE WINAPI CreateThread(
  __in_opt   LPSECURITY_ATTRIBUTES lpThreadAttributes,
  __in       SIZE_T dwStackSize,
  __in       LPTHREAD_START_ROUTINE lpStartAddress,
  __in_opt   LPVOID lpParameter,
  __in       DWORD dwCreationFlags,
  __out_opt  LPDWORD lpThreadId
);
Parameters
lpThreadAttributes [in, optional]

A pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited.

The lpSecurityDescriptor member of the structure specifies a security descriptor for the new thread. If lpThreadAttributesis NULL, the thread gets a default security descriptor. The ACLs in the default security descriptor for a thread come from the primary token of the creator.

Windows XP/2000:  The ACLs in the default security descriptor for a thread come from the primary or impersonation token of the creator. This behavior changed with Windows XP SP2 and Windows Server 2003. For more information, see Remarks.
dwStackSize [in]

The initial size of the stack, in bytes. The system rounds this value to the nearest page. If this parameter is zero, the new thread uses the default size for the executable. For more information, see Thread Stack Size.

lpStartAddress [in]

A pointer to the application-defined function to be executed by the thread. This pointer represents the starting address of the thread. For more information on the thread function, see ThreadProc.

lpParameter [in, optional]

A pointer to a variable to be passed to the thread.

dwCreationFlags [in]

The flags that control the creation of the thread.

ValueMeaning

0

The thread runs immediately after creation.

CREATE_SUSPENDED
0x00000004

The thread is created in a suspended state, and does not run until theResumeThread function is called.

STACK_SIZE_PARAM_IS_A_RESERVATION
0x00010000

The dwStackSize parameter specifies the initial reserve size of the stack. If this flag is not specified, dwStackSize specifies the commit size.

Windows 2000:  The STACK_SIZE_PARAM_IS_A_RESERVATION flag is not supported.
lpThreadId [out, optional]

A pointer to a variable that receives the thread identifier. If this parameter is NULL, the thread identifier is not returned.

Return Value

If the function succeeds, the return value is a handle to the new thread.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Note that CreateThread may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the thread's process. This behavior is similar to the asynchronous nature of CreateProcess, where the process is created even if it refers to invalid or missing dynamic-link libraries (DLLs).

Remarks

The number of threads a process can create is limited by the available virtual memory. By default, every thread has one megabyte of stack space. Therefore, you can create at most 2,028 threads. If you reduce the default stack size, you can create more threads. However, your application will have better performance if you create one thread per processor and build queues of requests for which the application maintains the context information. A thread would process all requests in a queue before processing requests in the next queue.

The new thread handle is created with the THREAD_ALL_ACCESS access right. If a security descriptor is not provided when the thread is created, a default security descriptor is constructed for the new thread using the primary token of the process that is creating the thread. When a caller attempts to access the thread with the OpenThread function, the effective token of the caller is evaluated against this security descriptor to grant or deny access.

Windows XP/2000:  If a security descriptor is not provided when the thread is created, a default security descriptor is constructed using the effective token of the thread. If the thread is impersonating another user, the thread's effective token is the impersonation token and the default security descriptor allows access only to the impersonation token's TokenDefaultDacl owner or members. If the thread is not impersonating another user, the thread's effective token is its primary token. This behavior changed starting with Windows XP SP2 and Windows Server 2003. For more information, see  Thread Security and Access Rights.

The newly created thread has full access rights to itself when calling the GetCurrentThread function.

Windows Server 2003 and Windows XP/2000:  The thread's access rights to itself are computed by evaluating the primary token of the process in which the thread was created against the default security descriptor constructed for the thread. If the thread is created in a remote process, the primary token of the remote process is used. As a result, the newly created thread may have reduced access rights to itself when calling  GetCurrentThread. Some access rights including THREAD_SET_THREAD_TOKEN and THREAD_GET_CONTEXT may not be present, leading to unexpected failures. For this reason, creating a thread while impersonating another user is not recommended.

The thread execution begins at the function specified by the lpStartAddress parameter. If this function returns, the DWORDreturn value is used to terminate the thread in an implicit call to the ExitThread function. Use the GetExitCodeThread function to get the thread's return value.

The thread is created with a thread priority of THREAD_PRIORITY_NORMAL. Use the GetThreadPriority and SetThreadPriorityfunctions to get and set the priority value of a thread.

When a thread terminates, the thread object attains a signaled state, satisfying any threads that were waiting on the object.

The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call toCloseHandle.

The ExitProcessExitThreadCreateThreadCreateRemoteThread functions, and a process that is starting (as the result of a call by CreateProcess) are serialized between each other within a process. Only one of these events can happen in an address space at a time. This means that the following restrictions hold:

  • During process startup and DLL initialization routines, new threads can be created, but they do not begin execution until DLL initialization is done for the process.
  • Only one thread in a process can be in a DLL initialization or detach routine at a time.
  • ExitProcess does not return until no threads are in their DLL initialization or detach routines.

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 multi-threaded version of the CRT. If a thread created using CreateThread calls the CRT, the CRT may terminate the process in low-memory conditions.

Examples

For an example, see Creating Threads.

Requirements
ClientRequires Windows Vista, Windows XP, or Windows 2000 Professional.
ServerRequires Windows Server 2008, Windows Server 2003, or Windows 2000 Server.
Header

Declared in Winbase.h; include Windows.h.

Library

Use Kernel32.lib.

DLL

Requires Kernel32.dll.

See Also

CloseHandle
CreateProcess
CreateRemoteThread
ExitProcess
ExitThread
GetExitCodeThread
GetThreadPriority
Process and Thread Functions
ResumeThread
SetThreadPriority
SECURITY_ATTRIBUTES
ThreadProc
Threads

<think>好的,我现在需要帮助用户解决如何翻译Python项目的部署、启动和使用文档,并指导他们根据这些说明操作项目。首先,我需要回顾之前的对话历史和用户提供的引用内容,确保回答的相关性。用户提到他们参考了多个引用,其中引用[1]涉及全栈Python项目,提到了Django、Flask等框架的部署指导。引用[4]详细说明了项目部署的步骤,包括数据库配置、uWSGI和Nginx的使用。这些信息是关键的,因为用户需要翻译部署文档,并实际操作。接下来,用户的需求分为两部分:翻译文档和操作指导。我需要将这两部分结合起来,提供结构化的步骤。首先,翻译文档需要理解技术术语,确保准确性。然后,操作指导需要详细说明每一步骤,比如安装依赖、配置环境、启动服务等。考虑到用户可能对部署流程不熟悉,我需要分解步骤,使用代码块和命令示例,方便用户复制执行。例如,引用[4]中的uWSGI和Nginx配置,需要具体说明如何编写配置文件,如何启动服务。同时,用户可能需要了解如何迁移数据库,安装依赖包,这些都需要详细说明。 另外,用户提供的引用[3]提到SQL Translator项目,这可能涉及到数据库相关的部署步骤,需要结合引用[4]中的数据库配置部分。还有引用[5]中的翻译器代码示例,可能帮助用户理解如何处理文本翻译,但当前问题更关注部署和启动,所以这部分可能暂时不需要深入。 用户可能遇到的常见问题包括环境配置错误、依赖冲突、服务启动失败等,需要在指导中提供排查方法,比如查看日志文件、使用ps命令检查进程状态。同时,引用[2]中的日志生成和清理内容,可以建议用户配置日志管理,确保部署稳定性。 最后,生成相关问题时要围绕部署、配置、扩展和维护,帮助用户深入理解后续步骤。例如,如何处理高并发部署,如何监控性能,或者如何自动化部署流程,这些都是用户可能感兴趣的方向。 总结来说,我需要将引用中的关键信息整合,分步骤指导用户翻译和操作,确保每个环节都有明确的命令和配置示例,同时预见到用户可能遇到的问题,并提供解决方案。这样用户不仅能翻译文档,还能顺利部署和运行Python项目。</think>### Python项目部署与使用指南翻译及操作说明 #### 一、部署文档翻译要点 1. **环境配置要求** - Python版本要求: $3.8 \leq version \leq 3.11$ - 数据库配置参考[^4]: ```sql CREATE DATABASE app_db CHARACTER SET utf8mb4; GRANT ALL PRIVILEGES ON app_db.* TO &#39;app_user&#39;@&#39;localhost&#39;; ``` 2. **依赖安装** ```bash # 通过requirements.txt安装依赖 pip install -r requirements.txt --no-cache-dir ``` 3. **服务启动方式** - 开发模式[^4]: ```bash python manage.py runserver 0.0.0.0:8000 ``` - 生产模式(uWSGI+Nginx): ```ini # uwsgi.ini配置示例 [uwsgi] http = :8000 chdir = /path/to/project module = app.wsgi processes = 4 ``` #### 二、操作指导手册 1. **部署步骤分解** **步骤1:迁移数据库** ```bash python manage.py makemigrations python manage.py migrate ``` **步骤2:静态文件收集** ```bash python manage.py collectstatic ``` **步骤3:服务进程管理** ```bash # 启动uWSGI uwsgi --ini uwsgi.ini # Nginx配置反向代理 location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; } ``` 2. **监控与日志** ```bash # 查看实时日志 tail -f /var/log/uwsgi/app.log # 进程状态检查 ps aux | grep -E &#39;uwsgi|nginx&#39; ``` #### 三、典型问题解决方案 1. **依赖冲突处理** ```bash # 使用虚拟环境隔离 python -m venv venv source venv/bin/activate ``` 2. **端口占用问题** ```bash # 查找占用8000端口的进程 lsof -i :8000 # 终止进程 kill -9 <PID> ``` #### 四、高级部署配置 1. **负载均衡设置** $$ \text{吞吐量} = \frac{\text{总请求数}}{\text{平均响应时间}} \times \text{节点数} $$ ```nginx # Nginx负载均衡配置 upstream app_servers { server 192.168.1.10:8000 weight=3; server 192.168.1.11:8000; } ``` 2. **自动化部署脚本** ```python # deploy.py示例 import subprocess subprocess.run(["git", "pull"]) subprocess.run(["systemctl", "restart", "uwsgi"]) ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值