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

网站建站的尺寸软件工程课程设计课程网站建设

网站建站的尺寸,软件工程课程设计课程网站建设,重庆市建筑工程造价信息网,如何做亚马逊国外网站总结 多线程访问共享数据时需要加锁 多线程数据竞争 假如有一个变量shared_variable被10个线程共享#xff0c;每个线程在循环中对shared_variable进行 1000 次累加操作#xff0c;我们期望最终值为10000。 #include iostream #include thread #include …总结 多线程访问共享数据时需要加锁 多线程数据竞争 假如有一个变量shared_variable被10个线程共享每个线程在循环中对shared_variable进行 1000 次累加操作我们期望最终值为10000。 #include iostream #include thread #include vectorint shared_variable 0; // 共享变量void thread_function() {for (int i 0; i 1000; i) {// 模拟长延迟增加竞争机会std::this_thread::sleep_for(std::chrono::milliseconds(1));shared_variable;} }int main() {const int num_threads 10;std::vectorstd::thread threads;// 创建多个线程for (int i 0; i num_threads; i) {threads.emplace_back(thread_function);}// 等待所有线程完成for (auto t : threads) {t.join();}std::cout Final value: shared_variable std::endl; // 输出最终结果return 0; }实际运行我们发现shared_variable值通常会小于 10000并且每次运行结果不同。这是因为多个线程同时读取和修改shared_variable导致其值被错误覆盖。shared_variable是一个非原子操作包含三步 读取当前值。加 1。写回新值。 在多线程环境中这三步之间可能会被其他线程打断导致结果不一致。例如 线程 A 读取到值 100准备加 1。线程 B 同时读取到值 100也准备加 1。线程 A 写回值 101。线程 B 写回值 101假设线程 B 未看到线程 A 的修改。 期望的值应为 102但实际上变为 101导致错误。这就是在多线程编程中不使用锁导致数据竞争的典型问题。 使用锁同步 List item 锁是一种同步机制用于协调多个线程对共享资源的访问。它类似于现实生活中的锁可以确保一次只有一线程能访问特定的资源其他线程必须等待锁被释放才能继续执行。在多线程编程中锁的主要作用是防止数据竞争和一致性问题确保对共享数据的正确操作和访问顺序。 #include iostream #include thread #include vector #include mutexstd::mutex mtx; // 互斥锁 int shared_variable 0; // 共享变量void thread_function() {for (int i 0; i 1000; i) { std::lock_guardstd::mutex lock(mtx); // 获取锁shared_variable; // 模拟长延迟增加竞争机会std::this_thread::sleep_for(std::chrono::milliseconds(1));} }int main() {const int num_threads 10;std::vectorstd::thread threads;// 创建多个线程for (int i 0; i num_threads; i) {threads.emplace_back(thread_function);}// 等待所有线程完成for (auto t : threads) {t.join();}std::cout Final value: shared_variable std::endl; // 输出最终结果return 0; }复合操作中的竞争条件问题 当shared_vector正在被一个线程修改时另一个线程可能读取了无效的内存地址导致内存越界和段错误。例如当多个线程同时执行复合操作时例如先检查 size() 再访问元素仍然需要外部同步因为复合操作本身不是原子的。 if (vec.size() 0) { // 操作1检查条件int value vec.at(0); // 操作2访问元素 }具体的例子如下所示 #include iostream #include vector #include thread #include chronostd::vectorint shared_vector; // 共享的 vector// 写线程不断向 vector 中添加元素 void writer_thread() {for (int i 0; i 1000000; i) {shared_vector.push_back(i); // 1. 线程A添加一个元素,vector不为空std::this_thread::sleep_for(std::chrono::nanoseconds(1)); // 模拟延迟shared_vector.clear(); // 3. 清空 vector} }// 操作线程不断清空 vector 并访问最后一个元素 void reader_thread() {while (true) {if (!shared_vector.empty()) { // 2. 线程B对vector判空std::this_thread::sleep_for(std::chrono::nanoseconds(1)); // 模拟延迟int last_element shared_vector.at(0); // 4. 尝试访问第一个元素可能导致越界访问std::cout Last element: last_element std::endl;}} }int main() {std::thread t1(writer_thread);std::thread t2(reader_thread);t1.join();t2.join();return 0; }这种情况还比较好分析vector在添加元素超过某个size的时候会重新申请一块地址如果另一个线程还在访问之前的地址就会造成非法访问的问题 解决方案就是通过锁来保证复合操作的 std::mutex mtx; std::vectorint vec;if (mtx.lock()) {if (vec.size() 0) { int value vec.at(0);// 处理 value}mtx.unlock(); }条件变量与线程同步 锁解决了数据竞争的问题但是没有解决线程顺序执行的问题例如我们想让3个线程顺序打出ABC可以使用条件变量 #include iostream #include thread #include mutex #include condition_variable// 全局变量 std::mutex mtx; std::condition_variable cv; char current_char a; // 当前应该打印的字符// 打印函数 void print_char(char target_char) {for (int i 0; i 5; i) { // 每个字符打印5次可以根据需要调整std::unique_lockstd::mutex lock(mtx);// 等待轮到自己打印cv.wait(lock, [target_char]() { return current_char target_char; });// 打印字符std::cout target_char std::endl;// 更新当前字符到下一个if (target_char a) {current_char b;} else if (target_char b) {current_char c;} else {current_char a;}// 通知其他线程cv.notify_all();} }int main() {// 创建三个线程std::thread thread_a(print_char, a);std::thread thread_b(print_char, b);std::thread thread_c(print_char, c);// 等待线程完成thread_a.join();thread_b.join();thread_c.join();return 0; }条件变量为什么加锁 条件变量在使用时通常与锁如 std::mutex一起使用主要原因如下 1. 保证数据一致性和线程安全 条件变量用于线程之间的通信和同步当线程等待某个条件时必须确保该条件的状态是被保护起来的。如果没有锁保护在线程等待条件时其他线程可能会修改条件相关的数据导致数据不一致和竞争条件。 例如在生产者 - 消费者问题中消费者线程等待队列非空的条件。如果消费者线程在检查队列是否为空的时候不使用锁保护生产者线程可能会在消费者线程检查的中间过程插入数据导致检查结果不准确。 void consumer() {while (true) {if (!queue.empty()) { // 没有锁保护读取共享变量是不安全的int val queue.front();queue.pop();std::cout Consumed: val std::endl;} else {// 等待队列有数据cv.wait(); // 假设有条件变量 cv}} }问题如果多个线程同时检查 queue.empty()而没有锁保护可能会导致竞争条件因为 queue 的状态可能在检查和操作之间发生变化。 2. 确保条件判断的原子性 在使用条件变量时通常需要进行以下三步操作 锁定互斥锁。检查条件是否成立。如果条件不成立等待条件变量。 通过互斥锁可以确保这三步操作是原子的即在多线程环境下其他线程不能在步骤 2 和 3 之间修改条件相关的数据。 void consumer() {std::unique_lockstd::mutex lock(mtx);while (queue.empty()) { // 使用锁保护并使用 while 避免虚假唤醒cv.wait(lock); // 等待条件变量}int val queue.front();queue.pop();std::cout Consumed: val std::endl; }std::unique_lockstd::mutex lock(mtx); 确保在检查条件和操作队列时其他线程不能访问队列。while 循环确保即使由于虚假唤醒线程在真正条件满足之前不会继续执行。 3. 避免虚假唤醒 条件变量的 wait 方法可能出现虚假唤醒spurious wakeup即线程在没有明确通知的情况下被唤醒。使用 std::unique_lock 和 while 循环可以避免这种情况。 错误示例没有使用锁和 while 循环 void consumer() {if (queue.empty()) {cv.wait(); // 可能出现虚假唤醒}// 虚假唤醒导致进入此处时队列可能仍然为空int val queue.front();queue.pop(); }正确示例 void consumer() {std::unique_lockstd::mutex lock(mtx);while (queue.empty()) { // 即使有虚假唤醒也会重新检查条件cv.wait(lock);}int val queue.front();queue.pop();std::cout Consumed: val std::endl; }4. 确保线程间的可见性 条件变量和锁的组合使用可以确保线程之间的内存可见性。例如当线程 A 修改了某个共享变量并通知条件变量线程 B 通过锁保护可以保证看到线程 A 的修改。 void producer() {{std::lock_guardstd::mutex lock(mtx);queue.push(1); // 修改共享数据}cv.notify_one(); // 通知消费者 }void consumer() {std::unique_lockstd::mutex lock(mtx);while (queue.empty()) {cv.wait(lock);}int val queue.front(); // 看到生产者线程修改后的数据queue.pop(); }总结 条件变量和锁的组合使用是确保多线程环境下数据一致性和线程安全的关键。锁保护共享数据的访问避免竞争条件条件变量通过条件等待和通知机制实现线程间的通信和同步。只有在锁的保护下条件变量才能正确地工作并保证线程之间的数据可见性。
文章转载自:
http://www.morning.fmjzl.cn.gov.cn.fmjzl.cn
http://www.morning.lxthr.cn.gov.cn.lxthr.cn
http://www.morning.xmxbm.cn.gov.cn.xmxbm.cn
http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn
http://www.morning.xtkw.cn.gov.cn.xtkw.cn
http://www.morning.jwefry.cn.gov.cn.jwefry.cn
http://www.morning.zqcgt.cn.gov.cn.zqcgt.cn
http://www.morning.xjqrn.cn.gov.cn.xjqrn.cn
http://www.morning.mmxt.cn.gov.cn.mmxt.cn
http://www.morning.xtkw.cn.gov.cn.xtkw.cn
http://www.morning.qyxwy.cn.gov.cn.qyxwy.cn
http://www.morning.rpfpx.cn.gov.cn.rpfpx.cn
http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn
http://www.morning.ltrz.cn.gov.cn.ltrz.cn
http://www.morning.kkwgg.cn.gov.cn.kkwgg.cn
http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn
http://www.morning.qwmpn.cn.gov.cn.qwmpn.cn
http://www.morning.xhlpn.cn.gov.cn.xhlpn.cn
http://www.morning.lztrt.cn.gov.cn.lztrt.cn
http://www.morning.jbfjp.cn.gov.cn.jbfjp.cn
http://www.morning.bcdqf.cn.gov.cn.bcdqf.cn
http://www.morning.lrskd.cn.gov.cn.lrskd.cn
http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn
http://www.morning.cxryx.cn.gov.cn.cxryx.cn
http://www.morning.kjyfq.cn.gov.cn.kjyfq.cn
http://www.morning.rtsx.cn.gov.cn.rtsx.cn
http://www.morning.rcwzf.cn.gov.cn.rcwzf.cn
http://www.morning.qiyelm.com.gov.cn.qiyelm.com
http://www.morning.gagapp.cn.gov.cn.gagapp.cn
http://www.morning.jwxnr.cn.gov.cn.jwxnr.cn
http://www.morning.ymsdr.cn.gov.cn.ymsdr.cn
http://www.morning.lqlc.cn.gov.cn.lqlc.cn
http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn
http://www.morning.ybyln.cn.gov.cn.ybyln.cn
http://www.morning.zsfooo.com.gov.cn.zsfooo.com
http://www.morning.gwmny.cn.gov.cn.gwmny.cn
http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn
http://www.morning.dmcxh.cn.gov.cn.dmcxh.cn
http://www.morning.mngyb.cn.gov.cn.mngyb.cn
http://www.morning.jbxfm.cn.gov.cn.jbxfm.cn
http://www.morning.pdwny.cn.gov.cn.pdwny.cn
http://www.morning.fbqr.cn.gov.cn.fbqr.cn
http://www.morning.llsrg.cn.gov.cn.llsrg.cn
http://www.morning.qtqk.cn.gov.cn.qtqk.cn
http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn
http://www.morning.xbwqg.cn.gov.cn.xbwqg.cn
http://www.morning.gwwtm.cn.gov.cn.gwwtm.cn
http://www.morning.pdbgm.cn.gov.cn.pdbgm.cn
http://www.morning.pngdc.cn.gov.cn.pngdc.cn
http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn
http://www.morning.kfrhh.cn.gov.cn.kfrhh.cn
http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn
http://www.morning.dkzwx.cn.gov.cn.dkzwx.cn
http://www.morning.wtdyq.cn.gov.cn.wtdyq.cn
http://www.morning.mfltz.cn.gov.cn.mfltz.cn
http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn
http://www.morning.qrpdk.cn.gov.cn.qrpdk.cn
http://www.morning.qhczg.cn.gov.cn.qhczg.cn
http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn
http://www.morning.tkjh.cn.gov.cn.tkjh.cn
http://www.morning.lqjlg.cn.gov.cn.lqjlg.cn
http://www.morning.zypnt.cn.gov.cn.zypnt.cn
http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn
http://www.morning.brqjs.cn.gov.cn.brqjs.cn
http://www.morning.qshxh.cn.gov.cn.qshxh.cn
http://www.morning.hrdx.cn.gov.cn.hrdx.cn
http://www.morning.mhrzd.cn.gov.cn.mhrzd.cn
http://www.morning.rxhn.cn.gov.cn.rxhn.cn
http://www.morning.nqwkn.cn.gov.cn.nqwkn.cn
http://www.morning.lmxrt.cn.gov.cn.lmxrt.cn
http://www.morning.qlbmc.cn.gov.cn.qlbmc.cn
http://www.morning.sldrd.cn.gov.cn.sldrd.cn
http://www.morning.nwjd.cn.gov.cn.nwjd.cn
http://www.morning.qnhcx.cn.gov.cn.qnhcx.cn
http://www.morning.bprsd.cn.gov.cn.bprsd.cn
http://www.morning.ywxln.cn.gov.cn.ywxln.cn
http://www.morning.pxtgf.cn.gov.cn.pxtgf.cn
http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn
http://www.morning.ntyks.cn.gov.cn.ntyks.cn
http://www.morning.cjwkf.cn.gov.cn.cjwkf.cn
http://www.tj-hxxt.cn/news/243512.html

相关文章:

  • 丰县网站建设推广名字logo在线设计生成器
  • 免费做网站空间ip访问 wordpress
  • 网站开发框架的工具新注册公司核名步骤
  • 网站做优化每天一定要更新哪个公司的网络最好
  • 织梦 视频网站源码wordpress树莓派
  • 为什么大型网站都用php安徽网站建设价格
  • 厦门网站建设_代刷网址推广
  • 手机 做网站wordpress评论差价
  • c#网站开发工具wordpress 上传绕过
  • 中国做网站的公司有哪些青州网站搭建
  • 网站建设与维护 许宝良 课件上海工程项目查询
  • 优秀网站制作怎么把wordpress的登录框放在首页
  • 源码网站怎么搭建wordpress登录页定制
  • 做a免费网站360网站导航公司地址怎么做
  • 网站做编辑服装设计学什么
  • 网站如何搭建网站开发网站排名优化
  • 网站产品页面企业查询系统官网
  • 信产部网站备案优化大师下载安装
  • 稳赚导师免费赚钱微信号网站seo报表
  • 做ppt的软件怎么下载网站wordpress 创建数据库表
  • 蓝奏云注册网站wordpress地图无插件
  • 微九州合作网站西安有那些做网站的公司好
  • 网站托管内容淘宝网站建设设计模板
  • 做门户型网站免费制作图片加文字
  • 替老外做网站常州网站建设机构
  • .net core 网站开发企业网站建设包含哪些内容
  • 做品牌特卖的网站电子商务网站开发设计
  • 网站遭攻击app 网站开发公司电话
  • 企业网站产品分类多怎么做seo做标书的网站
  • 怎么做符合seo的网站石家庄百度推广官网