2018-05-15

伪函数和多线程

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

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

void mainu1()
{
    func f1;
    f1();

    cin.get();
}

//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<future>
#include<string>
#include<cstdlib>

promise<string> val;  //全局通信变量

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

    thread th1([]() 
    {
        future<string> 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 <typename T,typename...Args> //子类调用父类构造函数 ,可变参数的构造
    huathread(T&& func, Args&&...args): thread(   forward<T>(func),    forward<Args>(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<<endl; }, 100 );
    th2.run("notepad");

    cin.get();
}

条件变量

#include<condition_variable>
#include<mutex>

//线程通信 结合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<mutex> 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<mutex> lckg(m); //解锁向导
    //  //cv.notify_one(); //挨个通知
    //}

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


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

    cin.get();
}

并行计算 获取结果

#include<future> //线程将来结果
#include<chrono> //时间
#include<mutex>

mutex g_m;

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

    packaged_task<int(int)> pt1(run);
    packaged_task<int(int)> 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<int> 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<int> myintx{ 11,12,13,14,15 };
    vector<int> 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<int> myint2{ 1,2,3,4,5 };
    vector<int> 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<int> 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<int> myintx1{ 1,2,3,4,5 };
    vector<int> myintx2{ 21,2,3,4,5 ,6,7};
    vector<int> myintx3{ 1,2,3 };

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


    cin.get();
}


可变参数与多线程

#include<cstdarg>

//可变参数
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<future>
#include<vector>
#include<cstdlib>

#define COUNT 5000000

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

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

    return sum;
}

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

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

    vector<future<int> > 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<mutex>
#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<condition_variable>
#include<mutex>
#include<thread>

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

void fun(int id)
{
    for (int i = 0; i < Loop; i++)
    {
        unique_lock<mutex> 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:" <<t1.get_id() << endl<<"t2 id:" << t2.get_id() << endl;
    //swap(t1,t2);
    //cout << "t1 id:" << t1.get_id() << endl << "t2 id:" << t2.get_id() << endl;

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

    thread t1([]() {
        int i = 0;
        while (1)
        {
            i++;
            if (i > 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>

mutex g_mutex; //全局互斥量

#define N 1000000

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

        lock_guard<mutex>lgd(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<chrono>
#include<condition_variable>
#include<mutex>
#include<ctime>

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<mutex> 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<vector>
#include<array>
#include<mutex>
#include<condition_variable>

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

vector<int> myint; 

void put(int num) //生产者
{
    for (int i = 0; i < num; i++)
    {
        unique_lock<mutex> 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<mutex> lk(m);//锁定
        while (myint.size()==0)
        {
            isfull.wait(lk); //等待
        }

        if(flag)
        {
            cout <<"消费:"<< myint[myint.size() - 1] << "  "<<this_thread::get_id() <<endl;
            myint.pop_back(); //消除
            isempty.notify_all(); //通知生产者
        }
    }
}


void main()
{
    vector<int> 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();
}

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,084评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,623评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,450评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,322评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,370评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,274评论 1 300
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,126评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,980评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,414评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,599评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,773评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,470评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,080评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,713评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,852评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,865评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,689评论 2 354

推荐阅读更多精彩内容

  • 从三月份找实习到现在,面了一些公司,挂了不少,但最终还是拿到小米、百度、阿里、京东、新浪、CVTE、乐视家的研发岗...
    时芥蓝阅读 42,243评论 11 349
  • 在一个方法内部定义的变量都存储在栈中,当这个函数运行结束后,其对应的栈就会被回收,此时,在其方法体中定义的变量将不...
    Y了个J阅读 4,417评论 1 14
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,625评论 18 399
  • 早上起床想给头像加个圣诞帽 但实在太丑 晚上公众号上和大家问好 有小伙伴回复什么时候? 我黑人问号.jpg 因为我...
    咸酥阅读 139评论 0 0
  • 如何做网络的主人?找到它带给你的快感并用笑话取代它,分给网络的时间只能是碎片化的,只是调剂生活,并不能取代生活。
    创新中心阅读 255评论 0 0