2018-05-15

伪函数和多线程

struct func
{
    //伪函数
    void operator()()  //方法  可以将对象名当作函数名使用
    {
        cout << "hello1" << endl;
    }

    void operator()(int i)  //
    {
        cout << "hello2" <

//void operator()() 对象名 当作函数名 重载()
//()仅使用于当前结构体对象
//mystruct()构造函数 创建一个临时对象 既匿名对象

struct mystruct
{
    mystruct()
    {
        cout << "creat" << endl;
    }
    ~mystruct()
    {
        cout << "end" << endl;
    }

    void operator()()
    {
        MessageBoxA(0,"AABCL","123",0);
    }
};

void mainu2()
{
    mystruct go1;
    thread t1(go1);

    mystruct go2;
    thread t2(go2);

    thread t3(mystruct()); //不适合作为多线程参数  销毁太快

    //mystruct()构造函数  创建一个临时对象 既匿名对象
    mystruct()();

    mystruct*p = new mystruct(); //() 初始化  构造函数

    cin.get();
}

成员函数与多线程

struct funx
{
public:
    static void run()
    {
        MessageBoxA(0, "SSSS", "RRRR", 0);
        cout << "hello" << endl;
    }

    static void runl(const char*str)
    {
        MessageBoxA(0, str, "RRRR", 0);
        cout << "hello" << endl;
    }
};

void main()
{
    //funx *p(nullptr);
    // p->run(); //空类指针可以引用没有调用内部成员变量的成员函数
    funx fun1;
    //&funx::run() 引用成员函数
    thread th1(&funx::run(),fun1);
    thread th2(&funx::run(), fun1);
    thread th3(&funx::runl(), fun1,"DDD"); //传递参数模式
    thread th4(&funx::runl(), fun1,"DDD");

    cin.get();
}

多线程通信

#include
#include
#include

promise val;  //全局通信变量

void mainu4()
{
    string str1("12345");
    string str2("12345");
    string str3(str1+str2); //C++风格字符串
    //cout << str3 << endl;

    thread th1([]() 
    {
        future fu = val.get_future(); //获取未来的状态
        cout << "等待中" << endl;
        cout << fu.get() << endl;
    });

    thread th2([]() 
    {
        system("pause");
        val.set_value("HHHHHHHHHHHHHHH");
        system("pause");
    });

    th1.join();
    th2.join();

    cin.get();
}

线程功能扩展基于继承

class huathread:public thread  //C++代码重用继承
{
public:
    huathread() :thread() //子类调用父类构造函数
    {
    
    }

    template  //子类调用父类构造函数 ,可变参数的构造
    huathread(T&& func, Args&&...args): thread(   forward(func),    forward(args)...   )
    {


    }

    void run(const char*cmd)
    {
        system(cmd);
    }

};

void mainu5()
{
    huathread th1([]() {cout << "HHHH" << endl; });
    th1.run("calc");

    huathread th2([](int num) {cout << "HHHH" << num<

条件变量

#include
#include

//线程通信 结合mutex
//一个线程 多个线程处于等待,通知一个或者多个

mutex m; //线程互相排斥
condition_variable cv; //线程通信

void mainu6()
{
    thread **ppth = new thread*[10]; //开辟线程指针数组
    for (size_t i = 0; i < 10; i++)
    {

        ppth[i] = new thread([](int index) {
            unique_lock lck(m);//锁定
            cv.wait_for(lck,chrono::hours(1000));//一直等待
            cout << index << endl;  //打印编号

        },i); //传递参数

        this_thread::sleep_for(chrono::milliseconds(100)); //错开
    }

    //for (size_t i = 0; i < 10; i++)
    //{
    //  lock_guard lckg(m); //解锁向导
    //  //cv.notify_one(); //挨个通知
    //}

    for (size_t i = 0; i < 10; i++)
    {
        lock_guard lckg(m); //解锁向导
    }
    cv.notify_all(); //通知全部


    for (size_t i = 0; i < 10; i++)
    {
        ppth[i]->join();
        delete ppth[i];
    }
    delete[] ppth;

    cin.get();
}

并行计算 获取结果

#include //线程将来结果
#include //时间
#include

mutex g_m;

void mainu7()
{
    auto run = [=](int index) ->int
    {
        lock_guard lckg(g_m); //加锁
        cout << this_thread::get_id() <<"  "<< index << endl; //获取线程id
        this_thread::sleep_for(chrono::seconds(10));
        return index * 1024; //返回结果
    };

    packaged_task pt1(run);
    packaged_task pt2(run);  //创建两个任务包

    thread t1([&]() {pt1(2);});
    thread t2([&]() {pt2(3);}); //开启线程

    cout << pt1.get_future().get() << endl;
    cout << pt2.get_future().get() << endl; //获取结果

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



    cin.get();
}

vector介绍

//vector 动态数组 无限嵌套
//增删查改,动态的数据结构 线性表的顺序存储

void mainu8()
{
    //vector myint{ 1,2,3,4,5 };
    //

    //for (int i = 10; i < 20; i++)
    //{
    //  myint.push_back(i);
    //}

    //myint.resize(5);  //动态调整大小
    //myint.resize(6,90); //vector  6个数据 缓冲90
    
    //cout << myint.front() << endl;
    //cout << myint.back() << endl;

    //cout << "============================" << endl;
    //for (auto i : myint)
    //{
    //  cout << i << endl;
    //}

    //for (size_t i = 0; i < myint.size(); i++)
    //{
    //  cout << myint[i] <<"  "<< myint.at(i) << endl;
    //}
    ////迭代器访问 本质就是指针
    //for (auto ib = myint.begin(), ie = myint.end();ib != ie;ib++) //顺序
    //{
    //  cout << *ib << endl;
    //}
    //for (auto rb = myint.rbegin(), re = myint.rend();rb != re;rb++) //逆序
    //{
    //  cout << *rb << endl;
    //}

    vector myintx{ 11,12,13,14,15 };
    vector myint1{ 1,2,3,4,5 };
    //myint1.assign(7,100); //重新初始化
    auto it = myint1.begin() + 3;
    int a[5]{ 21,22,23,24,25 };

    //it = myint1.insert(it,400); //根据位置插入
    //cout << "*it:" << *it << endl;

    //it = myint1.insert(it, a, a + 5); //插入一个数组
    //it = myint1.insert(it,myintx.begin(),myintx.end()); //批量插入

    //myint1.erase(it);
    myint1.erase(myint1.begin(),myint1.begin()+3); //批量删除 不包含最后一个
    

    vector myint2{ 1,2,3,4,5 };
    vector myint3{ 31,32,33,34,35 };  //堆上 不会栈溢出
    myint2.swap(myint3);

    //for (auto i : myint2)
    //{
    //  cout << i << endl;
    //}

    //for (int i = 0; i < 5; i++) //内存是连续的
    //{
    //  cout << myint2[i] << "  " << &myint2[i] << endl;

    //}

    vector myint;
    int*p = myint.get_allocator().allocate(5); //获取分配器分配内存
    cout << myint.size() << endl << endl << endl;

    for (size_t i = 0; i < 5; i++)
    {
     // cout << (p[i] = i) << endl;
    }
    myint.get_allocator().deallocate(p,5); //释放内存


    vector myintx1{ 1,2,3,4,5 };
    vector myintx2{ 21,2,3,4,5 ,6,7};
    vector myintx3{ 1,2,3 };

    vector< vector  > myallintx{ myintx1,myintx2,myintx3 };
    for (auto i : myallintx)
    {
        for (auto j : i)
        {
            cout << j << " ";
        }
        cout << endl;
    }


    cin.get();
}


可变参数与多线程

#include

//可变参数
int go(const char*fmt, ...)
{
    va_list ap; //指针
    va_start(ap, fmt); //开始
    vprintf(fmt,ap); //调用
    va_end(ap);  //结束

    return 0;
}

void mainu9()
{
    thread th(go, "%sABCD%d______%c_____%x", "12345as", 98, 'c', 256);

    cin.get();
}

并行计算结果汇总

#include
#include
#include

#define COUNT 5000000

int add(vector*arr,int start,int count)
{
    static mutex m;//只会初始化一次
    int sum(0);
    for (int i = 0; i < count; i++)
    {
        sum += (*arr)[start + i];//实现累加
    }

    {
        //显示结果必须  仅仅计算多余  加锁
        lock_guard lckg(m); //锁定
        cout << "  thread:" << this_thread::get_id()
            << "  count=" << count
            << "  sum=" << sum << endl; //打印结果
    
    }

    return sum;
}

void mainu10()
{
    vector data(COUNT); //数组

    for (int i = 0; i < COUNT; i++)
    {
        data[i] = (i + 1) % 1000; //0-999
    }

    vector > result; //结果数组
    int cpus = thread::hardware_concurrency(); //CPU核心个数

    for (int i = 0; i < cpus*2; i++) //4核CPU  总共创建8个线程任务
    {
        //线程任务分割

        int batch_each = COUNT / (cpus * 2);
        if (i == (cpus * 2) - 1)
        {
            batch_each=COUNT- COUNT / (cpus * 2)*i; //最后一个线程 承担多点

        }
        //不断压入结果
        result.push_back(  async(add, &data, i*batch_each,batch_each)  );  // 
        //async返回结果

    }

    //汇总
    int lastresult(0);
    for (int i = 0; i < cpus * 2; i++)
    {
        lastresult += result[i].get(); //汇总结果
    }

    cout << "lastresult= " << lastresult << endl;

    cin.get();
}

死锁与解锁

#include
#define COUNT1 1000000

mutex g_mutex1, g_mutex2; //互斥量

void add1(int*p1,int*p2)
{
    for (int i = 0; i < COUNT1; i++)
    {
        g_mutex1.lock();
        (*p1)++;
        g_mutex1.unlock();

        g_mutex2.lock();
        (*p2)++;
        g_mutex2.unlock();
    }
}


void add2(int*p1, int*p2)
{
    for (int i = 0; i < COUNT1; i++)
    {

        g_mutex2.lock();
        (*p2)++;
        g_mutex2.unlock();

        g_mutex1.lock();
        (*p1)++;
        g_mutex1.unlock();
    }
}


//thread 引用类型函数 模板  避免类型转换  尽量指针  少使用引用
//锁住一个变量  尽快操作完解锁  不要再锁其他变量  否则互锁
void mainu11()
{
    int a = 0;
    int b = 0;
    thread th1(add1, &a, &b);
    thread th2(add2, &a, &b);
    th1.join();
    th2.join();

    while (1)
    {
        cout << a << endl;
        cout << b << endl;
        this_thread::sleep_for(chrono::seconds(3));
    }

    cin.get();
}

迅雷线程面试题与线程交换与移动

题目:

//编写一个程序 开启3个线程 这3个线程ID分别为A B C
//每个线程将自己ID在屏幕上打印10遍
//要求输出结构必须按ABC顺序显示: 如ABCABC.....以此类推

#include
#include
#include

int Loop = 10;
int flag = 0;
mutex m;
condition_variable cv;

void fun(int id)
{
    for (int i = 0; i < Loop; i++)
    {
        unique_lock ulk(m); //设定锁定
        while ((id-65)!=flag)
        {
            cv.wait(ulk); //不是该出现的场合 等着
        }
        cout << (char)id << endl;//转换
        flag = (flag + 1) % 4;// 012 
        cv.notify_all(); //通知全部
    }

}

void mainu12()
{
    thread t1(fun,65);
    thread t2(fun, 66);
    thread t3(fun, 67);
    thread t4(fun, 68);
    t1.join();
    t2.join();
    t3.join();
    t4.join();

    cin.get();
}

线程交换与移动

void mainu13()
{
    //thread t1([]() {cout << "FFFFFFF" << endl;});
    //thread t2([]() {cout << "HHHHHHH" << endl;});


    //cout << "t1 id:" < 100000000) break;
            

        }
        cout << i << endl;
        system("pause");
    
    });

    //t1.join();
    cout << t1.get_id() << endl;

    thread t2 = move(t1);  //线程移动  t2具备t1的属性


    cout << t1.get_id() << endl;
    cout << t2.get_id() << endl;

    t2.join();

    cin.get();
}

unique_lock与lock_guard

#include

mutex g_mutex; //全局互斥量

#define N 1000000

void add(int*p)
{
    for (int i = 0; i < N; i++)
    {
        unique_lockulk(g_mutex);//  自动加锁 自动解锁  根据块语句锁定 
        //根据mutex属性 是否可以加锁

        lock_guardlgd(g_mutex); //拥有mutex所有权  锁定向导  自动加锁 自动解锁
        //读取失败的情况下就一直等待

        (*p)++;

    }

}

void mainu14()
{
    int a = 0;
    thread t1(add,&a);
    thread t2(add, &a);
    t1.join();
    t2.join();

    cout << a << endl;

    cin.get();
}

等待固定时间

#include
#include
#include
#include

condition_variable cv;
mutex m;
bool done=false;

void run()
{
    auto start = chrono::high_resolution_clock::now(); //当前时间

    auto end =start + chrono::seconds(10);

    unique_lock lk(m);
    while (!done)
    {
        if (cv.wait_until(lk, end) == cv_status::timeout) //超过时间
        {
            done = true;
            break;
        }
    }

    //this_thread::sleep_until(end);
    system("pause");
}

void mainu15()
{
    //thread  t1(run);

    time_t t1, t2;


    auto start = chrono::high_resolution_clock::now(); //当前时间
    t1 = time(&t1);

    double db = 0;
    for (int i = 0; i < 10000000; i++)
    {
        db += i;
    }
    t2 = time(&t2);

    auto end = start + chrono::milliseconds(5000);

    cout <<(end - start).count() << endl; //精确度高
    cout << difftime(t2 , t1) << endl;  //精确度低

    //this_thread::sleep_until(end); //线程等待不用sleep_until

    cin.get();
}

生产者 消费者

//生产不允许读取 读取不允许生产

#include
#include
#include
#include

mutex m;
condition_variable isfull, isempty; //处理两种情况
bool flag = true; //标志  消费完了退出

vector myint; 

void put(int num) //生产者
{
    for (int i = 0; i < num; i++)
    {
        unique_lock lk(m);//锁定
        while (myint.size()>=10)
        {
            isempty.wait(lk); //满了一直等待
        }

        myint.push_back(i);//插入
        cout << "生产" << i << endl;
        isfull.notify_all(); //通知消费者
        
    }
    this_thread::sleep_for(chrono::seconds(5)); //休眠

    flag = false;
}

void take() //消费者
{
    while (flag)
    {
        unique_lock lk(m);//锁定
        while (myint.size()==0)
        {
            isfull.wait(lk); //等待
        }

        if(flag)
        {
            cout <<"消费:"<< myint[myint.size() - 1] << "  "< myint{ 1,2,3,4,5 };
    
    myint.pop_back(); //删除尾部
    myint.push_back(19);

    for (auto i: myint)
    {
        cout << i << endl;
    }

    thread t1(take);
    thread t2(take);
    thread t3(take);


    put(100);

    thread s1(put,15);
    thread s2(put,10);

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

    cin.get();
}

你可能感兴趣的:(2018-05-15)