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

企业网站一般做哪些栏目seo网站优化经理

企业网站一般做哪些栏目,seo网站优化经理,网站开发好学不,做外包哪个网站好一些现代C&#xff08;C11及其之后的版本&#xff09;引入了标准的线程支持库&#xff0c;使得多线程编程变得更加简单和可移植。这个库提供了线程管理、互斥量、条件变量和其他同步原语。 1. std::thread - 基本线程 std::thread允许创建执行特定任务的线程。 #include <ios…

在这里插入图片描述

现代C++(C++11及其之后的版本)引入了标准的线程支持库,使得多线程编程变得更加简单和可移植。这个库提供了线程管理、互斥量、条件变量和其他同步原语。

1. std::thread - 基本线程

std::thread允许创建执行特定任务的线程。

#include <iostream>
#include <thread>void helloFunction() {std::cout << "Hello from thread!" << std::endl;
}void basicThread() {std::thread t(helloFunction);t.join();  // 等待线程完成
}

2. 传递参数给线程函数

线程函数可以接受参数,和普通函数一样。

#include <iostream>
#include <thread>void printMessage(const std::string& message) {std::cout << message << std::endl;
}void threadWithArguments() {std::thread t(printMessage, "Hello from thread with argument!");t.join();
}

3. std::mutex - 互斥量

互斥量用于同步对共享资源的访问。

#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void printBlocked(const std::string& message) {mtx.lock();std::cout << message << std::endl;mtx.unlock();
}void mutexExample() {std::thread t1(printBlocked, "Thread 1");std::thread t2(printBlocked, "Thread 2");t1.join();t2.join();
}

4. std::lock_guard - 自动管理互斥量

std::lock_guard提供了一种便捷的RAII风格的方式来自动上锁和解锁互斥量。

#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void safePrint(const std::string& message) {std::lock_guard<std::mutex> lock(mtx);std::cout << message << std::endl;// 互斥量在lock_guard对象被销毁时自动解锁
}void lockGuardExample() {std::thread t1(safePrint, "Thread 1 with lock_guard");std::thread t2(safePrint, "Thread 2 with lock_guard");t1.join();t2.join();
}

5. std::asyncstd::future - 异步执行

std::async允许异步执行函数,并通过std::future获取结果。

#include <iostream>
#include <future>int compute() {return 42;  // 模拟计算
}void asyncExample() {std::future<int> result = std::async(compute);std::cout << "The answer is " << result.get() << std::endl;
}

6. std::condition_variable - 条件变量

条件变量用于线程间的同步,允许线程在特定条件下等待或通知其他线程。

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>std::mutex mtx;
std::condition_variable cv;
bool ready = false;void printId(int id) {std::unique_lock<std::mutex> lock(mtx);while (!ready) cv.wait(lock);std::cout << "Thread " << id << std::endl;
}void go() {std::unique_lock<std::mutex> lock(mtx);ready = true;cv.notify_all();
}void conditionVariableExample() {std::thread threads[10];for (int i = 0; i < 10; ++i) {threads[i] = std::thread(printId, i);}std::cout << "10 threads ready to race..." << std::endl;go();for (auto& t : threads) {t.join();}
}
http://www.hkea.cn/news/3899/

相关文章:

  • 郑州网站制作报价百度快速排名用是
  • 东莞凤岗网站建设邯郸seo优化
  • 淘宝网站推广策划方案sem推广托管公司
  • ps网站子页怎么做网络营销制度课完整版
  • wordpress标签tag文章百度广告优化师
  • 专业的网站建设费用网站排名查询软件
  • 网站注册人查询全国新冠疫苗接种率
  • 阿里云虚拟主机与网站吗近三天发生的重要新闻
  • 做家教网站要多少钱东莞全网营销推广
  • 天津网站建设方案友情链接交换工具
  • 权威的网站制作上海网站推广公司
  • 北京网站建设公司现状网站收录量
  • b2c网站的认识市场营销方案怎么写
  • 中国公路工程建设网站淘宝搜索关键词技巧
  • 空间中国网站地址多少网络推广与优化
  • 东莞定制网站开发石家庄网络关键词排名
  • 如何做网站搭桥链接百度快照客服电话
  • cdn wordpress 统计扬州整站seo
  • 如何做自己的电影网站焊工培训
  • 网站建设 福田上海网站排名优化怎么做
  • 地产渠道12种拓客方式上海专业的seo公司
  • 日照哪里做网站电商网站网址
  • 武汉 网站建设下载谷歌浏览器并安装
  • 上海公安门户网站官网下载中心网站推广公司
  • 中国建设网站银行卡青岛网站制作seo
  • 帮别人做ppt赚钱的网站淘特app推广代理
  • 合肥网站建设=388元杭州seo排名收费
  • 企业所得税税率2022年最新税率表seo快速排名代理
  • 商品网站做推广方案提交网站收录入口
  • 建设常规的网站报价是多少建网站免费