c++11 语言级线程

c++11 语言级线程

 

线程的创建

用std::thread创建线程非常简单,只需要提供线程函数或函数对象即可,并且可以同时指定线程函数的参数。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <chrono>
#include <thread>

void func1()
{
    while (true)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(3000));
        std::cout << __FUNCTION__ << std::endl;
    }
}

void func2()
{
    while (true)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(3000));
        std::cout << __FUNCTION__ << std::endl;
    }
}

void func3(int a, char ch, const char *str)
{
    std::this_thread::sleep_for(std::chrono::milliseconds(3000));
    std::cout << "a = " << a << std::endl;
    std::cout << "ch = " << ch << std::endl;
    std::cout << "str = " << str << std::endl;
}


void mytest()
{
    std::thread t1(func1); // 子线程1
    std::thread t2(func2); // 子线程2

    // 线程还可以接收函数的参数
    std::thread t3(func3, 1, 'a', "xyz");   //子线程3
    std::thread t4(func3, 2, 'a', "abc");   //子线程3

    while(true) // 主线程
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(3000));
        std::cout << __FUNCTION__ << std::endl;
    }

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

 

回收线程资源

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

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <chrono>
#include <thread>

void puase_thread(int no, int n)
{
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "thread-" << no << " pause of " << n << " seconds" << std::endl;
}

void mytest()
{
    //std::cout << "Spawning 3 threads..." << std::endl;
    std::thread t1(puase_thread, 1, 3);
    std::thread t2(puase_thread, 2, 3);
    std::thread t3(puase_thread, 3, 3);

    std::cout << "Done spawning threads. Now waiting for them to join:" << std::endl;
    t1.joinable() ? t1.join() : std::cout << "Thread ID-" << t1.get_id() << " can not join" << std::endl;
    t2.joinable() ? t2.join() : std::cout << "Thread ID-" << t2.get_id() << " can not join" << std::endl;
    t3.joinable() ? t3.join() : std::cout << "Thread ID-" << t3.get_id() << " can not join" << std::endl;
    std::cout << "All threads joined!" << std::endl;

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

 

如果不希望线程被阻塞执行,可以调用线程的std::thread::detach(此函数不会阻塞),将线程和线程对象分离,让线程作为后台线程去执行。

但需要注意的是,detach之后就无法在和线程发生联系了,比如detach之后就不能再通过join来等待执行完,线程何时执行完我们也无法控制。

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <chrono>
#include <thread>

void puase_thread(int no, int n)
{
    std::this_thread::sleep_for(std::chrono::seconds(n));
    std::cout << "thread-" << no << " pause of " << n << " seconds" << std::endl;
}

void mytest()
{
    //std::cout << "Spawning 3 threads..." << std::endl;
    std::thread t1(puase_thread, 1, 3);
    std::thread t2(puase_thread, 2, 3);
    std::thread t3(puase_thread, 3, 3);

    std::cout << "Done spawning threads. Now waiting for them to join:" << std::endl;
    t1.joinable() ? t1.detach() : std::cout << "Thread ID-" << t1.get_id() << " can not detach" << std::endl;
    t2.joinable() ? t2.detach() : std::cout << "Thread ID-" << t2.get_id() << " can not detach" << std::endl;
    t3.joinable() ? t3.detach() : std::cout << "Thread ID-" << t3.get_id() << " can not detach" << std::endl;
    std::cout << "All threads joined!" << std::endl;

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

 

获取线程ID和CPU核心数

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <chrono>
#include <thread>

void puase_thread(int n)
{
    std::this_thread::sleep_for(std::chrono::seconds(n));
    // std::this_thread::get_id() 获取线程ID
    std::cout << "thread-" << std::this_thread::get_id() << " pause of " << n << " seconds" << std::endl;
}

void mytest()
{
    //std::cout << "Spawning 3 threads..." << std::endl;
    std::thread t1(puase_thread, 3);
    std::thread t2(puase_thread, 3);
    std::thread t3(puase_thread, 3);

    std::cout << "main id: " << std::this_thread::get_id() << std::endl; // 主线程id
    std::cout << "cpu num: " << std::thread::hardware_concurrency() << std::endl; // 获取cpu核心数,失败返回0

    std::cout << "Done spawning threads. Now waiting for them to join:" << std::endl;
    // t1.get_id() // 获取t1此线程的ID
    t1.joinable() ? t1.detach() : std::cout << "Thread ID-" << t1.get_id() << " can not detach" << std::endl;
    t2.joinable() ? t2.join() : std::cout << "Thread ID-" << t2.get_id() << " can not join" << std::endl;
    t3.joinable() ? t3.join() : std::cout << "Thread ID-" << t3.get_id() << " can not join" << std::endl;
    std::cout << "All threads joined!" << std::endl;

    return;
}

int main()
{
    mytest();

    system("pause");
    return 0;
}

 

posted @ 2017-11-05 21:50  lsgxeva  阅读(541)  评论(0编辑  收藏  举报