第五课:C++多线程数据共享问题

一、通过容器去创建多个线程

需要通过vector<>容器创建(装)多个线程、当然可以是其他容器,看个人习惯。

如下:

//1,通过容器去创建多个线程
void print(int indx)
{
    //cout<<"线程序号:"<<indx<<endl;
    printf("线程序号:%d\n",indx);
}

void testcreateThread()
{
    vector<thread*>test;
    for(int i = 0; i < 10; i++)
    {
        test.push_back(new thread(print,i));
    }
    
    for(auto v:test)
    {
        v->join();
    }
    
    cout<<"主线程"<<endl;
}

二、数据共享问题


1、只读数据:稳定安全,不需要特殊处理,直接读取即可

如下:

vector<int> g_date = {1,2,3};

void printTest(int num)
{
    printf("子线程序号:%d\n",num);
    
    for(auto v:g_date)
    {
        printf("%d\t",v);
    }
}

void visitedDate()
{
    vector<thread*> test;
    for(int i = 0; i < 10; i++ )
    {
        test.push_back(new thread(printTest,i));
    }
    for(auto v: test)
    {
        v->join();
    }
    
    cout<<"主线程"<<endl;
}

2、有读有写数据:需要经过特殊处理,让程序只对共享数据做单一操作(写不读,读不能写)

如下:

class Seaking
{
  
  public:
    void makeFriend()
    {
        for(int i = 0; i < 1000; i++ )
        {
            
            printf("谈女朋友\n");
            mm.push_back(i);
        }
    }
    
    void breakUp()
    {
        for(int i = 0; i < 1000; i++)
        {
            if(!mm.empty())
            {
                printf("和女朋友分手%d\n",mm.front());
                    
                mm.pop_front();
            }  
            else
            {
                printf("单身\n");
            }
        }
    }
    
  protected:
    list<int> mm;
};

三、完整案例

#include<thread>
#include<iostream>
#include<vector>
#include<cstdio>
#include<list>

using namespace std;
using namespace this_thread;
//1,通过容器去创建多个线程
void print(int indx)
{
    //cout<<"线程序号:"<<indx<<endl;
    printf("线程序号:%d\n",indx);
}

void testcreateThread()
{
    vector<thread*>test;
    for(int i = 0; i < 10; i++)
    {
        test.push_back(new thread(print,i));
    }
    
    for(auto v:test)
    {
        v->join();
    }
    
    cout<<"主线程"<<endl;
}

//2,数据共享问题
/*
1、只读数据:稳定安全,不需要特殊处理,直接读取即可
2,有读有写数据:需要经过特殊处理,让程序只对共享数据做单一操作(写不读,读不能写)
*/

vector<int> g_date = {1,2,3};

void printTest(int num)
{
    printf("子线程序号:%d\n",num);
    
    for(auto v:g_date)
    {
        printf("%d\t",v);
    }
}

void visitedDate()
{
    vector<thread*> test;
    for(int i = 0; i < 10; i++ )
    {
        test.push_back(new thread(printTest,i));
    }
    for(auto v: test)
    {
        v->join();
    }
    
    cout<<"主线程"<<endl;
}

//2,有读有写数据:需要经过特殊处理,让程序只对共享数据做单一操作(写不读,读不能写)
class Seaking
{
  
  public:
    void makeFriend()
    {
        for(int i = 0; i < 1000; i++ )
        {
            
            printf("谈女朋友\n");
            mm.push_back(i);
        }
    }
    
    void breakUp()
    {
        for(int i = 0; i < 1000; i++)
        {
            if(!mm.empty())
            {
                printf("和女朋友分手%d\n",mm.front());
                    
                mm.pop_front();
            }  
            else
            {
                printf("单身\n");
            }
        }
    }
    
  protected:
    list<int> mm;
};

void testSeeaking()
{
    Seaking seaman;
    thread t1(&Seaking::makeFriend,&seaman);
    thread t2(&Seaking::breakUp,&seaman);
    t1.join();
    t2.join();
    
}



int main()
{
    
    testcreateThread();
    visitedDate();
    visitedDate();
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值