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

数码网站建设论文免费源码资源源码站

数码网站建设论文,免费源码资源源码站,做微信商城网站,做设计的一般用什么网站找素材1.介绍 在C中&#xff0c;线程是实现并行开发的核心工具之一。C11引入了标准库<thread>&#xff0c;提供了对多线程编程的支持。通过线程&#xff0c;程序可以同时执行多个任务&#xff0c;从而提高性能和响应速度。 线程池介绍在这篇博客——C之线程池&#xff08;Threa…

1.介绍

        在C++中,线程是实现并行开发的核心工具之一。C++11引入了标准库<thread>,提供了对多线程编程的支持。通过线程,程序可以同时执行多个任务,从而提高性能和响应速度。

        线程池介绍在这篇博客——C++之线程池(Thread Pool)-CSDN博客

2.基本概念

        线程——是操作系统能够调度的最小执行单位。一个进程可以包含多个进程,所有线程共享进程的内存空间。

        并发——多个线程同时执行,但可能交替占用CPU资源。

        并行——多个线程同时执行,且同时占用多个CPU资源。

3.C++中的线程库

        C++11引入了<thread>头文件,提供了以下核心功能:

        thread——用于创建于管理线程。

        mutex——用于实现线程间的互斥锁。

        condition_variable——用于实现线程间的条件变量。

        future和promise——用于异步任务的结果获取。

4.用法

        (1)创建线程。使用thread创建线程,并指定线程执行的函数。例如:

#include <iostream>
#include <thread>void threadFunction() {std::cout << "Hello from thread!" << std::endl;
}int main() {// 创建线程并执行 threadFunctionstd::thread t(threadFunction);// 等待线程结束,回收t.join();std::cout << "Main thread finished." << std::endl;return 0;
}

        通过上面的简单例子,可以知道如何创建线程并指定线程执行的函数。

        (2)线程的参数传递。可以向线程函数传递参数。例如:

#include <iostream>
#include <thread>void printMessage(const std::string& message) {std::cout << "Message: " << message << std::endl;
}int main() {std::string msg = "Hello, World!";std::thread t(printMessage, msg);t.join();return 0;
}

        如果需要引用传递,可以使用std::ref。

     (3)线程的同步。多个线程访问共享资源时,需要使用同步机制避免竞争条件。

        1.互斥锁(mutex)

#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void printBlock(char c) {std::unique_lock<std::mutex> lock(mtx);for (int i = 0; i < 10; ++i) {std::cout << c;}std::cout << std::endl;
}int main() {std::thread t1(printBlock, '*');std::thread t2(printBlock, '$');t1.join();t2.join();return 0;
}

        mutex——用于保护共享资源。

        unique_lock——自动管理锁的生命周期。

        2.条件变量(condition_variable)

#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>std::mutex mtx;
std::condition_variable cv;
bool ready = false;void printMessage(int id) {std::unique_lock<std::mutex> lock(mtx);cv.wait(lock, [] { return ready; });std::cout << "Thread " << id << " is running." << std::endl;
}int main() {std::thread threads[3];for (int i = 0; i < 3; ++i) {threads[i] = std::thread(printMessage, i);}std::this_thread::sleep_for(std::chrono::seconds(1));{std::unique_lock<std::mutex> lock(mtx);ready = true;}cv.notify_all();for (auto& t : threads) {t.join();}return 0;
}

        condition_variable:用于线程间的条件同步。三个线程都阻塞在cv.wait。

        cv.wait(lock,predicate):等待条件满足。阻塞期间会释放锁。

        cv.notify_all():通知所有等待线程。

        上边程序的运行流程是三个线程调用执行函数,阻塞等待,等待1s后,加锁,条件满足继续执行后续函数,从而实现三个线程同步执行。

        (4)异步任务。使用 std::async和 std::future实现异步任务。

#include <iostream>
#include <future>int computeSum(int a, int b) {return a + b;
}int main() {std::future<int> result = std::async(computeSum, 10, 20);std::cout << "Result: " << result.get() << std::endl;return 0;
}

        std::async:启动一个异步任务。作用就是避免阻塞主线程。

        std::future:用于获取异步任务的结果。

5.总结

  • C++11 提供了强大的多线程支持,包括线程创建、互斥锁、条件变量和异步任务。

  • 在实际开发中,需要注意线程安全和资源管理问题。

如有错误,敬请指正!!!

http://www.dtcms.com/wzjs/180292.html

相关文章:

  • 做预算的网站附近电商培训班
  • 做公司网站需要提供的资料百度seo咋做
  • 网站优化哪里好网络seo推广培训
  • 珠海微网站哈尔滨优化网站公司
  • 乐陵seo排名企业网站优化解决方案
  • app软件免费模板下载网站成品网站源码1688免费推荐
  • 搭建网站教程网络推广员是干什么的
  • 余姚公司网站建设常州百度推广代理公司
  • 网站上如何做天气插件seo优化什么意思
  • 做网站的叫什么软件网店推广渠道有哪些
  • 专业做球赛旅游的网站足球排名世界排名
  • python 网站开发代码免费一键生成个人网站
  • 邛崃建设局网站seo关键词软件
  • 湖南建设厅网站外贸网站平台都有哪些 免费的
  • 谷城网站建设最近发生的热点新闻事件
  • 网站开发上市公司查收录网站
  • 涪城网站建设2022小说排行榜百度风云榜
  • 企业网站建设心得怎么收录网站
  • 定制一款appseo优化排名技术百度教程
  • 宠物网站建设毕业论文2022拉新推广赚钱的app
  • 苹果手机免费做ppt模板下载网站有哪些网站推广的作用
  • 招商外包服务公司保定seo网络推广
  • 东莞建外贸网站提高工作效率的句子
  • 网站建设方案备案内部优化
  • 网站管理 上传模板免费引流推广方法
  • 开封网站建设中层管理者培训课程有哪些
  • 大连免费建站模板安卓在线视频嗅探app
  • 如何做影视网站搜索引擎营销的基本方法
  • ps可以在哪个网站上做兼职seo网络排名优化技巧
  • 哪里培训做网站广州竞价托管代运营