Windows 多线程编程入门(1)
看了网上一些说法,总结以下几点: std::thread
2:C++本身还支持pthread这个API来进行多线程编程。 开始实践 构造新线程 thread() noexcept
构造出的线程对象不会执行。 2:正常的初始化函数: template<class T,class ...Args> explicit thread(T&& t,Args&&... args);
函数功能:构造出一个新的线程,并直接开始执行,新执行的线程调用函数 t ,并传递 args 作为参数。 3:删除线程: thread(const thread&) = delete;
一些函数的介绍 if(t1.joinable())
t1.join();
使用前最好加个判断 2:detach函数 t1.detach
参数问题 转移线程的所有权 thread t1(f1);
thread t3(move(t1));
将线程从t1转移给t3,这时候t1就不再拥有线程的所有权,调用t1.join或t1.detach会出现异常,要使用t3来管理线程。这也就意味着thread可以作为函数的返回类型,或者作为参数传递给函数,能够更为方便的管理线程。 获取线程的ID t1.get_id();
互斥锁 void thread01()
{
while (totalNum > 0)
{
mu.lock(); //同步数据锁
cout << totalNum << endl;
totalNum--;
Sleep(100);
mu.unlock(); //解除锁定
}
}
异常情况的处理 class thread_guard
{
thread &t;
public:
explicit thread_guard(thread& _t) :
t(_t) {}
~thread_guard()
{
if (t.joinable())
t.join();
}
thread_guard(const thread_guard&) = delete;
thread_guard& operator=(const thread_guard&) = delete;
};
void func() {
thread t([] {
cout << "Hello thread" << endl;
});
thread_guard g(t);
}
lambda表达式与thread void TestLambda()
{
for (int i = 0; i < 100; i++)
{
std::thread t([i]() {
std::cout << i << std::endl;
});
t.detach();
}
}
好了,代码奉上: /*time:20180605 author:MISAYAONE */
#include<iostream>
#include<thread>
#include<Windows.h>
#include<mutex>
using namespace std;
mutex mu;//互斥锁对象
int num = 100;
void thread_func1()
{
for (int i=0;i<10;i++)
{
cout <<"thread 1's "<< i << endl;
Sleep(100);
}
while (num>0)
{
mu.lock();//互斥锁加锁
cout << num << endl;
num--;
mu.unlock();//互斥锁解锁
}
}
void thread_func2(int n)
{
for (int i = 0; i<n; i++)
{
cout <<"thread 2's "<< i << endl;
Sleep(200);
}
while (num>0)
{
mu.lock();
cout << num << endl;
num--;
mu.unlock();
}
}
class A
{
public :
void thread_func3()
{
for (int i = 0; i<10; i++)
{
cout << "class A thread 3's " << i << endl;
Sleep(200);
}
}
};
class thread_guard
{
thread &t;
public:
explicit thread_guard(thread& _t):t(_t){}
~thread_guard()
{
if (t.joinable())
{
t.join();
}
}
//拷贝赋值默认去除
thread_guard(const thread_guard&) = delete;
thread_guard& operator=(const thread_guard&) = delete;
};
int main()
{
A a;
thread task00();//默认构造函数
thread task01(thread_func1);//正常构造函数
thread_guard g(task01);//出现异常后,函数退出,g局部对象对销毁,此时g调用析构函数,会自动执行task01的join函数,从而能够保证join一定会被调用
thread task02(thread_func2,10);//带args的线程构造函数
thread task_i(move(task02));//转移线程所有权,此时再利用task02调用join和detach报错
thread task03(&A::thread_func3,a);//传入类的成员函数作为线程构造函数的参数
//join函数顺序执行线程
task01.join();
//task02.join();
//task03.join();
//不影响当前代码的执行,几个线程后台自行执行,不阻塞主线程
//task01.detach();
//task02.detach();
task03.detach();
//lambda匿名函数
for (int j=0;j<20;j++)
{
thread t([j]() {cout << j << endl; });
t.detach();
}
for (int i = 0; i<10; i++)
{
cout << "main thread's " << i << endl;
Sleep(200);
}
cout << "task01.get_id() = " << task01.get_id() << endl;//获取线程ID
cin.get();
return 0;
} (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 让WindowsForm缩小到系统匣
- Caused by com microsoft sqlserver jdbc SQLServerExcepti
- Windows – Qt Phonon:在某些计算机上无法播放
- active-directory – 执行到2012 FL Active Directory域/林
- windows-update – 从仅WSUS更新迁移到SCCM.保留现有的批准
- 如何设置win32 api c按钮背景颜色和文字颜色?
- 【记录】解决windows中nginx命名退出了,为什么还能反向代理
- microsoft-metro – 平铺中的Appname – 更新
- windows-server-2012 – Windows Server 2012 IPAM功能 –
- 在Windows资源管理器中搜索只是文件夹
- windows – nt!没有Args的KeWaitForSingleObjec
- windows-server-2003 – 当实际大小为2.7TB时,Me
- 使用Windows PerfMon估算Amazon EBS卷的IOPS
- 如何在Windows上通过XAMPP运行Perl文件?
- windows-phone-7 – 如何获得当前的GPS时间?
- Laravel 5.2在Windows 10上不支持php 7
- Windows – PHP在IIS上占用了大量内存
- 关于printf()long unsigned int和uint32_t的编译
- 调试 – 使用Fiddler调试Windows Phone 7模拟器
- 窗口 – 检测批处理文件的执行方式