网易云课堂上有门c++多线程的入门教程,学习过程在此记录下。
并发编程:
1.多进程(通信:文件,管道,消息队列);2.多线程(通信:共享内存)。
code1:
#include <iostream>
#include <thread>
void test_funtion(){
std::cout << "test test test!" <<std:: endl;
}
int main()
{
std::thread t1(test_funtion);
//t1.join();//main线程等待t1线程完成后继续向下执行
t1.detach();//t1还没来得及输出,主程序就结束了,所以没有输出。线程detach之后,不能再join。
if (t1.joinable()){
t1.join();
}
return 0;
}
code2:
#include <iostream>
#include <thread>
void test_funtion(){
std::cout << "test_funtion!" <<std:: endl;
}
int main()
{
std::thread t1(test_funtion);
for (int i = 0; i < 100;++i)