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

怎么做公司宣传网站岳阳网站建设收费标准

怎么做公司宣传网站,岳阳网站建设收费标准,电商设计美工,网站服务内容怎么写在 C 中#xff0c;std::thread 构造函数可以用于将参数传递给线程。这里是一个基本的示例#xff0c;展示了如何使用 std::thread 来传递参数#xff1a; #include iostream #include thread// 定义一个被线程调用的函数 void threadFunc(int arg1, doubl…在 C 中std::thread 构造函数可以用于将参数传递给线程。这里是一个基本的示例展示了如何使用 std::thread 来传递参数 #include iostream #include thread// 定义一个被线程调用的函数 void threadFunc(int arg1, double arg2, std::string arg3) {std::cout arg1: arg1 , arg2: arg2 , arg3: arg3 std::endl; }int main() {// 创建一个线程并传递参数std::thread t(threadFunc, 1, 3.14, Hello, World!);// 等待线程结束t.join();return 0; }在这个例子中定义了一个函数 threadFunc它接受三个参数。然后在 main 函数中创建了一个线程并将这三个参数传递给了 threadFunc。 如果函数参数是引用类型可以使用 std::ref 或 std::cref 来传递引用 #include iostream #include thread #include functional // std::ref 和 std::cref 需要这个头文件// 定义一个被线程调用的函数 void threadFunc(int arg1, double arg2, std::string arg3) {std::cout arg1: arg1 , arg2: arg2 , arg3: arg3 std::endl; }int main() {int arg1 1;double arg2 3.14;std::string arg3 Hello, World!;// 创建一个线程并传递参数引用std::thread t(threadFunc, std::ref(arg1), std::ref(arg2), std::ref(arg3));// 等待线程结束t.join();return 0; }在这个例子中使用 std::ref 来传递变量的引用这样就可以在 threadFunc 中修改这些变量的值。 std::ref 和 std::cref 是 C11 引入的用于在函数绑定或异步函数调用中引用成员函数或者非成员函数。这些函数主要在多线程中使用目的是在函数调用中保持对象的引用而不是复制对象。 std::ref 和 std::cref 的使用 std::ref: std::ref 用于在函数绑定或异步函数调用中引用非 const 对象。例如 std::thread t(func, std::ref(myObj));std::cref: std::cref 类似于 std::ref但它用于引用 const 对象。例如 std::thread t(func, std::cref(myObj));这两个函数都定义在 functional 头文件中因此在使用它们之前必须包含这个头文件。 多线程示例 以下是一个示例展示了如何使用 std::thread 和 std::promise 进行线程同步 #include iostream #include thread #include future #include stringint main() {std::promisestd::string promise;std::futurestd::string future promise.get_future();std::thread t([promise] {std::string s hello;std::this_thread::sleep_for(std::chrono::seconds(1));promise.set_value(s);});t.join();std::string value future.get();std::cout value std::endl;return 0; }使用信号量的多线程示例 下面是一个使用信号量和互斥锁进行线程同步的示例 #include iostream #include thread #include vector #include mutex #include semaphore.hstd::mutex mtx; sem_t sem; int counter 0;void increment_counter(int id) {sem_wait(sem);std::lock_guardstd::mutex lock(mtx);std::this_thread::sleep_for(std::chrono::milliseconds(100));counter;std::cout Thread id incremented counter to counter std::endl;sem_post(sem); }void read_counter(int id) {sem_wait(sem);std::lock_guardstd::mutex lock(mtx);std::this_thread::sleep_for(std::chrono::milliseconds(100));std::cout Thread id read counter value counter std::endl;sem_post(sem); }int main() {sem_init(sem, 0, 5);std::vectorstd::thread threads;for (int i 0; i 10; i) {if (i % 2 0) {threads.push_back(std::thread(increment_counter, i));} else {threads.push_back(std::thread(read_counter, i));}}for (auto thread : threads) {thread.join();}sem_destroy(sem);return 0; }在这个示例中使用了信号量和互斥锁来控制对共享资源 counter 的访问。这样可以确保多个线程安全地访问和修改共享资源。
http://www.hkea.cn/news/14569125/

相关文章:

  • 做电商网站的上海公司手机人才网
  • asp网站制作教程重庆个人网站建设
  • 关于 门户网站 建设 请示信誉好的低价网站建设
  • 东莞品牌型网站建设科技类公司网站设计
  • 珠宝公司网站模版亚马逊aws wordpress
  • 网站权重分析域名有了怎么建设网站
  • 简单的seo网站优化排名在线房产网
  • 生活服务网站开发与设计网络营销师培训费用是多少
  • 在萍乡谁可以做网站ip做网站
  • 东湖网站建设福州网站开发si7.cc
  • 高端网站开发 金蝶wordpress资源模板
  • 免费发布信息的平台奉化seo页面优化外包
  • 自创网站怎么赚钱wordpress div layer
  • 广州 网站 设计乡下房屋室内装修
  • 做网站需要执照吗济南网站制作搜到
  • 网站和微信 微网站解决方案网站后台统计怎么启动啊
  • 海淀做网站设计的公司天津网站建设索王道下拉
  • tp5手机网站开发网站改版方案流程
  • 廊坊论坛网站建设ftp客户端软件
  • 做黄页网站要告我dedecms免费模板
  • 重庆做网站最好的网页浏览
  • 一元购物网站怎么做wordpress安装幻灯片插件
  • 郑州网站权重广州建站外贸网站
  • 网站做缓存在哪里可以自己建网站
  • 沈阳网站建设方案襄阳做网站价格
  • 搭建企业网站的步骤wordpress语录主题
  • 山东省住房和城乡建设厅网站注册中心网页设计与网站建设完全实战手册
  • 网站建设叁金手指花总6杭州市建设信用网站
  • 源代码做的网站好用么萧山好的做网站的公司
  • 挂马网站 名单上海公司注册网上查名