药店网站源码,wordpress站长邮箱,造价企业怎么登陆建设部网站,wordpress自动升级了一、基本概念
1、程序是一段静态代码#xff1b;进程是正在运行的程序#xff1b;线程则是程序内部的执行路径。 上面这张图就解释了线程和多线程的意义。
2、若一个程序在同一时间执行多个线程#xff0c;便是支持多线程的。一个进程中的多个线程共享相同的内存单元/内存…一、基本概念
1、程序是一段静态代码进程是正在运行的程序线程则是程序内部的执行路径。 上面这张图就解释了线程和多线程的意义。
2、若一个程序在同一时间执行多个线程便是支持多线程的。一个进程中的多个线程共享相同的内存单元/内存地址空间。 二、两种形式的多线程
1、单CPU内核的多线程
单个CPU以极高的频率轮流执行多个线程的运算各个线程雨露均沾。 2、多CPU内核的多线程
可以做到真正的并行运算 所以现在我们所说的软件意义上的线程和硬件意义上的线程如我的i7就是4核心8线程并不是同一个概念。单个CPU内核也可以模拟出多个线程。对于一般的家用电脑运行大约100个线程是可行的。 三、Cpp的多线程
在C11标准之前C多线程需要借助操作系统提供的API如Linux的pthread.h库和Windows的windows.h库。C11提供了语言层面上的多线程包含在头文件thread中。它解决了跨平台的问题。以下的各个库均为C11标准为多线程所设计的
threadmutexatomiccondition_variablefuture 四、创建线程
创建线程时必须把需要分开执行的程序封装在不同的代码块里也就是函数里。对于线程C用类来描述创建了一个Thread类的对象即是新增了一个线程。
比如说我要分别执行两个冒泡排序我便先写了一个冒泡排序的函数。
void bubblesort(int* array,int len)
{for(int i1;ilen;i){for(int j0;jlen-i;j){if (array[j]array[j1]){int temparray[j];array[j]array[j1];array[j1]array[j];}}}
}
有下面两种创建线程的方法
std::thread thread1(bubblesort,array1,LEN);
thread1.join();
1、创建线程thread1运行函数bubblesort。对于thread构造函数其第一个参数为所执行函数的名称可能是引用后面的参数则为所执行函数的参数。
std::thread (bubblesort,array1,LEN).join();
2、直接创建线程无名称并以规定的方式运行程序。 下面给出创建线程运行的例子以及单线程运行的对比程序。两程序输出结果均为排序算法运行时间。读者可以自行验证多线程的效率确实大有优势
多线程
#include iostream
#include thread
#include ctime
#include cstdlib#define LEN 100000void bubblesort(int* array,int len);int main(void)
{int* array1;int* array2;array1new int[LEN];array2new int[LEN];srand(time(0));for (int i0;iLEN;i){array1[i](rand()%10000)*(rand()%10000);array2[i](rand()%10000)*(rand()%10000);}clock_t start_timeclock();std::thread thread1(bubblesort,array1,LEN);std::thread thread2(bubblesort,array2,LEN);thread1.join();thread2.join();clock_t stop_timeclock();std::cout1.0*(stop_time-start_time)/CLOCKS_PER_SEC;
} void bubblesort(int* array,int len)
{for(int i1;ilen;i){for(int j0;jlen-i;j){if (array[j]array[j1]){int temparray[j];array[j]array[j1];array[j1]array[j];}}}
}
单线程
#include iostream
#include thread
#include ctime
#include cstdlib#define LEN 100000void bubblesort(int* array,int len);int main(void)
{int* array1;int* array2;array1new int[LEN];array2new int[LEN];srand(time(0));for (int i0;iLEN;i){array1[i](rand()%10000)*(rand()%10000);array2[i](rand()%10000)*(rand()%10000);}clock_t start_timeclock();bubblesort(array1,LEN);bubblesort(array2,LEN);clock_t stop_timeclock();std::cout1.0*(stop_time-start_time)/CLOCKS_PER_SEC;
} void bubblesort(int* array,int len)
{for(int i1;ilen;i){for(int j0;jlen-i;j){if (array[j]array[j1]){int temparray[j];array[j]array[j1];array[j1]array[j];}}}
} 五、是否循环等待 detach和join
detach方式启动的线程自主在后台运行当前的代码继续往下执行不等待新线程结束。join方式等待启动的线程完成才会继续往下执行。
1、直接一个小实验分析join的用法
#include thread
#include iostream
#include windows.h//func1和func2这两个函数分别打印对应内容10次
void func1()
{ int count0;while(count10){std::coutthread1 workingstd::endl;Sleep(1000);count;}
}void func2()
{ int count0;while(count10){std::coutthread2 workingstd::endl;Sleep(1000);count;}
}int main(void)
{std::coutonestd::endl;std::thread thread1(func1);std::thread thread2(func2);std::couttwostd::endl;//这里使用了join程序会一直循环等待 thread1.join();thread2.join();std::coutthree;
}
程序的输出结果是 one two thread2 working thread1 working thread1 working thread2 working thread1 working thread2 working thread1 workingthread2 working thread2 working thread1 working thread1 working thread2 working thread2 working thread1 working thread2 working thread1 working thread2 working thread1 working thread2 working thread1 working thread2 working thread1 working three 可以看出位于join之前的one和two还是可以被打印的而three要等到thread1和thread2执行完毕才被打印。可以把join理解为循环等待函数。在thread1和thread2执行完毕前主程序一直卡在里面不继续执行知道两个线程结束主程序才继续执行。
此外由于两线程一同占用输出流可以看到thread1的某个换行符被吞掉了我想这是两线程占用资源冲突造成的 2、把join改为detach重复实验
int main(void)
{std::coutonestd::endl;std::thread thread1(func1);std::thread thread2(func2);std::couttwostd::endl;//这里使用了detach主线程继续运行 thread1.detach();thread2.detach();std::coutthree;
} one two thread1 working thread2 working three 可以看到thread1和thread2中的函数只执行了一次打印主线程就结束了。那么子线程也结束了。 3、只改一个join呢
int main(void)
{std::coutonestd::endl;std::thread thread1(func1);std::thread thread2(func2);std::couttwostd::endl;//这里使用了join程序会一直循环等待 thread1.join();thread2.detach();std::coutthree;
} one two thread1 working thread2 working thread1 workingthread2 working thread1 workingthread2 working thread1 workingthread2 working thread2 working thread1 working thread2 working thread1 working thread1 working thread2 working thread2 working thread1 working thread1 workingthread2 working thread1 working thread2 working thread1 working thread2 working three 这印证了我们对join的理解它的的确确就是一个循环等待函数一般的存在像arduino里和ROS里面一样
上面这个例子我们可以说吗托thread1的福thread2也得以运行完毕但是如果thread2运行得慢一点那么thread2依旧无法执行完毕。 4、继续做一些操作呢
int main(void)
{std::coutonestd::endl;std::thread thread1(func1);std::thread thread2(func2);std::couttwostd::endl;thread1.detach();std::coutthreestd::endl;thread2.join();std::coutfour;
}
我把代码改成了这样读者可以自行验证输出结果非常有意思 参考资料多线程的学习_多线程学习-CSDN博客
C多线程详解全网最全 - 知乎 (zhihu.com)