C++多线程

#include <thread>
#include <atomic>
#include <iostream>
#include <list>
#include <future>

using namespace std;

int iCount = 10;

void threadfun1()
{
    while (true)
    {
        printf("iCount1:%d\r\n", iCount--);
        std::this_thread::sleep_for(std::chrono::seconds(1));
        if (iCount < 0)
        {
            break;
        }
    }
}

void threadfun2()
{
    int i = 10;
    while (true)
    {
        printf("iCount2:%d\r\n", iCount--);
        if (iCount < 0)
        {
            break;
        }
    }
}

int fun(int x) {
    x++;
    x *= 10;
    std::cout << std::this_thread::get_id() << std::endl;
    // std::this_thread::sleep_for(std::chrono::seconds(5));
    iCount = 99;
    return x;
}

int main()
{
    printf("in major thread1\r\n");
    thread t1(threadfun1);
    printf("in major thread2\r\n");
    thread t2(threadfun2);
    
    printf("in major thread3\r\n");
    std::future<int> fu = std::async(std::launch::async, [](){
        return fun(1);
    });
    std::cout << "async "<< fu.get() << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(100));
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。