【c++】 thread

thread

创建一个简单线程

#include <iostream>
#include <thread>
#include <utility>
#include <chrono>


void f1(int n)
{
    for (int i=0;i<5;i++)
    {
        std::cout << "Thread 1 executing\n";
        ++n;
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}

void f2(int& n)
{
    for (int i=0;i<5;i++)
    {
        std::cout << "Thread 2 executing\n";
        ++n;
        //Blocks the execution of the current thread for at least the specified sleep_duration
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
}

int main()
{
    int n = 1;
    std::thread t1(f1,10);
    std::thread t2(f2,std::ref(n));

    std::thread::id t1_id = t1.get_id();
    std::thread::id t2_id = t2.get_id();
    std::cout << "t1's id: " << t1_id << '\n';
    std::cout << "t2's id: " << t2_id << '\n';
    t1.join(); // waits for the thread to finish its execution
    t2.join();
    std::cout << "now n = " << n << std::endl;

}
// g++ thread.cpp -o thread -Wall -g -lpthread

mutex

mutex是用来保证线程同步的,防止不同的线程同时操作同一个共享数据

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


std::map<std::string,std::string> page;
std::mutex m;

void save_page(const std::string& url)
{
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
    std::string value = "res";
    std::lock_guard<std::mutex> lg(m); // lock_guard implements a strictly scope-based mutex ownership wrapper
    // m.lock();
    page[url] = value; 
    // m.unlock();
}

void work_add(std::mutex& m,int& a)
{
    std::lock_guard<std::mutex> lk(m);
    for (int i=0;i<5;i++)
    {
        a = a+1;
        std::cout << "add count = " << a <<std::endl;
    }
}


int main()
{
    std::thread t1(save_page,"abc");
    std::thread t2(save_page,"124");
    t1.join();
    t2.join();
    for (const auto& pair:page)
    {
        std::cout <<"key = "<< pair.first << ", " << "value = " << pair.second <<std::endl;
    }

    int a = 0;
    std::mutex m2;
    // 参数上要使用引用的形式
    std::thread t3(work_add,std::ref(m2),std::ref(a)),t4(work_add,std::ref(m2),std::ref(a));
    t3.join();
    t4.join();

    std::cout << "a = " << a << std::endl;

}

condition variable


#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <chrono>

std::mutex m;
std::condition_variable cv;
int i=0;


void work()
{
    std::unique_lock<std::mutex> lk(m);
    std::cout << "waiting ..." <<std::endl;
    cv.wait(lk,[]{return i == 1;});
    std::cout << "finish ..." <<std::endl;
}


int main()
{
    std::thread t1(work),t2(work),t3(work);
    std::cout << "first notice" <<std::endl;
    cv.notify_one();
    // 先让其他线程拿到锁,然后再让主线程拿锁,一定要保证所有的work都已经拿到锁并且处在wait的状态
    std::this_thread::sleep_for(std::chrono::seconds(1)); 
    std::unique_lock<std::mutex> lk(m); // 主线程拿锁
    std::cout << "change i = 1" << std::endl;
    i=1;
    lk.unlock();
    std::cout << "notify one" << std::endl;
    cv.notify_one();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    lk.lock();

    lk.unlock();
    std::cout << "notify all" << std::endl;
    cv.notify_all();
    std::this_thread::sleep_for(std::chrono::seconds(1));
    lk.lock();

    t1.join();
    t2.join();
    t3.join();


}

promise

#include <future>
#include <thread>
#include <chrono>
#include <iostream>

void set_promise(std::promise<int> p)
{
    std::this_thread::sleep_for(std::chrono::milliseconds(1000));
    p.set_value(100);
}


int main()
{
    std::promise<int> p_int;
    std::future<int> f_int = p_int.get_future();
    // 一个std::promise实例只能与一个std::future关联共享状态,当在同一个std::promise上反复调用get_future会抛出future_error异常
    // std::future<int> f_int2 = p_int.get_future();
    std::thread t(set_promise,std::move(p_int));
    std::cout << "f_int.get()=" << f_int.get() << std::endl;
    // 一定要加join,等待线程结束
    t.join();   
    return 0;
}
//g++ thread_future2.cpp -o future2 -Wall -g -lpthread
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值