ch3_4 多线程中的同步与合作

ch3_4 多进程中的同步与合作

信号量的介绍
信号量(Semaphore),有时被称为信号灯,是在多线程环境下使用的一种设施,是可以用来保证两个或多个关键代码段不被并发调用。

在进入一个关键代码段之前,线程必须获取一个信号量;一旦该关键代码段完成了,那么该线程必须释放信号量。其它想进入该关键代码段的线程必须等待直到第一个线程释放信号量。

为了完成这个过程,需要创建一个信号量VI,然后将Acquire Semaphore VI以及Release Semaphore VI分别放置在每个关键代码段的首末端。确认这些信号量VI引用的是初始创建的信号量。

信号量本质是一个非负的整型变量。

1. 信号量

信号量是计算机科学和操作系统中使用的一种同步机制,用于控制多个进程或线程对共享资源的访问。它有助于防止竞争条件并确保一次只有一个进程可以访问共享资源。

P 操作是用在进入共享资源之前,V 操作是用在离开共享资源之后,
这两个操作是必须成对出现的。

获取一个资源资源后执行减一,也称为P操作:
增加一个可用资源执行加一,也称为V操作;

1.1 P 操作

P(Proberen)操作:当进程或线程想要访问共享资源时,它对与该资源关联的信号量执行 P 操作。如果信号量的值大于零,则会递减该值并继续访问资源。如果信号量的值为零或小于零,它将阻塞或等待,直到该值变得大于零,表明资源可用。

1.2 V操作

V(Verhogen)操作:当进程或线程使用完共享资源后,它会对信号量执行 V 操作。此操作会增加信号量的值,可能允许另一个进程访问该资源。

通过使用这些 P 和 V 操作,进程或线程可以协调对共享资源的访问,避免冲突并确保以同步方式控制资源访问。

1.3信号量的两种类型

根据信号值不同分为两类:

  • 二值信号量,信号量值只有0和1,初始值为1,1表示资源可用,0表示资源不可用;二值信号量与互斥锁类似。

  • 计数信号量, 信号量的值在0到一个大于1的限制值之间,信号值表示可用的资源的数目。

使用二值信号量 完成互斥 功能;

进入临界区 的互斥操作,
在这里插入图片描述
使用二值信号量,完成同步的功能:

如下, 信号量初值为0,
通过如下方式, 确保了线程A condition->P() 之后的语句必须 等到 Thread B , condition->V 之前的语句完成 之后, 线程A condition->P() 之后的语句才能继续执行,

原因是, 线程A condition->P() 执行后, 信号量的值从0减1, 小于0, 由于小于0代表了当前进程所需要的资源不可用, 从而导致当前进程进入阻塞等待,

那等待到什么时候呢?
此时,线程B 也在执行, 当执行到 condition->V 后,信号量加1,
信号量在加1 之后, 线程A 中condition->P() 之后的语句才可以继续执行。

在这里插入图片描述

根据作用对象不同分为两类:

  • 有名信号量,信号值保存在文件中,用于进程间同步
  • 无名信号量,又称为基于内存信号量,信号值保存在内存中,用于线程间同步

2. 生产者与消费者

2.1

在这里插入图片描述

在这里插入图片描述

2.2

2.3

3.

3.1

3.2

3.3

4. C++和Python的多线程

C++和Python都支持多线程,允许程序同时执行多个线程。然而,C++ 和 Python 中多线程的实现和管理方式存在显着差异:

Language Type:语言类型:

C++: Multithreading in C++ is typically done using the Standard Library’s and headers. C++ supports low-level thread management and provides more direct control over threads.C++:C++ 中的多线程通常使用标准库的 和 标头来完成。 C++ 支持低级线程管理并提供对线程更直接的控制。
Python: Python has a Global Interpreter Lock (GIL) in CPython (the reference implementation). This means that only one thread can execute Python code at a time, making Python threads less suitable for CPU-bound tasks. Python threads are best used for I/O-bound tasks or for running concurrent tasks that spend a lot of time waiting for external resources.Python:Python 在 CPython(参考实现)中有一个全局解释器锁(GIL)。这意味着一次只有一个线程可以执行 Python 代码,使得 Python 线程不太适合 CPU 密集型任务。 Python 线程最适合用于 I/O 密集型任务或运行花费大量时间等待外部资源的并发任务。
Concurrency Models:并发模型:

C++: C++ supports both native threads (std::thread) and platform-specific thread APIs (e.g., WinAPI or POSIX threads). It allows you to create and manage threads at a low level and provides better control over thread management.C++:C++ 支持本机线程 (std::thread) 和特定于平台的线程 API(例如 WinAPI 或 POSIX 线程)。它允许您在较低级别创建和管理线程,并提供对线程管理的更好控制。
Python: Python’s threading module provides a simple and consistent interface for creating and managing threads, but due to the GIL, it is not well-suited for CPU-bound tasks. For CPU-bound tasks, you may use the multiprocessing module or other third-party libraries like concurrent.futures.Python:Python 的线程模块提供了一个简单且一致的接口来创建和管理线程,但由于 GIL,它不太适合 CPU 密集型任务。对于CPU密集型任务,您可以使用多处理模块或其他第三方库,例如concurrent.futures。
GIL (Global Interpreter Lock):GIL(全局解释器锁):

C++: C++ doesn’t have a GIL. Multiple threads can run in parallel, and you have the freedom to use multiple CPU cores efficiently.C++:C++ 没有 GIL。多个线程可以并行运行,您可以自由高效地使用多个 CPU 核心。
Python: In CPython, the GIL ensures that only one thread can execute Python bytecode at a time. This can limit the potential performance gains from using multiple threads in CPU-bound applications.Python:在 CPython 中,GIL 确保一次只有一个线程可以执行 Python 字节码。这可能会限制在 CPU 密集型应用程序中使用多个线程所带来的潜在性能提升。
Performance:表现:

C++: C++ offers more control over memory management and thread synchronization, making it well-suited for high-performance multithreaded applications.C++:C++ 提供了对内存管理和线程同步的更多控制,使其非常适合高性能多线程应用程序。
Python: Python threads are better for I/O-bound tasks and can help improve the responsiveness of I/O-heavy applications. However, due to the GIL, Python’s multithreading is less efficient for CPU-bound tasks.Python:Python 线程更适合 I/O 密集型任务,有助于提高 I/O 密集型应用程序的响应能力。然而,由于 GIL,Python 的多线程处理 CPU 密集型任务的效率较低。
Thread Safety:线程安全:

C++: In C++, you have to manage thread safety explicitly using mechanisms like mutexes, condition variables, and atomic operations.C++:在 C++ 中,您必须使用互斥体、条件变量和原子操作等机制显式管理线程安全。
Python: Python provides a higher-level thread synchronization interface through the threading module, which simplifies some aspects of thread safety.Python:Python通过threading模块提供了更高级的线程同步接口,简化了线程安全的某些方面。
Library Ecosystem:图书馆生态系统:

C++: C++ has a vast ecosystem of libraries and tools for multithreaded and parallel programming, including the C++ Standard Library and third-party libraries.C++:C++ 拥有庞大的用于多线程和并行编程的库和工具生态系统,包括 C++ 标准库和第三方库。
Python: Python has libraries like concurrent.futures for high-level threading and multiprocessing, as well as third-party libraries for more advanced concurrency control.Python:Python 拥有用于高级线程和多重处理的库,例如 concurrent.futures ,以及用于更高级并发控制的第三方库。
In summary, C++ offers low-level control over multithreading and is suitable for a wide range of applications, including high-performance computing. Python, due to the GIL, is generally better for I/O-bound tasks and benefits more from multiprocessing for CPU-bound tasks. However, the choice between C++ and Python depends on the specific requirements of your project and your expertise with each language.总之,C++ 提供了对多线程的低级控制,适用于各种应用程序,包括高性能计算。由于 GIL,Python 通常更适合 I/O 密集型任务,并且从 CPU 密集型任务的多处理中获益更多。但是,C++ 和 Python 之间的选择取决于您项目的具体要求以及您对每种语言的专业知识。

ref

read1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值