博客
关于我
Windows 临界区(CRITICAL_SECTION)的使用
阅读量:662 次
发布时间:2019-03-14

本文共 1826 字,大约阅读时间需要 6 分钟。

临界区与同步机制

在多线程环境中,确保临界数据的操作完整性至关重要。Linux系统使用锁来管理临界数据,而Windows则采用临界区(Critical Section)机制。

临界区的定义

临界区是程序中允许多个线程同时执行的代码段,例如自加自减操作等。在多线程环境下,如果不采取保护措施,这些代码段可能导致竞态条件,进而引发数据不一致或死锁问题。因此,需要用互斥机制来确保这些临界区在任何时刻只能被一个线程占有。

互斥与同步机制

互斥与同步是操作系统管理多线程共享资源的核心机制。尽管它们听起来很相似,但二者在实现方式上存在差异。简单来说:

  • 互斥:确保某一临界资源在任何时刻只能被规定数量的线程占用。
  • 同步:通过调度机制,使多个任务按照预定顺序访问共享资源。

在实际操作中,互斥是实现同步的核心手段。三个主要角色包括:

  • 不可独占的共享资源
  • 多个使用者(如线程或进程)
  • 调度者(决定资源访问顺序的核心,可能是内核或应用程序)

重要术语解析

  • 竞态条件:当多个线程同时访问共享资源时,若没有互斥保护,可能导致结果不确定。
  • 临界区:保护程序中需要互斥访问的代码段,使其只能被一个线程占用。

Windows临界区操作

Windows提供了丰富的API来管理临界区:

  • InitializeCriticalSection:创建临界区对象。
  • DeleteCriticalSection:释放临界区资源。
  • EnterCriticalSection: Wait 判断是否可以进入临界区(类似于Linux的锁)。
  • LeaveCriticalSection:退出临界区(类似于Linux的解锁)。

代码示例

以下程序展示了如何在Windows中使用临界区:

#include 
#include
#include
using namespace std;string g.Logf(const string& fmt, ...) { printf(fmt.c_str()); return "";}int counter = 0;CRITICAL_SECTION g_cs;void doit(void* arg) { int i, val; for (i = 0; i < 5000; ++i) { EnterCriticalSection(&g_cs); // 可能重复进入临界区,无需额外保护 val = counter; DoLf("thread %d : %d\n", (int)arg, val + 1); counter = val + 1; LeaveCriticalSection(&g_cs); }}int main(int argc, char* argv) { InitializeCriticalSection(&g_cs); HANDLE hThread1 = CreateThread( NULL, // STA或 apartment 模式 0, //_PRIORITY (LPTHREAD_START_ROUTINE)doit, (void*)1, // 标题 0, // 返回值 NULL); //_ptr economically HANDLE hThread2 = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)doit, (void*)2, 0, NULL); WaitForSingleObject(hThread1, INFINITE); WaitForSingleObject(hThread2, INFINITE); DeleteCriticalSection(&g_cs); return 0;}

运行结果应显示1~10000的连续计数,无重复或跳跃,证明临界区机制的有效性。

转载地址:http://phulz.baihongyu.com/

你可能感兴趣的文章
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>