zoukankan      html  css  js  c++  java
  • Windows程序设计 读书笔记 多线程

    一、多线程体系结构

      主线程创建程序所需要的所有窗口,并包括所有的窗口过程,以便处理这些窗口的所有消息;其他线程只进行一些后台处理,除了与主线程进行通信,他们不与用户进行交流。

      超过1/10秒的事件(大作业,如拼写检测、数据库排序、打印)不应该放在消息队列中,而应该放在单独的线程中处理。

      在线程终止时,线程创建的资源不会自动释放。

    二、多线程API:

      hThread = CreateThread(...)

      DWORD WINAPI ThreadProc([PVOID pParam)

      _beginthread(...)

      void __cdecl ThreadProc(void* pParam)///在此函数结尾一般有_endthread();

      ResumeThread  //恢复线程

      Sleep(0)//线程交出尚未完成的时间片。

    三、MSDN  

    Synchronization Functions

    Synchronization Functions

    The following functions are used in synchronization.

    Asynchronous FunctionDescription
    APCProc An application-defined callback function used with the QueueUserAPC function.
    GetOverlappedResult Retrieves the results of an overlapped operation.
    QueueUserAPC Adds a user-mode asynchronous procedure call (APC) object to the APC queue of the specified thread.
    Critical-Section Function//临界区Description
    DeleteCriticalSection Releases all resources used by an unowned critical section object.
    EnterCriticalSection Waits for ownership of the specified critical section object.
    InitializeCriticalSection Initializes a critical section object.
    InitializeCriticalSectionAndSpinCount Initializes a critical section object and sets the spin count for the critical section.
    LeaveCriticalSection Releases ownership of the specified critical section object.
    SetCriticalSectionSpinCount Sets the spin count for the specified critical section.
    TryEnterCriticalSection Attempts to enter a critical section without blocking.
    Event Function//事件对象Description
    CreateEvent Creates or opens a named or unnamed event object.
    OpenEvent Opens an existing named event object.
    PulseEvent Sets the specified event object to the signaled state and then resets it to the nonsignaled state after releasing the appropriate number of waiting threads.
    ResetEvent Sets the specified event object to the nonsignaled state.
    SetEvent Sets the specified event object to the signaled state.
    Interlocked FunctionDescription
    InterlockedCompareExchange Performs an atomic comparison of the specified values and exchanges the values, based on the outcome of the comparison.
    InterlockedCompareExchangePointer Performs an atomic comparison of the specified values and exchange of the values, based on the outcome of the comparison.
    InterlockedDecrement Decrements (decreases by one) the value of the specified variable and checks the resulting value.
    InterlockedExchange Atomically exchanges a pair of values.
    InterlockedExchangeAdd Performs an atomic addition of an increment value to an addend variable.
    InterlockedExchangePointer Atomically exchanges a pair of values.
    InterlockedIncrement Increments (increases by one) the value of the specified variable and checks the resulting value.
    Mutex Function//互斥Description
    CreateMutex Creates or opens a named or unnamed mutex object.
    OpenMutex Opens an existing named mutex object.
    ReleaseMutex Releases ownership of the specified mutex object.
    Semaphore FunctionDescription
    CreateSemaphore Creates or opens a named or unnamed semaphore object.
    OpenSemaphore Opens an existing named semaphore object.
    ReleaseSemaphore Increases the count of the specified semaphore object by a specified amount.
    Timer-Queue Timer FunctionDescription
    ChangeTimerQueueTimer Updates a timer-queue timer.
    CreateTimerQueue Creates a queue for timers.
    CreateTimerQueueTimer Creates a timer-queue timer.
    DeleteTimerQueue Deletes a timer queue.
    DeleteTimerQueueEx Deletes a timer queue.
    DeleteTimerQueueTimer Cancels a timer-queue timer.
    Wait FunctionDescription
    MsgWaitForMultipleObjects Returns when the specified criteria for the specified objects is met.
    MsgWaitForMultipleObjectsEx Returns when the specified criteria for the specified objects is met.
    RegisterWaitForSingleObject Directs a wait thread in the thread pool to wait on the object.
    SignalObjectAndWait Allows the caller to atomically signal an object and wait on another object.
    UnregisterWait Cancels a registered wait operation.
    UnregisterWaitEx Cancels a registered wait operation.
    WaitForMultipleObjects Returns when the specified criteria for the specified objects is met.
    WaitForMultipleObjectsEx Returns when the specified criteria for the specified objects is met.
    WaitForSingleObject Returns when the specified criteria for the specified object is met.
    WaitForSingleObjectEx Returns when the specified criteria for the specified object is met.
    WaitOrTimerCallback Returns when the specified criteria is met.
    Waitable Timer FunctionDescription
    CancelWaitableTimer Sets the specified waitable timer to the inactive state.
    CreateWaitableTimer Creates or opens a waitable timer object.
    OpenWaitableTimer Opens an existing named waitable timer object.
    SetWaitableTimer Activates the specified waitable timer.
    TimerAPCProc Application-defined timer completion routine used with the SetWaitableTimer function.

    =================

    注解。

    临界区,任意时刻只有一个线程可以进入临界区:

    init:    BOOL isused = false; //表示没线程进入临界区

    enter:    while(isused){sleep(1);} isused=true;

    leave:    isused = false;

    临界区只能用于同一进程内不同线程间的协调。不同进程间的协调,要用互斥。

    事件对象的用法:

    初始化

    hEvent = CreateEvent (NULL, FALSE, FALSE, NULL) ;

    beginthread (Thread, 0, &params) ;

    线程函数:

    void Thread (PVOID pvoid)
    {

       hEvent = ...//从pvoid中获得
         while (TRUE)
         {
              WaitForSingleObject (hEvent, INFINITE) ;         
         //。。。。。。。。。。主体
         }
    }

    开始执行:

    SetEvent (hEvent) ;

  • 相关阅读:
    Puppet部署
    ldap命令
    openldap slapd.conf参数
    ldap objectclass
    ldap 测试表设计
    Nginx/LVS/HAProxy负载均衡软件的优缺点
    SNAT DNAT MASQUERADE 区别
    iptables常用规则
    Python中文注释报错的解决方法
    用Node.js给邮箱发送邮件
  • 原文地址:https://www.cnblogs.com/aoyihuashao/p/1727892.html
Copyright © 2011-2022 走看看