c++ 线程并发---线程同步

1.什么是同步?

   某个线程需要等待另外一个线程的任务完成,才可以执行的自己的任务,被称为同步。

 2.c++ 条件变量

#include <condition_variable>
#include <mutex>
#include <stdio.h>
#include <thread>
#include <unistd.h>

static std::mutex              mtx;
static std::condition_variable cv;
void                           runthread(int index)
{
    while (1)
    {
        std::unique_lock< std::mutex > lck(mtx);  //上锁
        fprintf(stderr, "thread:%d is waiting.\n", index);
        cv.wait(lck);  // wait 解锁,然后加锁
        fprintf(stderr, "thread:%d is sleep.\n", index);
        std::this_thread::sleep_for(std::chrono::seconds(5));
        fprintf(stderr, "thread:%d is wakeup.\n", index);
    }
}

void runthread_1(int index)
{
    std::unique_lock< std::mutex > lck(mtx); //
    fprintf(stderr, "thread:%d is waiting.\n", index);
    std::this_thread::sleep_for(std::chrono::seconds(2));
    fprintf(stderr, "thread:%d is wakeup.\n", index);
}
int main()
{
    fprintf(stderr, "pid:%d\n", getpid());

    std::thread* pt[5];
    for (int i = 0; i < 5; i++)
    {
        pt[i] = new std::thread(runthread, i);  //创建5个线程
    }

    for (int i = 0; i < 3; i++)
    {
        std::this_thread::sleep_for(std::chrono::seconds(1));
        fprintf(stderr, "main thread sleep %d seconds\n", i);
    }
    cv.notify_all();     
    //  cv.notify_one();    // 唤醒其中的一个线程,此时该线程会执行完cv.wait(lck);
    // 由阻塞状态变成运行状态,此时m_mtx会锁上,因此,下面的runthread_1会被锁上,进而不能够打印输出
    // new std::thread(runthread_1, 10);

    for (int i = 0; i < 5; i++)
    {
        if (pt[i]->joinable())
        {
            pt[i]->join();
            delete pt[i];
            pt[i] = NULL;
        }
    }
    return 0;
}

3.std::future

    使用一次性future来模拟一类事件,若线程需要等待某一个事件发生,则会以一个恰当的方式取得一个future,它代表目标事件,可以一边等待事件的发生,一边执行其他任务。

    就好比: 去高铁站做高铁,你要等待高铁的到来,而在高铁来之前,你可以吃饭,玩手机等,等待其他事情的发生。

#include<iostream>
#include<mutex>
#include<future>
#include<unistd.h>
using namespace std;
//g++ -std=c++0x -pthread future.cpp
int printname(string a,int n)
{
       for(int i=0;i<n;i++)
       {    
            cout<<a<<endl;
            sleep(1);
       }
       return n-1;
}

class Add
{
    public:
    int  add(int x,int y)
    {
        return x+y;
    }
};
int main()
{
    std::future<int> f1 = std::async(printname,"thread1",3); //异步处理
    cout<<f1.get()<<endl; //f1.get() 会休眠,这里会等async 返回之后,
    Add x;
    auto f2 = std::async(&Add::add,x,1,2); //异步处理一个任务
    cout<<f2.get()<<endl; 
}

thread1
thread1
thread1
2
3

4.c++ 异步处理--std::async

参数列表:

std::launch::deferred //等待future运行wait或者get时才会继续运行。

std::launch::async    //运行新的线程

std::launch::deferred| std::launch::async  //交由实现自行选择运行方式

    auo f3=std::async(std::launch::async,printname,"thread2",3);
    auo f4=std::async(std::launch::deferred,printname,"thread3",3);
    auo f5=std::async(std::launch::deferred|std::launch::async,printname,"thread4",3);   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值