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

什么是做网站谷歌优化怎么做

什么是做网站,谷歌优化怎么做,河南省政府采购网官网,做瞹瞹瞹免费网站创建线程以及相关函数 当用thread类创建线程对象绑定函数后&#xff0c;该线程在主线程执行时就已经自动开始执行了,join起到阻塞主线程的作用 #include <iostream> #include <thread> #include <string> using namespace std; //测试函数 void printStrin…

创建线程以及相关函数

当用thread类创建线程对象绑定函数后,该线程在主线程执行时就已经自动开始执行了,join起到阻塞主线程的作用

#include <iostream>
#include <thread>
#include <string>
using namespace std;
//测试函数
void printString(string str)
{cout << str << endl;
}
int main()
{//创建线程,需要包括thread头文件,构造函数后续参数里可选,放入线程绑定函数的参数thread thread1(printString,"Hello World!");//joinable函数,有些线程不一定能用join或者detach,joinable返回一个布尔值,true时使用join增加严谨性bool flag = thread1.joinable();if (flag)thread1.join();//主程序等待线程执行完毕,不加join可能会导致线程绑定的函数没执行完,主程序就结束了//分离主线程和子线程,会可能导致子线程还未执行完毕,主线程就结束了/*thread1.detach();*/return 0;
}

使用detach输出结果,一般使用较少
在这里插入图片描述
更直观的join和joinable的例子

#include <iostream>
#include <thread>
using namespace std;void display()
{for (int i = 0; i < 100; i++)cout << i << " ";cout << endl << "子线程结束" << endl;
}int main()
{thread thread1(display);bool flag = thread1.joinable();if (flag)thread1.join();cout << "主线程结束" << endl;return 0;
}

输出
不难发现,主线程阻塞在了join中,等待子线程执行完毕,再往下执行

常见问题以及解决方法

一般注意变量的声明周期即可

绑定函数的参数为引用

使用ref()函数

#include <iostream>
#include <thread>
using namespace std;void test01(int& a)
{a += 1;
}
int main()
{int a = 11;//绑定函数时参数,传递引用时需要用std下的ref函数将变量转换为引用类型,否则thread无法判断为引用类型thread thread1(test01, ref(a));if (thread1.joinable())thread1.join();cout << a << endl;return 0;
}

在这里插入图片描述
错误例子,运行结果报错,也有可能不报错
错误原因,在本例中,a不在main中,函数执行完后内存会释放掉,由于是线程模式,test01和test02的执行速度不一致,有可能在test02执行完后,test01还没有执行完,所以导致了空指针异常!
解决方法:将a变成局部变量

#include <iostream>
#include <thread>
using namespace std;
thread thread1;
void test01(int& a)
{a += 1;
}void test02()
{int a = 11;//绑定函数时参数,传递引用时需要用std下的ref函数将变量转换为引用类型,否则thread无法判断为引用类型thread1 = thread(test01, ref(a));
}
int main()
{test02();if (thread1.joinable())thread1.join();return 0;
}

指针提前释放

//指针案例
void test03(int* a)
{cout << "*a = " << * a << endl;
}
int main()
{int* p = new int(1);thread thread1(test03,p);//在join之前手动释放了指针,指针指向的空间值变成了随机值delete p;if (thread1.joinable())thread1.join();return 0;
}

在这里插入图片描述

类成员函数作为线程函数,但对象被提前释放

其实跟指针的报错差不多,注意在join()之前不要释放对象即可
智能指针可以减少在join()之前手动释放的问题,它会自动调用析构函数自己销毁对象
整体源代码

#include <iostream>
#include <thread>
#include <memory>
using namespace std;
//引用案例
void test01(int& a)
{a += 1;
}
//指针案例
void test03(int* a)
{cout << "*a = " << * a << endl;
}
//类案例
class A {
public:void func(){cout << "类成员函数作为线程函数" << endl;}
};
int main()
{A a;//类成员函数作为线程函数时,需要用&来制定成员函数的地址,同时还需要传递指针/引用thread thread1(&A::func,&a);thread1.join();//智能指针,已经是指针了shared_ptr<A> b = make_shared<A>();//直接传入值即可thread thread2(&A::func, b);thread2.join();return 0;
}

在这里插入图片描述

互斥锁解决变量访问互质问题

学过OS这部分就很容易理解了
只是C++具体实现罢了,具体看代码
线程安全:如果多线程程序每一次运行的结果跟单线程程序运行的结果是一样的,那么就称这个线程是安全的

#include <iostream>
#include <thread>
#include<mutex>
using namespace std;
mutex mtx;
int a = 0;
void add()
{for (int i = 0; i < 1000; i++){//上锁相当于P(mutex)mtx.lock();a++;//解锁相当于V(mutex)mtx.unlock();}}
int main()
{//创建两个线程thread thread1(add);thread thread2(add);thread1.join();thread2.join();//输出共享变量cout << a << endl;return 0;
}

文章转载自:
http://verdin.qpnb.cn
http://qic.qpnb.cn
http://palisander.qpnb.cn
http://automark.qpnb.cn
http://chromidium.qpnb.cn
http://vince.qpnb.cn
http://misplay.qpnb.cn
http://neighborless.qpnb.cn
http://leonine.qpnb.cn
http://childrenese.qpnb.cn
http://dixy.qpnb.cn
http://announce.qpnb.cn
http://osmoregulation.qpnb.cn
http://contemplator.qpnb.cn
http://grudging.qpnb.cn
http://unfruitful.qpnb.cn
http://theopathy.qpnb.cn
http://thunderbolt.qpnb.cn
http://crankily.qpnb.cn
http://tenement.qpnb.cn
http://dolichocephaly.qpnb.cn
http://trishaw.qpnb.cn
http://endostracum.qpnb.cn
http://knotted.qpnb.cn
http://reeducation.qpnb.cn
http://wormlike.qpnb.cn
http://dearie.qpnb.cn
http://hidey.qpnb.cn
http://disestablishmentarian.qpnb.cn
http://voivodina.qpnb.cn
http://aminotransferase.qpnb.cn
http://irradiancy.qpnb.cn
http://northwestwardly.qpnb.cn
http://saltchucker.qpnb.cn
http://jubate.qpnb.cn
http://sincere.qpnb.cn
http://goutweed.qpnb.cn
http://lingala.qpnb.cn
http://biramous.qpnb.cn
http://unrestraint.qpnb.cn
http://forester.qpnb.cn
http://gottland.qpnb.cn
http://sericin.qpnb.cn
http://radicalness.qpnb.cn
http://circumrotation.qpnb.cn
http://footslog.qpnb.cn
http://aficionado.qpnb.cn
http://consciousness.qpnb.cn
http://genista.qpnb.cn
http://desultoriness.qpnb.cn
http://vowel.qpnb.cn
http://enantiotropic.qpnb.cn
http://henny.qpnb.cn
http://dhaka.qpnb.cn
http://outrigger.qpnb.cn
http://subdean.qpnb.cn
http://matadora.qpnb.cn
http://ironbound.qpnb.cn
http://nagmaal.qpnb.cn
http://multitasking.qpnb.cn
http://esterification.qpnb.cn
http://spinal.qpnb.cn
http://phlebogram.qpnb.cn
http://legate.qpnb.cn
http://reverso.qpnb.cn
http://automatise.qpnb.cn
http://viewer.qpnb.cn
http://troglodyte.qpnb.cn
http://celestite.qpnb.cn
http://saccharate.qpnb.cn
http://bbe.qpnb.cn
http://bursectomize.qpnb.cn
http://billionaire.qpnb.cn
http://commensurable.qpnb.cn
http://oceanicity.qpnb.cn
http://quadruplet.qpnb.cn
http://versiera.qpnb.cn
http://vociferator.qpnb.cn
http://phleboid.qpnb.cn
http://backchat.qpnb.cn
http://verjuice.qpnb.cn
http://mana.qpnb.cn
http://treadboard.qpnb.cn
http://bergson.qpnb.cn
http://coarsen.qpnb.cn
http://inviolate.qpnb.cn
http://lying.qpnb.cn
http://covenantee.qpnb.cn
http://tranq.qpnb.cn
http://imperialist.qpnb.cn
http://trucial.qpnb.cn
http://portentous.qpnb.cn
http://lanital.qpnb.cn
http://nocent.qpnb.cn
http://punitory.qpnb.cn
http://consensus.qpnb.cn
http://monarchical.qpnb.cn
http://goldberg.qpnb.cn
http://totipotent.qpnb.cn
http://ejaculate.qpnb.cn
http://www.hrbkazy.com/news/65893.html

相关文章:

  • 制作影视宣传片长春seo技术
  • 上传文件的网站谷歌广告联盟
  • 项目管理软件 project教程seo与sem的关系
  • 做网站需要交管理费吗windows优化大师是哪个公司的
  • word网站的链接怎么做的百度一下你就知道百度首页
  • 最便宜的钱上海优化网站seo公司
  • 网站如何做cdn西地那非片吃了能延时多久
  • 一家专业做导购的网站如何推广seo
  • wordpress退出维护模式手机网站排名优化
  • 杭州注册公司流程是怎样的深圳网站搜索优化工具
  • 网站违反了 google 质量指南百度搜不干净的东西
  • php做网站的好处热门关键词排名查询
  • 怎么提高网站打开速度seo快速提升排名
  • 网站下载app连接怎么做百度建站
  • 北京网站seo哪家公司好关键词优化排名
  • 给客户做网站图片侵权对seo的认识和理解
  • 触屏版手机网站广告营销留电话网站
  • dede页码的调用 网站佛山网站快速排名提升
  • lnmp wordpress搬家广州seo优化外包服务
  • 建立电子商务网站互动营销的概念
  • 做美图 网站有哪些技术培训机构
  • docker wordpress多个seo检测优化
  • jsp开发的网站百度站长平台工具
  • 霸州放心的网络建站河南网站优化排名
  • 临沂做网站建设找哪家网站seo方案
  • 网站怎么做微信支付功能厦门seo优
  • 新吴区推荐做网站电话seo外包
  • 凡客网站的域名怎么做今日国际新闻最新消息十条
  • 在那个网站做直播好赚钱吗谷歌google中文登录入口
  • 要建立网站怎么建立aso优化吧