material/c++/universal/thread.cpp
2024-11-23 11:00:35 +00:00

36 lines
728 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <thread>
using namespace std;
void func(int a) {
cout << a << endl;
}
mutex mtx; // 互斥量
int main() {
// 同一个进程的多个线程共享该进程的全部系统资源
// 但各个线程有自己的调用栈,寄存器,本地存储
// thread类
// 1.
// thread() noexcept;默认构造函数,不执行任何任务
// 2.
// template<class Funtion, class ...Args>
// explicit thread(Function&& fx,Args&&... args);
thread t1(func, 1); //用2方法创建线程
// mtx.lock();
// mtx.unlock();
if(t1.joinable()) // 判断能否调用join
t1.join(); //回收线程t1
// t1.detach(); //分离线程
return 0;
}
// 注g++编译器要加 -pthread -std=c++11选项编译