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

网站建设怎么申请域名超级外链吧

网站建设怎么申请域名,超级外链吧,深圳搜索优化排名公司,大连专业制作网站C std 多线程教程 理解多线程的概念 多线程是一种并发编程技术,它允许程序同时运行多个任务。每个线程共享同一进程的资源(如内存),但拥有独立的执行路径。多线程编程在现代 C 中变得更加便捷和安全,标准库提供了强大…

C++ std 多线程教程


理解多线程的概念

多线程是一种并发编程技术,它允许程序同时运行多个任务。每个线程共享同一进程的资源(如内存),但拥有独立的执行路径。多线程编程在现代 C++ 中变得更加便捷和安全,标准库提供了强大的多线程支持,包括线程创建、同步和管理。

多线程的优点
  1. 提高性能:充分利用多核 CPU 的并行计算能力。
  2. 异步操作:后台任务(如文件处理、网络通信)可以在不阻塞主线程的情况下运行。
  3. 简化复杂任务:将复杂任务分解为多个线程独立处理。
C++ 提供的多线程支持

C++11 开始标准库新增了 <thread> 模块,用于线程管理,并提供同步机制(如互斥锁、条件变量)。


1. C++ 多线程基础

1.1 创建线程

通过 std::thread 创建线程。线程可以执行:

  1. 函数。
  2. Lambda 表达式。
  3. 成员函数。
示例
#include <iostream>
#include <thread>void printMessage() {std::cout << "Hello from thread!" << std::endl;
}int main() {std::thread t(printMessage); // 创建线程 tt.join(); // 等待线程 t 执行完毕return 0;
}

1.2 线程的生命周期

  1. 创建:通过 std::thread 创建新线程。
  2. 加入
    • join():主线程等待子线程完成。
    • detach():分离线程,子线程独立运行。
  3. 结束:线程完成任务或被终止。
示例:detachjoin
#include <iostream>
#include <thread>void detachedThread() {std::cout << "Detached thread running..." << std::endl;
}void joinedThread() {std::cout << "Joined thread running..." << std::endl;
}int main() {std::thread t1(detachedThread);t1.detach(); // 分离线程,主线程不再管理它std::thread t2(joinedThread);t2.join(); // 主线程等待 t2 完成std::cout << "Main thread finished." << std::endl;return 0;
}

1.3 Lambda 表达式作为线程函数

示例
#include <iostream>
#include <thread>int main() {std::thread t([] {std::cout << "Hello from Lambda!" << std::endl;});t.join();return 0;
}

1.4 传递参数到线程

通过 std::thread 构造函数将参数传递给线程函数。

示例
#include <iostream>
#include <thread>void printNumbers(int n, const std::string& message) {for (int i = 0; i < n; i++) {std::cout << message << " " << i << std::endl;}
}int main() {std::thread t(printNumbers, 5, "Count"); // 传递参数 5 和 "Count"t.join();return 0;
}

2. 线程同步

多线程共享资源时,可能会导致数据竞争(race condition)。C++ 提供了多种同步机制来解决这些问题。


2.1 互斥锁(std::mutex

示例:保护共享资源
#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx; // 定义一个互斥锁
int counter = 0;void increment() {for (int i = 0; i < 100; i++) {std::lock_guard<std::mutex> lock(mtx); // 自动加锁和解锁counter++;}
}int main() {std::thread t1(increment);std::thread t2(increment);t1.join();t2.join();std::cout << "Final counter value: " << counter << std::endl;return 0;
}

2.2 条件变量(std::condition_variable

条件变量用于线程间通信和同步。

示例:生产者-消费者模型
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>std::queue<int> q;
std::mutex mtx;
std::condition_variable cv;void producer() {for (int i = 0; i < 10; i++) {std::unique_lock<std::mutex> lock(mtx);q.push(i);std::cout << "Produced: " << i << std::endl;cv.notify_one(); // 通知消费者}
}void consumer() {for (int i = 0; i < 10; i++) {std::unique_lock<std::mutex> lock(mtx);cv.wait(lock, [] { return !q.empty(); }); // 等待队列非空int item = q.front();q.pop();std::cout << "Consumed: " << item << std::endl;}
}int main() {std::thread t1(producer);std::thread t2(consumer);t1.join();t2.join();return 0;
}

3. 多线程的Linux Demo

以下示例演示了如何在 Linux 环境下编写一个多线程程序,该程序计算数组的部分和,并最终输出总和。

完整代码
#include <iostream>
#include <thread>
#include <vector>
#include <numeric>
#include <functional>void partialSum(const std::vector<int>& data, int start, int end, int& result) {result = std::accumulate(data.begin() + start, data.begin() + end, 0);std::cout << "Partial sum [" << start << ", " << end << ") = " << result << std::endl;
}int main() {const int size = 100;std::vector<int> data(size);std::iota(data.begin(), data.end(), 1); // 初始化数组 [1, 2, ..., 100]int numThreads = 4;int blockSize = size / numThreads;std::vector<std::thread> threads;std::vector<int> results(numThreads);// 启动线程for (int i = 0; i < numThreads; i++) {int start = i * blockSize;int end = (i == numThreads - 1) ? size : start + blockSize;threads.emplace_back(partialSum, std::cref(data), start, end, std::ref(results[i]));}// 等待线程完成for (auto& t : threads) {t.join();}// 计算总和int totalSum = std::accumulate(results.begin(), results.end(), 0);std::cout << "Total sum: " << totalSum << std::endl;return 0;
}

运行示例

在 Linux 环境中编译和运行:

g++ -std=c++11 -pthread -o multithreading_demo multithreading_demo.cpp
./multithreading_demo

结果示例

Partial sum [0, 25) = 325
Partial sum [25, 50) = 950
Partial sum [50, 75) = 1575
Partial sum [75, 100) = 2200
Total sum: 5050

学习建议

  1. 理解线程的生命周期:掌握线程的创建、管理和结束。
  2. 同步机制:熟悉 std::mutexstd::condition_variable 的使用。
  3. 多线程调试:在 Linux 中使用工具(如 gdb)调试多线程程序。

通过这些内容,你可以编写高效的 C++ 多线程程序,并在实际项目中灵活运用!

http://www.hrbkazy.com/news/45316.html

相关文章:

  • 西安网站建设成功建设电商营销推广有哪些?
  • 京东可以做特效的网站缅甸在线今日新闻
  • 济南定制网站建设公司seo优化的价格
  • 湖北做网站价格成都网站快速排名
  • 企业做自己的网站要注意什么什么是论坛推广
  • 龙华区住房和建设局网站电商培训班一般多少钱
  • 河南营销型网站百度会员登录入口
  • 商城网站制作的教程seo服务公司
  • 贷款网站开发做seo需要投入的成本
  • jsp动态网站开发考试题重庆网站推广软件
  • 有域名之后怎么自己做网站站内seo优化
  • 上海工程相关的公司广州网站优化排名
  • 科技网站设计公司网络营销的基本职能
  • 建立公司网站的好处网络营销特点
  • 定制管理系统软件郑州seo公司哪家好
  • 专业建设网站应该怎么做百度账户登录
  • 做的网站缩小内容就全乱了网站搭建
  • 娱乐网站建设武汉seo收费
  • 网站建设按期没有完成能要回定金吗网络营销师证书含金量
  • 网站建设中需求分析说明书成都企业网站seo技术
  • 西安好的皮肤管理做团购网站微博指数
  • 做百度商桥网站友情链接价格
  • 狮山建网站长沙本地推广
  • 免费做网站整站seo排名外包
  • 网站规划建设书如何优化网络延迟
  • 深圳市做网站知名公司百度游戏中心app
  • 外贸专业网站ui培训
  • 青山别墅网页制作教程潍坊seo按天收费
  • 怎么看网站什么时候做的百度认证是什么
  • 漳州做网站公司百度竞价排名公式