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

wordpress视频防止下载文件西安优化seo

wordpress视频防止下载文件,西安优化seo,最好设计网站建设,网站建设营销型网站在工作中看到了如下代码,代码基于 std::thread 封装了一个 Thread 类。Thread 封装了业务开发中常用的接口,比如设置调度策略,设置优先级,设置线程名。如下代码删去了不必要的代码,只保留能说明问题的代码。从代码实现…

在工作中看到了如下代码,代码基于 std::thread 封装了一个 Thread 类。Thread 封装了业务开发中常用的接口,比如设置调度策略,设置优先级,设置线程名。如下代码删去了不必要的代码,只保留能说明问题的代码。从代码实现上来看,我们看不出什么问题,创建一个线程,第一个形参是线程的入口函数,后边的传参是线程入口函数的参数列表。

class Thread {
public:template <class Function, class... Args>Thread(Function &&f, Args &&...args) noexcept: internal_{[func = std::forward<Function>(f), &args...]() {func(args...);}}{}private:std::thread internal_;
};

Thread 类在大部分使用场景下是没问题的,比如下面的使用方式,创建了一个线程,线程中是一个死循环,每隔一秒打印一次 "thread running",可以正常工作。

#include <stdio.h>
#include <unistd.h>
#include <memory>
#include <thread>
#include <vector>class Thread {
public:template <class Function, class... Args>Thread(Function &&f, Args &&...args) noexcept: internal_{[func = std::forward<Function>(f), args...]() {func(args...);}}{}private:std::thread internal_;
};void func() {while (1) {printf("thread running\n");sleep(1);}
}int main() {Thread *t = new Thread(func);sleep(100);return 0;
}

1 问题现象

在下边这个使用场景下,就能暴露出来 Thread 的问题。

如下代码中连续创建了 8 个线程,线程的入口函数是 func(),func() 的形参是 Obj 对象,Obj 中的成员 i_  分别取值 0 ~ 7。

#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <memory>
#include <thread>
#include <vector>class Thread {
public:template <class Function, class... Args>Thread(Function &&f, Args &&...args) noexcept: internal_{[func = std::forward<Function>(f), &args...]() {func(args...);}}{}private:std::thread internal_;
};class Obj {
public:Obj(int i) {i_ = i;std::cout << "Obj(), i: " << i_ << std::endl;}Obj(const Obj &obj) {i_ = obj.i_;std::cout << "copy constructor, i: " << i_ << std::endl;}~Obj() {std::cout << "~Obj(), i: " << i_ << std::endl;}int i_;};void func(Obj obj) {printf("                in thread, i: %d\n", obj.i_);
}int main() {std::vector<Thread *> threads;int i = 0;for (i = 0; i < 8; i++) {printf("    out thread, i: %d\n", i);Obj obj(i);auto tmp = new Thread(func, obj);printf("after create thread %d\n", i);threads.emplace_back(tmp);// sleep(2);}sleep(100);return 0;
}

上边的代码编译之后,运行结果如下所示。我们的预期是在 func() 中的打印分别是 0 ~ 7,每个数字打印一次。但实际的打印结果是有重复的,如下图所示,2 有重复的,7 也有重复的。

root@wangyanlong-virtual-machine:/home/wyl/cpp# ./a.outout thread, i: 0
Obj(), i: 0
after create thread 0
~Obj(), i: 0out thread, i: 1
Obj(), i: 1
after create thread 1
~Obj(), i: 1out thread, i: 2
Obj(), i: 2
copy constructor, i: 2in thread, i: 2
~Obj(), i: 2
copy constructor, i: 2in thread, i: 2
~Obj(), i: 2
after create thread 2
~Obj(), i: 2out thread, i: 3
Obj(), i: 3
copy constructor, i: 2in thread, i: 2
~Obj(), i: 2
copy constructor, i: 3in thread, i: 3
~Obj(), i: 3
after create thread 3
~Obj(), i: 3out thread, i: 4
Obj(), i: 4
after create thread 4
~Obj(), i: 4out thread, i: 5
Obj(), i: 5
copy constructor, i: 4
after create thread 5
~Obj(), i: 5out thread, i: 6
Obj(), i: 6in thread, i: 4
~Obj(), i: 4
copy constructor, i: 5in thread, i: 5
~Obj(), i: 5
after create thread 6
~Obj(), i: 6out thread, i: 7
Obj(), i: 7
copy constructor, i: 7in thread, i: 7
~Obj(), i: 7
after create thread 7
~Obj(), i: 7
copy constructor, i: 7in thread, i: 7
~Obj(), i: 7

上边的代码把 sleep(2) 注释打开,打印结果是符合预期的。

或者将 main() 中的 Thread() 改成 std::thread,打印结果也是符合预期的,说明这种使用方式是符合 c++ 规范的。

2 问题分析

导致问题的原因有以下几个方面:

(1)线程的构造函数入参是右值引用,这个右值引用的生命周期在构造函数返回的时候已经结束了。右值引用,指向一个临时的存储空间,在反复创建 8 个线程期间,8 个右值引用指向的是同一块内存空间,后边的值会将前边的值覆盖。

(2)线程构造函数中,std::thread 的回调函数是一个 lambda 表达式,lambda 表达式中引用捕获了 args。

(3)在 Thread 构造函数中创建了线程,但是线程并不是立即执行的,从创建到真正执行是有一段时间的延迟。这样当线程真正运行的时候,再从 args 引用里边读取数据,取出来的是这块内存最新的数据,属于这个线程的数据已经被覆盖。

3 问题修改

引用捕获改成值捕获

如下代码,在 Thread() 构造函数中的 lambda 表达式对 args 的引用捕获改成值捕获。

#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <memory>
#include <thread>
#include <vector>class Thread {
public:template <class Function, class... Args>Thread(Function &&f, Args &&...args) noexcept: internal_{[func = std::forward<Function>(f), args...]() {func(args...);}}{}private:std::thread internal_;
};class Obj {
public:Obj(int i) {i_ = i;std::cout << "Obj(), i: " << i_ << std::endl;}Obj(const Obj &obj) {i_ = obj.i_;std::cout << "copy constructor, i: " << i_ << std::endl;}~Obj() {std::cout << "~Obj(), i: " << i_ << std::endl;}int i_;};void func(Obj obj) {printf("                in thread, i: %d\n", obj.i_);
}int main() {std::vector<Thread *> threads;int i = 0;for (i = 0; i < 8; i++) {printf("    out thread, i: %d\n", i);Obj obj(i);auto tmp = new Thread(func, obj);printf("after create thread %d\n", i);threads.emplace_back(tmp);// sleep(2);}sleep(100);return 0;
}
http://www.yayakq.cn/news/932157/

相关文章:

  • 做旅游宣传网站的流程做ppt免费模板软件
  • 甘井子区城市建设管理局网站国家高新技术企业管理办法
  • 泉州中企网站做的好吗微信小程序公司
  • 电子商务网站建设前期知乎企业网站建设
  • 成都网站内容策划大学生网站设计作品
  • 百度seo网站优化服务百度霸屏推广一般多少钱
  • 电子商务网站保密协议昆明网站制作计划
  • 服务器网站部署端口配置网站加入谷歌地图导航
  • 域通联达网站东莞传媒公司
  • 图书馆建设网站需要哪些费用湖北建设执业注册管理中心网站
  • 石家庄手机网站建设公司四川省住建厅考试报名官网
  • 南昌网站外包重庆最新网站备案
  • seo网站推广怎么做秦皇岛建筑
  • 网站建设制作、微信公众号贵阳网站建设方案
  • 前端怎么做自己的博客网站网站建设优惠中
  • 网站备案去哪里备案作文网哪个平台好
  • 一个网站2级域名多个人简历手机版免费
  • 个人如何网站备案定制设计网站
  • 自己制作网站的方法是页游游戏
  • 做网站和商城有什么好处网站 内容
  • 佛山做网站哪家好广州今天新闻
  • 酷炫网站模板网站建设费用一年
  • dw软件做的网站怎么发到网上阳江房产网二手房出售
  • 乐清手机网站优化推广网站设计深圳哪家强?
  • wordpress 安装php云南网站优化建设
  • 怎样做旅游公司的网站建设银行网上银行登录
  • 企业网站建设源码 微信 手机建设商务网站的步骤
  • 南昌模板建站定制青岛科友网站建设网络公司
  • 网络公司网站报价方案营销网站与企业网站的区别
  • 红色网站源码网页设计作品到哪个网站