Choosing between Critical Sections and Mutex

本文深入探讨了在多线程编程中选择和应用锁机制时需考虑的关键因素,包括性能成本、复杂性及不同场景下的重要性权衡。详细介绍了窗口句柄、互斥锁、信号量等机制的特性和使用场景,强调了正确选择锁机制对于提高程序性能和确保线程安全的重要性。

General points to consider:

  1. The performance cost of using the mechanism.
  2. The complexity introduced by using the mechanism.

In any given situation 1 or 2 may be more important.

E.g.

If you using multi-threading to write a high performance algorithm by making use of many cores and need to guard some data for safe access then 1 is probably very important.

If you have an application where a background thread is used to poll for some information on a timer and on the rare occasion it notices an update you need to guard some data for access then 2 is probably more important than 1.

1 will be down to the underlying implementation and probably scales with the scope of the protection e.g. a lock that is internal to a process is normally faster than a lock across all processes on a machine.

2 is easy to misjudge. First attempts to use locks to write thread safe code will normally miss some cases that lead to a deadlock. A simple deadlock would occur for example if thread A was waiting on a lock held by thread B but thread B was waiting on a lock held by thread A. Surprisingly easy to implement by accident.

On any given platform the naming and qualities of locking mechanisms may vary.

On windows critical sections are fast and process specific, mutexes are slower but cross process. Semaphores offer more complicated use cases. Some problems e.g. allocation from a pool may be solved very efficently using atomic functions rather than locks e.g. on windows InterlockedIncrement which is very fast indeed.



In Windows parlance, a critical section is a hybrid between a spin lock and a non-busy wait. It spins for a short time, then--if it hasn't yet grabbed the resource--it sets up an event and waits on it. If contention for the resource is low, the spin lock behavior is usually enough.

Critical Sections are a good choice for a multithreaded program that doesn't need to worry about sharing resources with other processes.

A mutex is a good general-purpose lock. A named mutex can be used to control access among multiple processes. But it's usually a little more expensive to take a mutex than a critical section.


下载代码方式:https://pan.quark.cn/s/e2157c05e625 在信息技术领域中,数学问题的复杂求解在很大程度上依赖于数值计算,这在科学计算、工程分析以及数据分析等多个方面尤为重要。线性方程组的求解是数值计算中的一个核心且关键的问题,而雅克比迭代法作为一种有效策略,专门用于处理大规模稀疏线性方程组。这个资源提供了一段采用C++语言编写的雅克比迭代法源代码,配合附带的博客文章,能够帮助使用者深入掌握此方法的基本原理和实际应用。 雅克比迭代法,有时也被称作局部迭代方法,主要用于求解形式为 Ax = b 的线性方程组,其中矩阵A需满足对角占优的条件。对角占优的特性是指矩阵中每个对角线元素的绝对值要大于该行其他元素绝对值之和,这一性质确保了算法的收敛性能。该方法的实施基于矩阵A的雅克比矩阵J,其构成方式为 J = D - L - U,其中D、L和U分别代表矩阵A的对角线部分、下三角部分以及上三角部分。 迭代过程的数学表达式为:x(k+1) = J^-1 * b + (I - J^-1*A) * x(k),在此表达式中,x(k)表示第k次迭代的解向量,x(k+1)则是第k+1次迭代的解向量,I是单位矩阵。每次迭代都利用前一次得到的解来计算下一次的解,迭代会持续进行,直到解的精度达到预设标准或迭代次数达到最大限制。 在使用C++进行编程实现时,主要步骤包括: 1. 初始化阶段:设定初始解向量x(0),并明确迭代过程中的参数,例如最大迭代次数和容许的误差界限。 2. 构建雅克比矩阵:依据矩阵A的非对角元素来形成J矩阵。 3. 迭代计算:依照上述迭代公式计算新的解向量,并验证是否满足终止条件(即当前解与前一次解的差值小于设定的误差界限)。 4. 结果输出...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值