C++ thread用法总结(整理)

1,简介

C++11中加入了<thread>头文件,此头文件主要声明了std::thread线程类。C++11的标准类std::thread对线程进行了封装,定义了C++11标准中的一些表示线程的类、用于互斥访问的类与方法等。应用C++11中的std::thread便于多线程程序的移值。

std::thread类成员函数:

(1)、get_id:获取线程ID,返回一个类型为std::thread::id的对象。

(2)、joinable:检查线程是否可被join。检查当前的线程对象是否表示了一个活动的执行线程缺省构造的thread对象、已经完成join的thread对象、已经detach的thread对象都不是joinable。

(3)、join:调用该函数会阻塞当前线程(主调线程)阻塞调用者(caller)所在的线程(主调线程)直至被join的std::thread对象标识的线程(被调线程)执行结束

(4)、detach:将当前线程对象所代表的执行实例与该线程对象分离,使得线程的执行可以单独进行。一旦线程执行完毕,它所分配的资源将会被释放。

(5)、native_handle:该函数返回与std::thread具体实现相关的线程句柄。native_handle_type是连接thread类和操作系统SDK API之间的桥梁,如在Linux g++(libstdc++)里,native_handle_type其实就是pthread里面的pthread_t类型,当thread类的功能不能满足我们的要求的时候(比如改变某个线程的优先级),可以通过thread类实例的native_handle()返回值作为参数来调用相关的pthread函数达到目录。This member function is only present in class thread if the library implementation supports it. If present, it returns a value used to access implementation-specific information associated to the thread.

(6)、swap:交换两个线程对象所代表的底层句柄。

(7)、operator=:moves the thread object

(8)、hardware_concurrency:静态成员函数,返回当前计算机最大的硬件并发线程数目。基本上可以视为处理器的核心数目。

另外,std::thread::id表示线程ID,定义了在运行时操作系统内唯一能够标识该线程的标识符,同时其值还能指示所标识的线程的状态。Values of this type are returned by thread::get_id and this_thread::get_id to identify threads.

有时候我们需要在线程执行代码里面对当前调用者线程进行操作,针对这种情况,C++11里面专门定义了一个命名空间this_thread,此命名空间也声明在<thread>头文件中,其中包括get_id()函数用来获取当前调用者线程的ID;yield()函数(yield,放弃的意思)可以用来将调用者线程跳出运行状态,重新交给操作系统进行调度,即当前线程放弃执行,操作系统调度另一线程继续执行;sleep_until()函数是将线程休眠至某个指定的时刻(time point),该线程才被重新唤醒;sleep_for()函数是将线程休眠某个指定的时间片(time span),该线程才被重新唤醒,不过由于线程调度等原因,实际休眠实际可能比sleep_duration所表示的时间片更长。

1.创建一个线程

创建线程比较简单,使用std的thread实例化一个线程对象就创建完成了,示例:

#include <iostream>
#include <thread>
#include <stdlib.h> //sleep
 
using namespace std;
 
void t1()  //普通的函数,用来执行线程
{
    for (int i = 0; i < 10; ++i)
    {
        cout << "t1111\n";
        sleep(1);
    }
}
void t2()
{
    for (int i = 0; i < 20; ++i)
    {
        cout << "t22222\n";
        sleep(1);
    }
}
int main()
{
    thread th1(t1);  //实例化一个线程对象th1,使用函数t1构造,然后该线程就开始执行了(t1())
    thread th2(t2);
 
    th1.join(); // 必须将线程join或者detach 等待子线程结束主进程才可以退出
    th2.join(); 
 
    //or use detach
    //th1.detach();
    //th2.detach();
 
    cout << "here is main\n\n";
 
    return 0;
}

上述提到的问题,还可以使用detach来解决,detach是用来和线程对象分离的,这样线程可以独立地执行,不过这样由于没有thread对象指向该线程而失去了对它的控制,当对象析构时线程会继续在后台执行,但是当主程序退出时并不能保证线程能执行完。如果没有良好的控制机制或者这种后台线程比较重要,最好不用detach而应该使用join。

2, mutex和std::lock_guard的使用

头文件是#include <mutex>,mutex是用来保证线程同步的,防止不同的线程同时操作同一个共享数据。

但使用lock_guard则相对安全,它是基于作用域的,能够自解锁,当该对象创建时,它会像m.lock()一样获得互斥锁,当生命周期结束时,它会自动析构(unlock),不会因为某个线程异常退出而影响其他线程。示例:

#include <iostream>
#include <thread>
#include <mutex>
#include <stdlib.h>
 
int cnt = 20;
std::mutex m;
void t1()
{
    while (cnt > 0)
    {    
        std::lock_guard<std::mutex> lockGuard(m);
       // std::m.lock();
        if (cnt > 0)
        {
            //sleep(1);
            --cnt;
            std::cout << cnt << std::endl;
        }
       // std::m.unlock();
        
    }
}
void t2()
{
    while (cnt > 0)
    {
        std::lock_guard<std::mutex> lockGuard(m);
        // std::m.lock();
        if (cnt > 0)
        {
            --cnt;
            std::cout << cnt << std::endl;
        }
        // std::m.unlock();
    }
}
 
int main(void)
{
    std::thread th1(t1);
    std::thread th2(t2);
 
    th1.join();    //等待t1退出
    th2.join();    //等待t2退出
 
    std::cout << "here is the main()" << std::endl;
 
    return 0;
}

输出结果,cnt是依次递减的,没有因为多线程而打乱次序::

19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
0
here is the main()

3,详解

绍一下thread的用法。

std::thread 构造函数

thread() noexcept;(1)(C++11 起)
thread( thread&& other ) noexcept;(2)(C++11 起)
template< class Function, class... Args >
explicit thread( Function&& f, Args&&... args );
(3)(C++11 起)
thread(const thread&) = delete;(4)(C++11 起)

  构造新的 thread 对象。

1) 构造不表示线程的新 thread 对象。

2) 移动构造函数。构造表示曾为 other 所表示的执行线程的 thread 对象。此调用后 other 不再表示执行线程。

3) 构造新的 std::thread 对象并将它与执行线程关联。新的执行线程开始执行

4) 复制构造函数被删除; thread 不可复制。没有两个 std::thread 对象可表示同一执行线程。

std::thread::~thread

~thread();

 (C++11 起)

在下列操作后 thread 对象无关联的线程(从而可安全销毁)

std::thread::operator=

thread& operator=( thread&& other ) noexcept;

(1)(C++11 起)

这个例子是借鉴cppreference.com这个网站上面的,我详细的分析一下这个例子。

#include <iostream>
#include <utility>
#include <thread>
#include <chrono>
 
void f1(int n){
    for (int i = 0; i < 2; ++i) {
        std::cout << "Thread 1 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}
 
void f2(int& n){
    for (int i = 0; i < 2; ++i) {
        std::cout << "Thread 2 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
}
 
class foo{
public:
    void bar(){
        for (int i = 0; i < 2; ++i) {
            std::cout << "Thread 3 executing\n";
            ++n;
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }
    int n = 0;
};
 
class baz{
public:
    void operator()(){
        int n = 0;
        for (int i = 0; i < 2; ++i) {
            std::cout << "Thread 4 executing\n";
            ++n;
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
        }
    }
};
 
int main(){
    int n = 0;
    foo f;
    baz b;
    std::thread t1; // t1 不是线程
    std::thread t2(f1, n + 1); // 按值传递
    std::thread t3(f2, std::ref(n)); // 按引用传递
    std::thread t4(std::move(t3)); // t4 现在运行 f2() 。 t3 不再是线程
    std::thread t5(&foo::bar, &f); // t5 在对象 f 上运行 foo::bar()
    std::thread t6(b); // t6 在对象 b 上运行 baz::operator()
 
    t1 = std::thread(f1, n + 1); //t1等号运算符重载
    std::thread t7(std::thread(f2, std::ref(n))); //t7拷贝构造
    t2.join();
    t4.join();
    t5.join();
    t6.join();
    t1.join();
    t7.join();
    std::cout << "Final value of n is " << n << '\n';
    std::cout << "Final value of foo::n is " << f.n << '\n';
}


可以看到只是调用默认构造函数(构造函数(1))构造线程,线程是不会被构造的,如图,线程t1没有被构造

线程t2,t3调用构造函数(3) ,参数Function&& f, Args&&... args,f实质上是一个函数指针,保存线程所要执行的函数的地址args是函数的参数,线程运行f(args),线程t5因为调用的是类的成员方法,this指针作为非静态成员函数的隐含形参,因此我们将this指针即就是对象的地址传进去。

线程t4调用拷贝构造函数(构造函数(2)),thread的拷贝构造函数只有右值拷贝构造,左值的拷贝构造被删除了,右值引用是指函数的参数可以是临时变量,如线程t7,由一个临时变量std::thread(f2, std::ref(n))构造t7。而t4使用的std::move(t3),其中

template< class T >
typename std::remove_reference<T>::type&& move( T&& t ) noexcept;

std::move返回的是一个右值引用的变量,而函数的作用是将t到另一对象有效率的资源传递,传递过后,资源t会被销毁。在这个表达式中的作用就是将线程t3的资源传递给t4,并销毁t3。

线程t6是调用构造函数(3),而这个类里面有operator()(),括号的运算符重载,只要这个对象被使用,那么就会调用括号运算符重载。

线程t1调用等号运算符重载。

最后运行的结果:

线程2-5运行了两遍,所以上面的函数分别打印了2遍,另外线程1,7又将函数f1和f2打印了2遍。

std::thread::joinable

bool joinable() const noexcept;

 (C++11 起)

检查线程是否可合并,即潜在地运行于平行环境中

#include <iostream>
#include <thread>
#include <chrono>
 
void foo(){
    std::this_thread::sleep_for(std::chrono::seconds(1));
}
 
int main(){
    std::thread t;
    std::cout << "before starting, joinable: " << std::boolalpha << t.joinable()
              << '\n';
 
    t = std::thread(foo);
    std::cout << "after starting, joinable: " << t.joinable() 
              << '\n';
 
    t.join();
    std::cout << "after joining, joinable: " << t.joinable() 
              << '\n';
}

 

我们可以看到,在线程为活跃之前,joinable的值为false,在线程活跃之后,joinable的值为true。在线程被join,detach之后,置joinable的值为false。joinable被置为false的情况有

  • 被默认构造
  • 被移动
  • 已调用 join()
  • 已调用 detach()

这些情况和线程被析构的情况都是一样的,因为thread的析构函数是这样实现的

joinable为true的话,会调用terminate(),terminate()会调用abort()来终止程序,程序会报错,因此只有joinable变成false,才能执行析构函数,因此两者运行的必要条件是一样的。

std::thread::join

void join();

 (C++11 起)

 等待线程完成其执行

std::thread::detach

void detach();

 (C++11 起)

容许线程从线程句柄独立开来执行。 

join和detach执行的必要条件都是joinable是true。 

join和detach的区别,join会阻塞当前的线程,直到运行的线程结束,比如在main函数里面调用线程thread,那么main函数里面调用thread后,会先去执行thread中的代码逻辑,直到其结束,再去执行main函数里面的代码逻辑。调用join后,所有分配的资源都会被释放。在调用了join之后,*this就不会拥有任何线程了。

detach从线程对象中分离出执行线程,允许线程独立的执行。一旦线程退出,所有分配的资源都会被释放。在调用了detach之后,*this就不会拥有任何线程了。

以上图片借鉴https://www.cnblogs.com/ittinybird/p/4820142.html,这篇文章也写得很好,大家可以看一下。 

可能光说还是有点抽象,上代码:

#include <iostream>
#include <thread>
#include <chrono>
 
void foo(){
    for (int i = 0; i < 5; ++i){
        std::cout << "void foo()" << std::endl;
    }
}
 
void bar(){
    for (int i = 0; i < 5; ++i) {
        std::cout << "void bar()" << std::endl;
    }
}
 
int main(){
    std::thread helper1(foo);
    std::thread helper2(bar);
 
    helper1.join();
    helper2.join();
    for (int i = 0; i < 5; ++i){
        std::cout << "done!\n";
    }
}


#include <iostream>
#include <thread>
#include <chrono>
 
void foo(){
    for (int i = 0; i < 5; ++i){
        std::cout << "void foo()" << std::endl;
    }
}
 
void bar(){
    for (int i = 0; i < 5; ++i) {
        std::cout << "void bar()" << std::endl;
    }
}
 
int main(){
    std::thread helper1(foo);
    std::thread helper2(bar);
 
    helper1.detach();
    helper2.detach();
    for (int i = 0; i < 5; ++i){
        std::cout << "done!\n";
    }
    system( "pause");
}

可以看出来,detach(拆卸,分开)不会阻塞主线程,主线程和子线程是同时运行的。而join(合并)是会阻塞主线程,等待子线程运行完后,再运行主线程

  • 36
    点赞
  • 241
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
ListView是C++Builder中常用的一种控件,用于以列表的形式展示数据。下面是ListView控件的用法和示例总结。 1. 加入控件 在C++Builder中,我们可以通过拖拽的方式将ListView控件添加到窗体中。也可以在代码中使用下面的代码创建控件并添加到窗体中。 ```cpp TListView *lv = new TListView(Form1); lv->Parent = Form1; lv->Align = alClient; ``` 2. 设置样式 ListView控件提供了多种样式,可以通过Style属性来设置。我们可以设置为vsReport模式来展示表格,也可以设置为vsIcon模式来展示图标等。 ```cpp lv->ViewStyle = vsReport; ``` 3. 添加列 在ListView中,我们需要添加列来表示数据的不同字段。可以通过Columns属性来访问列,并使用Add方法来添加列。 ```cpp lv->Columns->Add()->Caption = "Name"; lv->Columns->Add()->Caption = "Age"; ``` 4. 添加行 在ListView中,我们需要添加行来表示数据的不同记录。可以通过Items属性来访问行,并使用Add方法来添加行。 ```cpp TListItem* item = lv->Items->Add(); item->Caption = "Tom"; item->SubItems->Add("18"); ``` 5. 设置单元格数据 在ListView中,我们可以通过访问行和列的交叉位置来设置单元格数据。例如,我们可以通过下面的代码设置第2行第1列的数据为"Jerry"。 ```cpp lv->Items->Item[1]->SubItems->Strings[0] = "Jerry"; ``` 6. 选择行 在ListView中,我们可以通过Selected属性来访问选中的行。例如,我们可以通过下面的代码获取选中行的数量。 ```cpp int count = lv->Selected->Count; ``` 7. 删除行 在ListView中,我们可以通过Items属性访问行,并使用Delete方法来删除行。例如,我们可以通过下面的代码删除第2行。 ```cpp lv->Items->Delete(1); ``` 8. 排序 在ListView中,我们可以通过SortType属性来设置排序方式。例如,我们可以通过下面的代码将ListView按照第2列升序排序。 ```cpp lv->SortType = stData; lv->Column[1]->Tag = 0; lv->CustomSort(NULL, 1); ``` 以上就是ListView控件的用法和示例总结。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值