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
APCProcAn application-defined callback function used with the QueueUserAPC function.
GetOverlappedResultRetrieves the results of an overlapped operation.
QueueUserAPCAdds a user-mode asynchronous procedure call (APC) object to the APC queue of the specified thread.

 

Critical-Section Function//临界区Description
DeleteCriticalSectionReleases all resources used by an unowned critical section object.
EnterCriticalSectionWaits for ownership of the specified critical section object.
InitializeCriticalSectionInitializes a critical section object.
InitializeCriticalSectionAndSpinCountInitializes a critical section object and sets the spin count for the critical section.
LeaveCriticalSectionReleases ownership of the specified critical section object.
SetCriticalSectionSpinCountSets the spin count for the specified critical section.
TryEnterCriticalSectionAttempts to enter a critical section without blocking.

 

Event Function//事件对象Description
CreateEventCreates or opens a named or unnamed event object.
OpenEventOpens an existing named event object.
PulseEventSets the specified event object to the signaled state and then resets it to the nonsignaled state after releasing the appropriate number of waiting threads.
ResetEventSets the specified event object to the nonsignaled state.
SetEventSets the specified event object to the signaled state.

 

Interlocked FunctionDescription
InterlockedCompareExchangePerforms an atomic comparison of the specified values and exchanges the values, based on the outcome of the comparison.
InterlockedCompareExchangePointerPerforms an atomic comparison of the specified values and exchange of the values, based on the outcome of the comparison.
InterlockedDecrementDecrements (decreases by one) the value of the specified variable and checks the resulting value.
InterlockedExchange Atomically exchanges a pair of values.
InterlockedExchangeAddPerforms an atomic addition of an increment value to an addend variable.
InterlockedExchangePointerAtomically exchanges a pair of values.
InterlockedIncrementIncrements (increases by one) the value of the specified variable and checks the resulting value.

 

Mutex Function//互斥Description
CreateMutexCreates or opens a named or unnamed mutex object.
OpenMutexOpens an existing named mutex object.
ReleaseMutexReleases ownership of the specified mutex object.

 

Semaphore FunctionDescription
CreateSemaphoreCreates or opens a named or unnamed semaphore object.
OpenSemaphoreOpens an existing named semaphore object.
ReleaseSemaphoreIncreases the count of the specified semaphore object by a specified amount.

 

Timer-Queue Timer FunctionDescription
ChangeTimerQueueTimerUpdates a timer-queue timer.
CreateTimerQueueCreates a queue for timers.
CreateTimerQueueTimerCreates a timer-queue timer.
DeleteTimerQueueDeletes a timer queue.
DeleteTimerQueueExDeletes a timer queue.
DeleteTimerQueueTimerCancels a timer-queue timer.

 

Wait FunctionDescription
MsgWaitForMultipleObjectsReturns when the specified criteria for the specified objects is met.
MsgWaitForMultipleObjectsExReturns when the specified criteria for the specified objects is met.
RegisterWaitForSingleObjectDirects a wait thread in the thread pool to wait on the object.
SignalObjectAndWaitAllows the caller to atomically signal an object and wait on another object.
UnregisterWaitCancels a registered wait operation.
UnregisterWaitExCancels a registered wait operation.
WaitForMultipleObjectsReturns when the specified criteria for the specified objects is met.
WaitForMultipleObjectsExReturns when the specified criteria for the specified objects is met.
WaitForSingleObjectReturns 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
CancelWaitableTimerSets the specified waitable timer to the inactive state.
CreateWaitableTimerCreates or opens a waitable timer object.
OpenWaitableTimerOpens an existing named waitable timer object.
SetWaitableTimerActivates the specified waitable timer.
TimerAPCProcApplication-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) ;

 

转载于:https://www.cnblogs.com/aoyihuashao/archive/2010/05/05/1727892.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值