当前位置: 首页 > wzjs >正文

网站开发要多长时间哪些平台可以打小广告

网站开发要多长时间,哪些平台可以打小广告,网站域名信息查询,互联网创业项目名称C17 信号量模拟实现 一、实现原理 C17 标准库没有原生信号量(C20才有)&#xff0c;但可以通过 std::mutex std::condition_variable 模拟实现。以下是核心逻辑&#xff1a; #include <mutex> #include <condition_variable>class CountingSemaphore { private:…

C++17 信号量模拟实现

一、实现原理

C++17 标准库没有原生信号量(C++20才有),但可以通过 std::mutex + std::condition_variable 模拟实现。以下是核心逻辑:

#include <mutex>
#include <condition_variable>class CountingSemaphore {
private:int count_;                  // 当前可用资源数std::mutex mutex_;           // 保护计数器的锁std::condition_variable cv_; // 阻塞/唤醒线程public:explicit CountingSemaphore(int initial = 0) : count_(initial) {}// P操作:请求资源(计数器减1)void acquire() {std::unique_lock<std::mutex> lock(mutex_);cv_.wait(lock, [this] { return count_ > 0; }); // 等待资源可用--count_;}// V操作:释放资源(计数器加1)void release() {std::lock_guard<std::mutex> lock(mutex_);++count_;cv_.notify_one(); // 唤醒一个等待线程}
};
二、完整可编译代码
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
#include <chrono>// 信号量实现类
class CountingSemaphore {
private:int count_;std::mutex mutex_;std::condition_variable cv_;
public:explicit CountingSemaphore(int initial = 0) : count_(initial) {}void acquire() {std::unique_lock<std::mutex> lock(mutex_);cv_.wait(lock, [this] { return count_ > 0; });--count_;}void release() {std::lock_guard<std::mutex> lock(mutex_);++count_;cv_.notify_one();}
};// 全局资源
constexpr int MAX_BUFFER = 5;
CountingSemaphore empty_slots(MAX_BUFFER); // 初始空位
CountingSemaphore data_items(0);           // 初始数据项
std::mutex buffer_mutex;                   // 保护缓冲区
std::queue<int> buffer;                     // 共享缓冲区
bool producer_done = false;                // 生产结束标志// 生产者函数
void producer() {for (int i = 1; i <= 10; ++i) {empty_slots.acquire(); // 等待空位{std::lock_guard<std::mutex> lock(buffer_mutex);buffer.push(i);std::cout << "生产者添加: " << i << std::endl;}data_items.release(); // 增加数据项std::this_thread::sleep_for(std::chrono::milliseconds(100));}// 标记生产完成std::lock_guard<std::mutex> lock(buffer_mutex);producer_done = true;
}// 消费者函数
void consumer() {while (true) {data_items.acquire(); // 等待数据项{std::lock_guard<std::mutex> lock(buffer_mutex);if (producer_done && buffer.empty()) break; // 退出条件int val = buffer.front();buffer.pop();std::cout << "消费者取出: " << val << std::endl;}empty_slots.release(); // 释放空位std::this_thread::sleep_for(std::chrono::milliseconds(150));}
}int main() {std::thread t1(producer);std::thread t2(consumer);t1.join();t2.join();std::cout << "程序正常退出" << std::endl;return 0;
}
三、编译与运行
  1. 编译命令(需要支持C++17的编译器):

    g++ -std=c++17 -pthread -o semaphore_demo semaphore_demo.cpp
    
  2. 运行输出示例

四、关键机制解析
组件作用
std::mutex保护共享计数器 count_ 的线程安全访问
std::condition_variable实现阻塞等待和唤醒机制
cv_.wait()阻塞线程直到资源可用(count_ > 0
cv_.notify_one()资源释放后唤醒一个等待线程
五、注意事项
  1. 虚假唤醒处理cv_.wait() 必须使用循环判断条件

    cv_.wait(lock, [this] { return count_ > 0; }); // 正确写法
    
  2. 死锁避免:确保每次 acquire() 都有对应的 release()

  3. 性能优化:高频场景下可改用 notify_all() + 线程竞争

    void release() {std::lock_guard<std::mutex> lock(mutex_);++count_;cv_.notify_all(); // 唤醒所有线程
    }
    

C语言的信号量

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>sem_t sem;
int shared_counter = 0;void* thread_func(void* arg) {for (int i = 0; i < 100000; i++) {sem_wait(&sem);       // P操作shared_counter++;     // 临界区sem_post(&sem);       // V操作}return NULL;
}int main() {// 初始化信号量(初始值为1)if (sem_init(&sem, 0, 1) != 0) {perror("sem_init failed");return 1;}pthread_t t1, t2;// 创建线程1if (pthread_create(&t1, NULL, thread_func, NULL) != 0) {perror("pthread_create t1 failed");return 1;}// 创建线程2if (pthread_create(&t2, NULL, thread_func, NULL) != 0) {perror("pthread_create t2 failed");return 1;}// 等待线程结束pthread_join(t1, NULL);pthread_join(t2, NULL);printf("Final counter value: %d\n", shared_counter);  // 正确应为200000// 销毁信号量sem_destroy(&sem);return 0;
}
http://www.dtcms.com/wzjs/203275.html

相关文章:

  • 微信网站开发技术磁力神器
  • 英文商城网站建设免费个人网站申请
  • 苏州建网站成都网站建设方案服务
  • 网站域名被注册seo评测论坛
  • 学历提升咨询seo网站外链工具
  • 云主机网站模板seo图片优化
  • 建一个公司网站要多久怎么免费建立网站
  • 旅游电商网站有哪些长沙官网优化公司
  • 萧山网站建设争锋网络怎么弄一个网站平台
  • 网站建设项目功能需求分析报告老铁seo外链工具
  • 做网站要多少人5月疫情第二波爆发
  • 国税局网站里打印设置如何做广告推广免费
  • 上海十大工业设计公司百度关键词优化曝光行者seo
  • 做基础销量的网站站长资源平台
  • 做网站用母版页不好么关键词排名什么意思
  • seo是怎么优化seo网站设计
  • 黄冈做网站深圳货拉拉
  • 新网站内部优化怎么做百度网址大全网站大全
  • 分类信息网站如何建设站长工具怎么关掉
  • 浏览器怎么做能不拦截网站东莞市网络seo推广价格
  • 网站怎么做浏览量才会多市场营销策划方案模板
  • 网站开发如何共用菜单栏友情链接交换网站
  • 网站无备案铜川网站seo
  • 网站禁ping廊坊网站推广公司
  • 网站设计的指导思想百度霸屏推广多少钱一个月
  • 福建省建设行业企业资质查询网站关于进一步优化当前疫情防控措施
  • 网站建设论文的部首百度推广怎么操作流程
  • 动态网站技术企业站seo价格
  • 成都网站设计师广告搜索引擎
  • 专业微网站建设公司首选网络营销策划方案书范文