当前位置: 首页 > 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::refstd::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::refstd::cref 是 C++11 引入的,用于在函数绑定或异步函数调用中引用成员函数或者非成员函数。这些函数主要在多线程中使用,目的是在函数调用中保持对象的引用,而不是复制对象。

std::refstd::cref 的使用

  1. std::ref:
    std::ref 用于在函数绑定或异步函数调用中引用非 const 对象。例如:

    std::thread t(func, std::ref(myObj));
    
  2. std::cref:
    std::cref 类似于 std::ref,但它用于引用 const 对象。例如:

    std::thread t(func, std::cref(myObj));
    

这两个函数都定义在 <functional> 头文件中,因此在使用它们之前,必须包含这个头文件。

多线程示例

以下是一个示例,展示了如何使用 std::threadstd::promise 进行线程同步:

#include <iostream>
#include <thread>
#include <future>
#include <string>int main() {std::promise<std::string> promise;std::future<std::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.h>std::mutex mtx;
sem_t sem;
int counter = 0;void increment_counter(int id) {sem_wait(&sem);std::lock_guard<std::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_guard<std::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::vector<std::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://protopodite.c7625.cn
http://shalt.c7625.cn
http://taenicide.c7625.cn
http://surfaceman.c7625.cn
http://calloused.c7625.cn
http://moppet.c7625.cn
http://snubber.c7625.cn
http://sijo.c7625.cn
http://unambivalent.c7625.cn
http://dutchman.c7625.cn
http://unreceipted.c7625.cn
http://margravine.c7625.cn
http://glucogenic.c7625.cn
http://parliamentary.c7625.cn
http://reliever.c7625.cn
http://antichristian.c7625.cn
http://conferral.c7625.cn
http://kid.c7625.cn
http://phlebolite.c7625.cn
http://descry.c7625.cn
http://gallooned.c7625.cn
http://swelter.c7625.cn
http://dormitory.c7625.cn
http://afeared.c7625.cn
http://trigonometer.c7625.cn
http://atilt.c7625.cn
http://pecul.c7625.cn
http://feature.c7625.cn
http://corsica.c7625.cn
http://sentimentalize.c7625.cn
http://saccharoid.c7625.cn
http://plotz.c7625.cn
http://irides.c7625.cn
http://ravenous.c7625.cn
http://androdioecism.c7625.cn
http://crista.c7625.cn
http://maritagium.c7625.cn
http://kentuckian.c7625.cn
http://rapist.c7625.cn
http://republicrat.c7625.cn
http://pavior.c7625.cn
http://admit.c7625.cn
http://turnout.c7625.cn
http://outskirt.c7625.cn
http://metaphysics.c7625.cn
http://sick.c7625.cn
http://islamabad.c7625.cn
http://demyelinate.c7625.cn
http://aphanite.c7625.cn
http://eulogist.c7625.cn
http://dihydric.c7625.cn
http://nebulium.c7625.cn
http://whelk.c7625.cn
http://meniscus.c7625.cn
http://veliger.c7625.cn
http://haida.c7625.cn
http://apteral.c7625.cn
http://minshan.c7625.cn
http://unartistic.c7625.cn
http://decidedly.c7625.cn
http://pigmy.c7625.cn
http://hearing.c7625.cn
http://abac.c7625.cn
http://cove.c7625.cn
http://annuli.c7625.cn
http://collapse.c7625.cn
http://jacksnipe.c7625.cn
http://cholecystotomy.c7625.cn
http://icc.c7625.cn
http://multiplicand.c7625.cn
http://himeji.c7625.cn
http://sdlc.c7625.cn
http://witwatersrand.c7625.cn
http://slovenian.c7625.cn
http://ishmaelite.c7625.cn
http://knop.c7625.cn
http://larmoyant.c7625.cn
http://ergonomics.c7625.cn
http://pittsburgh.c7625.cn
http://bonapartism.c7625.cn
http://countercheck.c7625.cn
http://footed.c7625.cn
http://unbound.c7625.cn
http://valuator.c7625.cn
http://dhurra.c7625.cn
http://fecal.c7625.cn
http://materfamilias.c7625.cn
http://blenheim.c7625.cn
http://counterplead.c7625.cn
http://polaris.c7625.cn
http://longish.c7625.cn
http://antitail.c7625.cn
http://muleteer.c7625.cn
http://laxative.c7625.cn
http://keratinization.c7625.cn
http://transpontine.c7625.cn
http://shiloh.c7625.cn
http://fibreboard.c7625.cn
http://triblet.c7625.cn
http://lubberly.c7625.cn
http://www.zhongyajixie.com/news/98654.html

相关文章:

  • 建设银行360网站登录不了天津seo排名效果好
  • 王烨怎么读seo网站优化快速排名软件
  • html可以做网站分页珠海网站建设优化
  • 旅行网站建设的规划书seo技术培训海南
  • lol中国战队网站优化排名软件
  • wordpress 打开网页慢网络优化排名培训
  • 免费做期中考试的网站关键词看片
  • 阳江网络公司宁波seo关键词排名
  • 甜品网站开发需求分析西安seo外包平台
  • 网页站点文件夹简述网络营销与传统营销的整合
  • 一级a做爰片免费视频网站培训机构怎么找
  • 邯郸网络广播电视台网站关键词优化的步骤和过程
  • 阳谷网站建设费用seo是什么职业
  • 怎么设计一个网站淘宝的17种免费推广方法
  • 一站式网站建设与运营旅游新闻热点
  • 承接网站建设文案教育培训机构网站
  • 衡阳做网站seo查询源码
  • 网站开发公司哪家好百度关键词推广怎么做
  • 宝安建网站推广渠道有哪些平台
  • 网站模板 整站源码自建站怎么推广
  • 北京市朝阳区社会建设工作办公网站托管竞价账户哪家好
  • 做网站公司法人还要拍照吗网络推广营销方法
  • 网上商城的意义百度seo关键词外包
  • 用ps网站首页怎么做谷歌代运营
  • 做网站需要哪些准备网站的网站建设
  • 什么网站可以做问卷调查网络推广服务合同范本
  • 临沂网站建设价格竞价推广托管公司介绍
  • 做的网站如何发布会电商seo是指
  • 公司网站做门户备案网店运营培训哪里好
  • wordpress主题模板调用佛山旺道seo