C++11之线程学习

在C++11之前,C/C++一直是一种顺序的编程语言。顺序是指所有指令都是串行执行的,即在相同的时刻,有且仅有单个CPU的程序计数器执行代码的代码段,并运行代码段中的指令。而C/C++代码也总是对应地拥有一份操作系统赋予进程的包括堆、栈、可执行的(代码)及不可执行的(数据)在内的各种内存区域。

而在C++11中,一个相当大的变化就是引入了多线程的支持。这使得C/C++语言在进行线程编程时,不比依赖第三方库。

线程可以接收任意个数的参数:

#include <thread>
#include <iostream>
using namespace std;

void func(int a, char ch, const char *str)
{
    std::cout << "a = " << a << "\n";
    std::cout << "ch = " << ch << "\n";
    std::cout << "str = " << str << "\n";
}

int main()
{
    std::thread t(func, 1, 'a', "mike");   //子线程, 需要头文件#include <thread>

    while(1); //特地写一个死循环,让程序不结束

    return 0;
}

运行结果如下:
在这里插入图片描述

回收线程资源
std::thread::join等待线程结束(此函数会阻塞),并回收线程资源,如果线程函数有返回值,返回值将被忽略。

#include <iostream>       // std::cout
#include <thread>         // std::thread, std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds
using namespace std;

void pause_thread(int n)
{
    //指定当前线程休眠一定的时间
    this_thread::sleep_for(chrono::seconds(n));
    cout << "pause of " << n << " seconds ended\n";
}

int main()
{
    cout << "Spawning 3 threads...\n";
    thread t1(pause_thread, 1);
    thread t2(pause_thread, 2);
    thread t3(pause_thread, 3);

    cout << "Done spawning threads. Now waiting for them to join:\n";

    t1.join();//等待线程结束(此函数会阻塞)
    t2.join();
    t3.join();
    cout << "All threads joined!\n";

    return 0;
}

运行结果如下:
在这里插入图片描述

如果不希望线程被阻塞执行,可以调用线程的std::thread::detach,将线程和线程对象分离,让线程作为后台线程去执行。但需要注意的是,detach之后就无法在和线程发生联系了,比如detach之后就不能再通过join来等待执行完,线程何时执行完我们也无法控制。

#include <iostream>       // std::cout
#include <thread>         // std::thread, std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds
using namespace std;

void pause_thread(int n)
{
    this_thread::sleep_for (chrono::seconds(n));
    cout << "pause of " << n << " seconds ended\n";
}

int main()
{
    cout << "Spawning and detaching 3 threads...\n";
    thread(pause_thread,1).detach();
    thread(pause_thread,2).detach();
    thread(pause_thread,3).detach();
    cout << "Done spawning threads.\n";

    cout << "(the main thread will now pause for 5 seconds)\n";

    // give the detached threads time to finish (but not guaranteed!):
    pause_thread(5);

    return 0;
}

在这里插入图片描述

获取线程ID和CPU核心数

#include <iostream>       // std::cout
#include <thread>         // std::thread, std::this_thread::sleep_for
#include <chrono>         // std::chrono::seconds
using namespace std;

void func()
{
    this_thread::sleep_for (chrono::seconds(1));//休眠1秒
    //获取当前线程id
    cout << "func id = " << this_thread::get_id() << endl;
}

int main()
{
    thread t(func);
    cout << "t.get_id() = " << t.get_id() << endl; //获取线程t的id
    cout << "main id = "<<this_thread::get_id() << endl; //主线程id
    cout << "cup num = " << thread::hardware_concurrency() << endl;//获取cpu核心数,失败返回0

    t.join(); //线程阻塞

    return 0;
}

运行结果如下:
在这里插入图片描述
本文来自:https://blog.csdn.net/tennysonsky/article/details/77417280

发布了159 篇原创文章 · 获赞 56 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/qq_35433716/article/details/90667771