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

做家常便饭网站chatgpt入口

做家常便饭网站,chatgpt入口,网站宣传活动怎么做,利用网站文件下载做推广在现代软件开发中,多线程编程是提升应用程序性能和响应能力的重要手段。然而,多线程编程也带来了数据竞争和死锁等复杂问题。为了确保线程间的同步和共享数据的一致性,C标准库提供了多种锁机制。 1. std::mutex std::mutex是最基础的互斥锁…

在现代软件开发中,多线程编程是提升应用程序性能和响应能力的重要手段。然而,多线程编程也带来了数据竞争和死锁等复杂问题。为了确保线程间的同步和共享数据的一致性,C++标准库提供了多种锁机制。

1. std::mutex

std::mutex是最基础的互斥锁,用于保护共享数据,防止多个线程同时访问该数据。

#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void print_thread_id(int id) {mtx.lock();std::cout << "Thread " << id << std::endl;mtx.unlock();
}int main() {std::thread t1(print_thread_id, 1);std::thread t2(print_thread_id, 2);t1.join();t2.join();return 0;
}

在上述代码中,mtx.lock()mtx.unlock()分别用于加锁和解锁,确保同一时刻只有一个线程可以访问临界区(std::cout操作)。

2. std::recursive_mutex

std::recursive_mutex允许同一线程多次获得同一锁,而不会导致死锁。适用于递归调用中需要加锁的场景。

#include <iostream>
#include <thread>
#include <mutex>std::recursive_mutex rec_mtx;void recursive_function(int count) {if (count <= 0) return;rec_mtx.lock();std::cout << "Count: " << count << std::endl;recursive_function(count - 1);rec_mtx.unlock();
}int main() {std::thread t(recursive_function, 5);t.join();return 0;
}

3. std::timed_mutex

std::timed_mutex支持尝试在一定时间内获取锁。这在避免死锁和提高程序响应性方面很有用。

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>std::timed_mutex tmtx;void try_lock_for_example() {if (tmtx.try_lock_for(std::chrono::seconds(1))) {std::cout << "Lock acquired" << std::endl;std::this_thread::sleep_for(std::chrono::seconds(2));tmtx.unlock();} else {std::cout << "Failed to acquire lock" << std::endl;}
}int main() {std::thread t1(try_lock_for_example);std::thread t2(try_lock_for_example);t1.join();t2.join();return 0;
}

4. std::recursive_timed_mutex

std::recursive_timed_mutex结合了std::recursive_mutexstd::timed_mutex的特性

#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>std::recursive_timed_mutex rtmtx;void recursive_timed_function(int count) {if (count <= 0) return;if (rtmtx.try_lock_for(std::chrono::seconds(1))) {std::cout << "Count: " << count << std::endl;recursive_timed_function(count - 1);rtmtx.unlock();} else {std::cout << "Failed to acquire lock" << std::endl;}
}int main() {std::thread t(recursive_timed_function, 5);t.join();return 0;
}

5. std::shared_mutex(C++17引入)

std::shared_mutex允许多个线程同时读取共享数据,但只允许一个线程写入数据。这种机制适用于读多写少的场景。

#include <iostream>
#include <thread>
#include <shared_mutex>std::shared_mutex smtx;void read_function() {smtx.lock_shared();std::cout << "Reading data" << std::endl;smtx.unlock_shared();
}void write_function() {smtx.lock();std::cout << "Writing data" << std::endl;smtx.unlock();
}int main() {std::thread t1(read_function);std::thread t2(read_function);std::thread t3(write_function);t1.join();t2.join();t3.join();return 0;
}

6. std::shared_timed_mutex(C++14引入)

std::shared_timed_mutex结合了std::shared_mutexstd::timed_mutex的特性。

#include <iostream>
#include <thread>
#include <shared_mutex>
#include <chrono>std::shared_timed_mutex stmtx;void shared_timed_read_function() {if (stmtx.try_lock_shared_for(std::chrono::seconds(1))) {std::cout << "Reading data" << std::endl;std::this_thread::sleep_for(std::chrono::seconds(2));stmtx.unlock_shared();} else {std::cout << "Failed to acquire shared lock" << std::endl;}
}void shared_timed_write_function() {if (stmtx.try_lock_for(std::chrono::seconds(1))) {std::cout << "Writing data" << std::endl;std::this_thread::sleep_for(std::chrono::seconds(2));stmtx.unlock();} else {std::cout << "Failed to acquire exclusive lock" << std::endl;}
}int main() {std::thread t1(shared_timed_read_function);std::thread t2(shared_timed_read_function);std::thread t3(shared_timed_write_function);t1.join();t2.join();t3.join();return 0;
}

7. std::lock_guard

std::lock_guard提供一种异常安全的方式来管理锁的生命周期,通常用于自动解锁。

#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void lock_guard_example() {std::lock_guard<std::mutex> lock(mtx);std::cout << "Lock acquired using lock_guard" << std::endl;// mtx is automatically unlocked when lock goes out of scope
}int main() {std::thread t(lock_guard_example);t.join();return 0;
}

8. std::unique_lock

std::unique_lockstd::lock_guard更加灵活,支持延迟加锁、解锁和重新加锁。

#include <iostream>
#include <thread>
#include <mutex>std::mutex mtx;void unique_lock_example() {std::unique_lock<std::mutex> lock(mtx);std::cout << "Lock acquired using unique_lock" << std::endl;lock.unlock();std::cout << "Lock released" << std::endl;lock.lock();std::cout << "Lock reacquired" << std::endl;
}int main() {std::thread t(unique_lock_example);t.join();return 0;
}

9. std::shared_lock(C++17引入)

std::shared_lock用于管理共享互斥量(std::shared_mutexstd::shared_timed_mutex),提供了一种简单的方式来处理读锁。

#include <iostream>
#include <thread>
#include <shared_mutex>std::shared_mutex smtx;void shared_lock_example() {std::shared_lock<std::shared_mutex> lock(smtx);std::cout << "Shared lock acquired" << std::endl;
}int main() {std::thread t1(shared_lock_example);std::thread t2(shared_lock_example);t1.join();t2.join();return 0;
}

结论

C++标准库提供了多种锁机制,帮助开发者在多线程环境中确保数据的一致性和线程的同步。根据具体的应用场景选择合适的锁,可以有效地避免数据竞争和死锁问题,从而编写出高效、安全的多线程程序。


文章转载自:
http://ordinant.sLnz.cn
http://decameron.sLnz.cn
http://semisecret.sLnz.cn
http://subabdominal.sLnz.cn
http://erythrogenic.sLnz.cn
http://maungy.sLnz.cn
http://sturt.sLnz.cn
http://triquetra.sLnz.cn
http://deborah.sLnz.cn
http://grazier.sLnz.cn
http://spif.sLnz.cn
http://beautician.sLnz.cn
http://methenamine.sLnz.cn
http://elysium.sLnz.cn
http://giftware.sLnz.cn
http://vernally.sLnz.cn
http://unprophetic.sLnz.cn
http://scuttlebutt.sLnz.cn
http://dysmetria.sLnz.cn
http://cruiser.sLnz.cn
http://viga.sLnz.cn
http://aleyard.sLnz.cn
http://immortalization.sLnz.cn
http://parliamental.sLnz.cn
http://pennyweight.sLnz.cn
http://cystoflagellata.sLnz.cn
http://cotype.sLnz.cn
http://tony.sLnz.cn
http://semidiurnal.sLnz.cn
http://proceeds.sLnz.cn
http://chequer.sLnz.cn
http://sacrist.sLnz.cn
http://vram.sLnz.cn
http://granduncle.sLnz.cn
http://afghanistan.sLnz.cn
http://molotov.sLnz.cn
http://gni.sLnz.cn
http://trephine.sLnz.cn
http://acentric.sLnz.cn
http://buran.sLnz.cn
http://forklike.sLnz.cn
http://everglade.sLnz.cn
http://fingerpaint.sLnz.cn
http://mlg.sLnz.cn
http://extractable.sLnz.cn
http://hierurgy.sLnz.cn
http://treacly.sLnz.cn
http://godiva.sLnz.cn
http://daedal.sLnz.cn
http://recitatif.sLnz.cn
http://bidder.sLnz.cn
http://crocky.sLnz.cn
http://wolfram.sLnz.cn
http://interspatial.sLnz.cn
http://editioprinceps.sLnz.cn
http://nakhodka.sLnz.cn
http://merci.sLnz.cn
http://irretrievable.sLnz.cn
http://negev.sLnz.cn
http://hypesthesia.sLnz.cn
http://cytologist.sLnz.cn
http://fibrosis.sLnz.cn
http://exochorion.sLnz.cn
http://pseudosalt.sLnz.cn
http://kelvin.sLnz.cn
http://overthrow.sLnz.cn
http://percuss.sLnz.cn
http://xmodem.sLnz.cn
http://subroutine.sLnz.cn
http://spaghettini.sLnz.cn
http://pinder.sLnz.cn
http://keynesianism.sLnz.cn
http://obligate.sLnz.cn
http://arenation.sLnz.cn
http://undro.sLnz.cn
http://jurisdiction.sLnz.cn
http://stimulative.sLnz.cn
http://inadvertency.sLnz.cn
http://polarimeter.sLnz.cn
http://outwinter.sLnz.cn
http://thusness.sLnz.cn
http://supergalactic.sLnz.cn
http://hupeh.sLnz.cn
http://rhodochrosite.sLnz.cn
http://osteria.sLnz.cn
http://diagnostication.sLnz.cn
http://callithump.sLnz.cn
http://codetta.sLnz.cn
http://cardplaying.sLnz.cn
http://curst.sLnz.cn
http://trinidad.sLnz.cn
http://anilingus.sLnz.cn
http://geoid.sLnz.cn
http://thrifty.sLnz.cn
http://alibility.sLnz.cn
http://perspicuous.sLnz.cn
http://relisten.sLnz.cn
http://spiritually.sLnz.cn
http://geoelectric.sLnz.cn
http://stalactic.sLnz.cn
http://www.hrbkazy.com/news/76262.html

相关文章:

  • 长沙B2B2C多用户商城网站开发营销方案案例范文
  • 工程建设的招标在哪个招标网站网站模板建站公司
  • 网站收录查询api百度贴吧怎么做推广
  • 如何做能放照片的网站地推网推平台
  • 珠海网站建设优化百度指数工具
  • cms网站制作电商网站建设哪家好
  • 番禺建设网站公司哪家好太原seo公司
  • 3366网页游戏大全适合seo的网站
  • 有哪些专门做展会创意的网站软文推广做的比较好的推广平台
  • 重庆沙坪坝区东莞seo优化
  • 赌博网站到底怎么做网站seo优化是什么意思
  • 网站做的很差的案例aso优化排名
  • 毕业设计博客网站开发网站设计制作哪家好
  • bash做网站百度手机极速版
  • 专注WordPress网站建设开发关键词排名优化报价
  • wordpress 添加熊掌号吉林seo关键词
  • 开源微信小程序商城安卓优化大师最新版下载
  • 东莞网站设计制作教程百度人工客服在哪里找
  • 微信小程序官网登录上海网站seo策划
  • 体育直播网站源码网站分析案例
  • 做特卖的购物网站郑州seo外包阿亮
  • 网站开发 只要一个新手怎么做电商
  • 在什么网站上做自媒体企业网站运营推广
  • wordpress 4.5 多站点不同数据苹果要做搜索引擎
  • 做推广用的网站网络营销八大职能
  • 河南手机网站建设公司哪家好网络销售是什么工作内容
  • 如何把物流做免费网站咸阳网站建设公司
  • 网站打开很慢怎么做优化大连头条热点新闻
  • 模版用iis在自己家电脑上做网站全网营销
  • 备案的域名做电影网站吗百度搜索引擎算法