2. Threads, Tasks, and Shared Memory (in Modern C++)


1. A bit of C++ History

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2. Thread Management in C++

在这里插入图片描述

2.1 Creating a Thread

在这里插入图片描述

2.2 Starting Threads

在这里插入图片描述

2.3 Wait and Detach

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.4 Pass Arguments

在这里插入图片描述
在这里插入图片描述

2.5 Ownership in C++

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3. Tasks

在这里插入图片描述

3.1 Task Parallelism

在这里插入图片描述

3.2 Data Parallelism

在这里插入图片描述

3.3 Async Tasks

在这里插入图片描述

4. Sharing Data between Threads

4.1 Shared Address Space Model

在这里插入图片描述
在这里插入图片描述

4.2 Cache Coherence and Memory Consistence

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

4.3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To implement a shared resource pool in C++, you can modify the previous example by introducing shared ownership of resources using smart pointers. Here's an updated version: ```cpp #include <iostream> #include <queue> #include <mutex> #include <condition_variable> #include <memory> template<typename T> class ResourcePool { public: ResourcePool(size_t maxSize) : max_size(maxSize), available_resources(0) {} std::shared_ptr<T> acquire() { std::unique_lock<std::mutex> lock(mutex); while (resources.empty()) { if (available_resources >= max_size) { // Wait until a resource is released cv.wait(lock); } else { // Create a new resource if the pool is not full resources.push(createResource()); ++available_resources; } } std::shared_ptr<T> resource = resources.front(); resources.pop(); return resource; } void release(std::shared_ptr<T> resource) { std::lock_guard<std::mutex> lock(mutex); resources.push(resource); cv.notify_one(); } private: size_t max_size; size_t available_resources; std::queue<std::shared_ptr<T>> resources; std::mutex mutex; std::condition_variable cv; // Create a new resource object (replace with your own code) std::shared_ptr<T> createResource() { return std::make_shared<T>(); } }; // Example usage class MyResource { public: void use() { std::cout << "Using resource" << std::endl; } }; int main() { ResourcePool<MyResource> pool(5); // Acquire and use resources for (int i = 0; i < 10; ++i) { std::shared_ptr<MyResource> resource = pool.acquire(); resource->use(); pool.release(resource); } return 0; } ``` In this updated example, the `ResourcePool` class now uses `std::shared_ptr` to manage shared ownership of resources. When acquiring a resource, a `std::shared_ptr` is returned instead of a raw pointer. This allows multiple threads or parts of the code to safely share and use the resource without worrying about premature destruction. When releasing a resource, a `std::shared_ptr` is passed back to the pool instead of a raw pointer. Note that the `createResource` function now uses `std::make_shared` to create the resource object, which ensures that the object is properly managed by a shared pointer. By using shared pointers, you can safely share and manage resources within the resource pool, allowing multiple parts of your code to use them without concerns about memory ownership and deallocation.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值