增加网站备案,网站页面优化分析,100个经典创意营销方案,高校后勤网站建设要求C多线程类Thread#xff08;C11#xff09;
C11中std命名空间将Boost库中的Thread加入#xff0c;Boost的多线程从准标准变为标准#xff0c;这里将其用法整理复习#xff0c;以demo的形式复习#xff0c;还是喜欢看我自己写的东西#xff0c;符合我的个人逻辑头文件为…C多线程类ThreadC11
C11中std命名空间将Boost库中的Thread加入Boost的多线程从准标准变为标准这里将其用法整理复习以demo的形式复习还是喜欢看我自己写的东西符合我的个人逻辑头文件为#includethread,通过std::thread应用。就以Hello thread开始吧需要注意的是1join()函数和detach()函数的区别,2数据同步操作mutex需包含includemutex互斥锁
1、 普通函数多线程调用
1无参数函数
#include thread
#include iostreamvoid hello_thread() {std::cout Hello Thread! std::endl;
}int main() {std::thread t1(hello_thread); t1.join(); // 主线程等待子线程执行结束之后才会执行下一步的操作std::cout Main here std::endl;getchar();return 0;
}
2有参数函数
#include thread
#include iostreamusing namespace std;int msum(int a, int b) {int c a b;cout a b c endl;return c;
}int main() {std::thread t1(msum,1,2);t1.join(); // 主线程等待子线程执行结束之后才会执行下一步的操作std::cout Main here std::endl;getchar();return 0;
}
2、在类内部创建线程
1类内部函数为静态函数
#include thread
#include iostreamusing namespace std;class HelloThread{public:static void hellothread(){cout Hello World, I‘m a thread endl;}static void start(){thread thread(hellothread);thread.join();}};int main(){HelloThread::start();getchar();return 0;
}
在这里start()和hellothread()方法都必须是static方法。
2在Singleton模式内部创建线程
3 、用类内部函数在类外部创建线程
非常普通的类只是用多线程调用其内部的函数
#include thread
#include iostreamusing namespace std;class HelloWorld{public:void hello(int year){cout I am year years old! endl;}
};int main(){HelloWorld obj;thread thrd(HelloWorld::hello,obj,26);thrd.join();getchar();return 0;
} join和detach的区别
join的作用前面已经提到主线程等待子线程结束方可执行下一步串行detach是的子线程放飞自我独立于主线程并发执行主线程后续代码段无需等待。看看效果
1join()
#include thread
#include iostreamusing namespace std;void hello_thread() {std::cout Hello Thread! std::endl;
}int msum(int a, int b) {int c a b;cout a b c endl;return c;
}int main(){thread t1(hello_thread);//主线程等待子线程运行结束之后才可以执行下一步t1.join();thread t2(msum,2,3);t2.join();//主线程代码cout Main here endl;getchar();return 0;
}
2detach
#include thread
#include iostreamusing namespace std;void hello_thread() {std::cout Hello Thread! std::endl;
}int msum(int a, int b) {int c a b;cout a b c endl;return c;
}int main(){thread t1(hello_thread);//主线程等待子线程运行结束之后才可以执行下一步t1.detach();thread t2(msum,2,3);t2.detach();//主线程代码cout Main here endl;getchar();return 0;
} 数据同步线程同时操作一个数据的安全性 执行单个线程上锁、解锁维护了线程的安全性防止线程之间交叉执行
#include thread
#include iostream
#include mutex
using namespace std;
std::mutex mt;
int data1 1;void addmethod(int a){mt.lock();data1 a;cout add data data1 endl;mt.unlock();
}
void multi(int a){mt.lock();data1 * a;cout multi data data1 endl;mt.unlock();
}
void hello_thread() {std::cout Hello Thread! std::endl;
}int msum(int a, int b) {int c a b;cout a b c endl;return c;
}int main(){thread t1(addmethod,2);thread t2(multi,2);t1.detach();t2.detach();getchar();return 0;
}
线程暂停
从外部让线程暂停会引发很多并发问题此处不做引申。这大概也是std::thread并没有直接提供pause函数的原因。但有时线程在运行时确实需要“停顿”一段时间怎么办呢可以使用std::this_thread::sleep_for或std::this_thread::sleep_until
#include thread
#include iostream
#include mutex
#include chrono
#include ratio
using namespace std;
std::mutex mt;void pausable(){//sleep 500毫秒this_thread::sleep_for(chrono::milliseconds(500));//sleep 到达指定的时间点this_thread::sleep_until(chrono::system_clock::now() chrono::milliseconds(500));
}
int main(){std::thread thread1(pausable);thread1.join();return 0;
}
线程停止
一般情况下当线程函数执行完成后线程“自然”停止。但在std::thread中有一种情况会造成线程异常终止那就是析构。当std::thread实例析构时如果线程还在运行则线程会被强行终止掉这可能会造成资源的泄漏因此尽量在析构前join一下以确保线程成功结束。如果确实想提前让线程结束怎么办呢一个简单的方法是使用“共享变量”线程定期地去检测该量如果需要退出则停止执行退出线程函数。使用“共享变量”需要注意在多核、多CPU的情况下需要使用“原子”操作。
参考链接
https://blog.csdn.net/ouyangfushu/article/details/80199140