08_I/O输入输出流、文件重定向、格式控制、标准输入输出、 串流类、 文件操作、多线程

【目录】

一、 I/O输入输出流 3
1、 流类库ios和streambuf: 3
2、 头文件: 3
3、 标准流 3
二、 文件重定向 4
1、 > 输出到文件 4
2、 < 从文件输入 4
三、 标准输入流 4
1、 标准输入流的函数: 4
2、 cin.get() :获取输入流中的一个字符,回车也算 4
3、 cin.getline():从流中提取一行字符,可以指定长度 5
四、 标准输出流 5
1、 标准输出流函数: 5
2、 cout.put(‘A’):无格式输出一个字符 5
3、 cout.write(str,len):无格式输出一个固定长度的字符串 5
4、 格式控制符: 5
5、 浮点数精度: 5
6、 指定宽度: 6
7、 对齐: 6
8、 定点数与指数显示: 6
五、 格式控制 7
1、 常用格式控制符: 7
2、 头文件:iomanip.h 7
3、 设置宽度: 7
六、 串流类 7
1、 串流类是io中的派生类: 8
2、 istringstream,由istream派生而来,提供读string的功能 8
3、 ostringstream,由ostream派生而来,提供写string的功能 8
4、 stringstream,由iostream派生而来,提供读写string的功能 8
5、 sstream:能读能写 10
七、 文件操作 11
1、 Ifstream和ofstream例子: 11
2、 文件打开open函数: 12
3、 文件读写fstream例子: 12
4、 利用ifstream与ofstream实现ostringstream的分类功能(上面实现的分别取出字符串功能)例子: 13
5、 二进制文件与文本文件的区别: 13
6、 文件追加例子: 14
7、 get与getline挖掘数据 14
8、 随机二进制文件的读写 16
八、 多线程初级 21
1、 简单线程: 21
2、 Join: 21
3、 线程通讯 22
4、 线程锁定: 23

一、I/O输入输出流

1、流类库ios和streambuf:
①.流库具有两个平行的基类:streambuf和ios类,所有流者皆以这两个之一作为基类;
②.streambuf类—filebuf,strstreambuf,stdiobuf:
提供对缓冲区的低级操作:设置缓冲区,对缓冲区指针操作,向缓冲区存取字符。
③.ios类及其派生类—istream , ostream:
|—istream :
|————–ifstream,istream_withassign,istrstream
|—ostream:
|————–ofstream,ostream_withassign,ostrstream
提供用户使用流类的接口,支持对streambuf的缓冲区输入输出的格式化和非格式化转换;
这里写图片描述
2、头文件:
这里写图片描述
3、标准流
这里写图片描述

二、文件重定向

1、> 输出到文件
2、 < 从文件输入

#include<iostream>
using namespace std;
void main()
{
    char str[30] = { 0 };
    cin >> str;
    cout << str;
    system(str);
    cerr << "error for you";


    cin.get();
    cin.get();


}

步骤:在doc命令行下,直接测试><这两个符号的效果。

三、标准输入流

1、标准输入流的函数:
这里写图片描述
2、cin.get() :获取输入流中的一个字符,回车也算

char ch = 0;
    while ((ch=cin.get())!='\t')//复合表达式
    {
        cin.get();
        cout.put(ch);

    }

3、cin.getline():从流中提取一行字符,可以指定长度

char str[10] = {0};
    cin.getline(str, 10);//限定长度
    cout << str;

四、标准输出流

1、标准输出流函数:
这里写图片描述
2、cout.put(‘A’):无格式输出一个字符

cout.put('A').put('B').put('C');

3、cout.write(str,len):无格式输出一个固定长度的字符串

char  str[] = "123456789abcdefg";
cout.write(str, 10);//最大输出10个字符,不包含/0

4、格式控制符:

//dec,oct,hex都是格式控制符
    int num = 01070;
    cout << num << endl;//默认十进制

    cout << hex;//十六进制强制标识,endl结束不了
    cout << num << num << "\n" << endl;
    cout << oct;//八进制强制标识,endl结束不了
    cout << num << num<<"\n"<<endl;
    cout << dec;//十进制强制标识
    cout << num << endl;//默认十进制
    cout << num << endl;//默认十进制

5、浮点数精度:

    double db = 1.98123178387127838718732;
    cout <<db << endl;//小数点后面六位
    cout << setprecision(25) << db;//小数点显示精确度

6、指定宽度:

cout.width (40); //设定显示宽度,长度不够则按照原有格式输出
    cout.fill('&');//填充字符,不写这行,填充是空格
    cout << "hello world"<<endl;

7、对齐:

//字符串输出
    cout.width(40); //设定显示的宽度
    cout.fill('&');//填充字符
    cout.setf(ios::left);//输出的内容左对齐
    cout << "hello world" << endl;


    cout.width(20); //设定显示的宽度,如果实际长度helloworld超过了2,按照实际长度输出
    cout.fill('*');//填充字符
    cout.setf(ios::right,ios::left);

    cout << "hello world" << endl;
    cin.get();

/*----------------------------------------------*/

int num1;
    cin.setf(ios::hex, ios::basefield);//设置输入为十六进制
    cin >> num1;
    cout.setf(ios::hex, ios::basefield);//设置十六进制
    cout << num1;
    int num2;
    cin.setf(ios::dec, ios::basefield);//设置输入为十进制
    cin >> num2;
    cout.setf(ios::dec, ios::basefield);
    cout << num2;

    int num3;
    cin.setf(ios::oct, ios::basefield);//设置输入为8进制
    cin >> num3;
    cout.setf(ios::oct, ios::basefield);
    cout << num3;

8、定点数与指数显示:

double  db = 100 / 7.0;
    cout.setf(ios::fixed | ios::showpoint);//定点
    for (int i = 1; i < 10; i++)
    {
        cout.precision(i);//控制小数点多少位
        cout << db << endl;
    }
    cout << db<<endl;
    //db = 1000000000000000000000.0;
    cout.setf(ios::scientific, ios::fixed | ios::showpoint);//科学计数法显示
    //实数,根据方便自动选择指数或者定点小数
    cout << db << endl;

9、笔记:
①.Cout.setf( ios::?? ) ; 可以设置很多格式的控制流格式
②.Cout<<setprecision(10);也可以设置同样的格式,只是头文件不同,<iomanip>
③.要想回到原来的格式:cout.setf( ios::basefield ) ;

五、格式控制

1、常用格式控制符:
这里写图片描述
这里写图片描述
2、头文件:iomanip.h
3、设置宽度:

const int num = 8848;
cout << setw(10) << setfill('*') << setiosflags(ios::left) << num << endl;
cout << setw(10) << setfill('*') << setiosflags(ios::right) << num << endl;
cout << resetiosflags(ios::right) << setw(10) << setbase(8) << setfill('X') << setiosflags(ios::left) << num << endl;
//resetiosflags 清除历史遗迹
//setw宽度
//setbase基数,决定进制

六、串流类

1、串流类是io中的派生类:
2、istringstream,由istream派生而来,提供读string的功能
3、ostringstream,由ostream派生而来,提供写string的功能
4、stringstream,由iostream派生而来,提供读写string的功能

#include<iostream>
#include<sstream>
#include<string>

using namespace std;

struct MyStruct
{
    string str1, str2, str3;
    double db;
    int num;
    char ch;

};

void main1()
{
    string  mystring("china  google  microsoft 12.9 123 A");
    //string.replace '#'  ' '
    MyStruct  struct1;

    istringstream input(mystring);//创建一个字符串扫描流
    input >> struct1.str1 >> struct1.str2 >> struct1.str3 >> struct1.db >> struct1.num >> struct1.ch;
    cout << struct1.str1 << endl;
    cout << struct1.str2<< endl;
    cout << struct1.str3 << endl;
    cout << struct1.db << endl;
    cout << struct1.num << endl;
    cout << struct1.ch << endl;

    cin.get();
}

void main2()
{


    char   mystring[50]="china#123#A";  

    for (char *p = mystring; *p != '\0'; p++)
    {
        if (*p == '#')
        {
            *p = ' ';
        }
    }

    istringstream input(mystring);//创建一个字符串扫描流

    string str;
    int num;
    char ch;
    input >> str >> num >> ch;

    cout <<str << endl;
    cout <<num << endl;
    cout << ch << endl;

    cin.get();
}

void main3()
{
    ostringstream  MYOUT;
    char str1[50] = "a1234567b";
    MYOUT << "a1234b" << 123 << 234.89 << 'h' << str1 << endl;
    /*将所有东西连接在一起,放入MYOUT中*/
    //cout << MYOUT.str();

    cin.get();
}

#include<strstream>
void main233()
{
    char str[100] = { 0 };
    ostrstream MYOUT(str, sizeof(str));//初始化,ostrstrean给char
    //ostringstream两个要区别开来 ,初始化不同

    char str1[50] = "a1234567b";
    MYOUT << "a1234b"  << str1 << ends;
    cout << MYOUT.str() << endl;
    std::cout<<str;

    cin.get();
}
/*
#include<iostream>
#include<strstream>
using namespace std;
void main()
{
    char buf[80];
    ostrstream Output(buf, sizeof(buf));
    double x, y;
    cout << "Input x : ";
    cin >> x;
    cout << "Input y : ";
    cin >> y;
    Output << x << " * " << y << " = " << x*y << ends;
    cout << buf << endl;
    cin.get();
    cin.get();
    cin.get();
}
*/

5、sstream:能读能写

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>

using namespace std;
void mainA()
{
    stringstream mystr;//字符串进行输入,
    mystr.put('X').put('Y');//连个字符输入
    mystr << "ZXCV";  //字符串输入
    cout << mystr.str();

    string str = mystr.str();//定义字符串接受值

    char ch;    //从字符串内部读取一个字符
    mystr >> ch;
    cout << "\n";
    cout.put(ch);

    cout << "\n";
    cout << mystr.str();
    std::cin.get();
}
void main()
{
    stringstream mystr;//sprintf功能
    char cmd1[30] = { 0 };
    char cmd2[30] = { 0 };
    cin.getline(cmd1, 30).getline(cmd2, 30);//读取
    mystr << cmd1 << "&" << cmd2;//字符打印
    string str = mystr.str();//定义字符串接受值
    system(str.c_str());
/**/
    char cstr[50] = { 0 };//默认的字符串
    strcpy(cstr, str.c_str());//拷贝
    cout << cstr << endl;
    for (char *p = cstr; *p != '\0'; p++)
    {
        if (*p == '&')
        {
            *p = ' ';
        }
    }
    char newcmd1[30] = { 0 };
    char newcmd2[30] = { 0 };
    stringstream  newstr(cstr);//sscanf的功能
    newstr >> newcmd1 >> newcmd2;
    cout << newcmd1<<"\n"<<newcmd2<< endl;

    system("pause");
}

七、文件操作<fstream>

1、Ifstream和ofstream例子:

#include <iostream>
#include<fstream>

using namespace std;
/*ofstream文件写入*/
void main1()
{

   ofstream fout;//ofstream.输出文件
   fout.open("C:\\1.txt");//打开文件
   fout << "1234abcdef";//写入文件
   fout.close();

}
/*ifstream文件读取*/
void main2()
{
    ifstream fin("C:\\1.txt");//创建读取文件的流
    char str[50] = { 0 };
    fin >> str;//读取
    fin.close();
    cout << str;
    cin.get();

}
/*ifstream文件读取,按行读取*/
void main3()
{

    //按照行来读取
    ifstream fin("C:\\1.txt");
    for (int i = 0; i < 4; i++)
    {
        char str[50] = { 0 };
        fin.getline(str, 50);
        cout << str << endl;
    }
    fin.close();
    cin.get();

}
/*ofstream文件写入,按行写入*/
void main4()
{

    ofstream fout;//ofstream.输出文件
    fout.open("C:\\2.txt");//打开文件
    fout << "锄禾日当午"<<endl;//写入文件
    fout << "地雷买下土" << endl;//写入文件
    fout << "谭胜来跳舞" << endl;//写入文件
    fout << "炸成250" << endl;//写入文件
    fout.close();
}

2、文件打开open函数:

3、文件读写fstream例子:

/*文件读写fstream*/
void  main5()
{
    fstream fio("C:\\3.txt", ios::in | ios::out);
    fio << "锄禾日当午" << endl;//写入文件
    fio << "地雷买下土" << endl;//写入文件
    fio << "谭胜来跳舞" << endl;//写入文件
    fio << "炸成250" << endl;//写入文件
    fio.close();/*第一种*/
    {
        fstream fio("C:\\3.txt", ios::in | ios::out);
        for (int i = 0; i < 4; i++)
        {
            char str[50] = { 0 };
            fio.getline(str, 50);
            cout << str << endl;
        }
        fio.close();

    }


    cin.get();
}

void  main6()
{
    fstream fio("C:\\4.txt", ios::in | ios::out);
    fio << "锄禾日当午" << endl;//写入文件
    fio << "地雷买下土" << endl;//写入文件
    fio << "谭胜来跳舞" << endl;//写入文件
    fio << "炸成250" << endl;//写入文件

    fio.seekg(ios::beg);/*第二种*/
    //文件指针  ios::beg开始

    for (int i = 0; i < 4; i++)
    {
            char str[50] = { 0 };
            fio.getline(str, 50);
            cout << str << endl;
    }
   fio.close();

    cin.get();
}

4、利用ifstream与ofstream实现ostringstream的分类功能(上面实现的分别取出字符串功能)例子:

//写入文件,不需要转换为字符串
//读取的时候,不需要把字符串转换为其他类型的操作

void  main7()
{
    ofstream fout;
    fout.open("C:\\X.txt");
    fout << "ABC" << " " << 123 << " " << 'ch'<<endl;//打印到文件
    fout.close();
    ifstream fin("C:\\X.txt");//创建读取文件的流
    char str[10] = { 0 };//读取字符串
    int num = 0;
    char ch = '\0';
    fin >> str >> num >> ch;
    std::cout << str << "\n" << num << "\n" << ch;
}

5、二进制文件与文本文件的区别:

//读写一个字符
//文本与二进制存储差别不一样

void main123213()
{

    ifstream fin("C:\\4.txt");//创建读取文件的流
    ofstream fout("C:\\40.txt");
    if (!fin || !fout)
    {
        std::cout << "文件打开失败";
        return;
    }
    std::cout << "文件拷贝开始\n";
    char ch=0;
    while (fout && fin.get(ch))//引用的方式读取到一个字符
    {
        fout.put(ch);//写入一个字节
    }
    fin.close();
    fout.close();

    std::cout << "文件拷贝完成";
    cin.get();
}

如果读写的是一个二进制文件,则操作不会得到想要的效果,必须加上ios::binary模式打开才行,默认是文本文件打开,所以出错。
6、文件追加例子:

void  main10()
{
    ofstream fout("C:\\40.txt",ios::app);//追加
    fout << "天下英雄,谭胜第一";
    fout.close();

    cin.get();
}

7、get与getline挖掘数据
①.原型:
这里写图片描述

#include<iostream>
/******************get**********************/

#include <stdlib.h>
using namespace std;

void main1()
{
    {
        char buf[80];
        cin.get(buf, 80, '#');//只是在流中提取一次,提取一段文本,最大长度为80,遇到#结束
        std::cout << buf;
    }

    system("pause");
    std::cin.get();
    std::cin.get();
}

void main2()
{
    {
        char buf[80];
        cin.get(buf, 80);//以回车结束,最大长度为80
        cin >> buf;//cin无法区分空格
        std::cout << buf;
    }

    system("pause");
    std::cin.get();
    std::cin.get();
}
void main3()
{
    {
        char buf[8];
        cin.get(buf, 8,'n');//如果记录回车,空格,可以以任何字符
        //cin >> buf;//cin无法区分空格
        std::cout << buf;
    }

    system("pause");
    std::cin.get();
    std::cin.get();
}
void main4()
{
    {
        char buf[80];
        cin.get(buf, 40, 'n');//如果记录回车,空格,可以以任何字符
        std::cout << buf<<"\n";//n意味着结束,后面不会读取
        cin.get(buf, 40, 'n');/*这里不会读取了*/
        std::cout << buf << "\n";
    }

    system("pause");
    std::cin.get();
    std::cin.get();
}
/******************getline**********************/
void main()
{
    char buf[80];
    //默认/n,可以设定,可以反复读取
    cin.getline(buf, 80,',');//逐行读取
    std::cout << buf << "\n";
    cin.getline(buf, 80,',');//逐行读取
    std::cout << buf << "\n";
    cin.getline(buf, 80, ',');//逐行读取
    std::cout << buf << "\n";
    cin.getline(buf, 80, '\n');//逐行读取
    std::cout << buf << "\n";

    //cin.get(buf, 80,'x');//一次性读取,以X为结束
    //std::cout << buf << "\n";

    system("pause");
    std::cin.get();
    std::cin.get();
}

8、随机二进制文件的读写
①.二进制文件读写
这里写图片描述
这里写图片描述
②.代码:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;


struct MyStruct
{
    char *p = "北京是帝都";
    int num = 20;
    double db = 10.98;
    char ch = 'a';

};
/************文本文件读写*******************/
void main1()
{
    ofstream fout("C:\\wen.txt",ios::out);
    ifstream fin("C:\\wen.txt");
    std::cout << sizeof(MyStruct) << std::endl;
    MyStruct my1;
    fout << my1.p <<" "<< my1.num <<" "<< my1.db <<" "<< my1.ch << "\n";
    fout.close();
    char str[100] = { 0 };
    fin.getline(str, 100, 0);//提取
    std::cout << str << std::endl;
    fin.close();

    cin.get();
}
/***************二进制文件读写***********************/
void main()
{
    MyStruct my1;
    my1.p = "chuheridangwu";
    ofstream fout("C:\\bin.bin", ios::binary);
    fout.write((char *)&my1, sizeof(my1));
    //第一个参数是要写入文件的内容的首地址,
    //第二个参数是长度
    fout.close();
    ifstream fin("C:\\bin.bin", ios::binary);
    MyStruct newmy1;
    fin.read((char*)&newmy1, sizeof(newmy1));
    //保存文件读取到内存,内存首地址
    //长度
    std::cout << newmy1.p << std::endl;
    fin.close();

    std::cin.get();
}

③.二进制文件拷贝

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

//按照字节的方式读写二进制,
//文件加密解密需要字节的方式

void main1()
{

    ifstream fin("C:\\write.exe", ios::binary);
    ofstream fout("C:\\newwrite.exe", ios::binary);
    if (!fin || !fout)
    {
        std::cout << "文件打开失败";
        return;
    }
    std::cout << "文件拷贝开始\n";
    char ch = 0;
    while (fout && fin.get(ch))//引用的方式读取到一个字符
    {
        fout.put(ch);//写入一个字节

    }
    fin.close();
    fout.close();

    std::cout << "文件拷贝完成";

    std::cin.get();
}

④.二进制内存文件拷贝

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
struct MyStruct
{
    int i;
    double db;
};

void main()
{
    ofstream fout("C:\\big.txt", ios::binary);
    MyStruct ss[5] = { { 1, 1.1 }, { 2, 2.2 }, { 3, 3.3 }, { 4, 4.4 }, { 5, 5.5 } };
    fout.write((char *)ss, sizeof(MyStruct)* 5);
    fout.close();

    ifstream fin("C:\\big.txt", ios::binary);
    MyStruct *p = new MyStruct[5];//动态分配内存
    fin.read((char *)p, sizeof(MyStruct)* 5);
    fin.close();

    for (int i = 0; i < 5; i++)
    {
        std::cout << p[i].i << " " << p[i].db << std::endl;
    }

    cin.get();
}

⑤.文本文件随机位置读写:

#include<iostream>
#include <fstream>
using namespace std;
/**********随机读取seekg***************/
void main1()//随机位置读取
{
    ofstream fout("p.txt");
    fout << "1234567890abcdefghijklmn";
    fout.close();
    ifstream fin("p.txt");
    //fin.seekg(9, ios::beg);//从开始,往前9个字符
    fin.seekg(-9, ios::end);//从尾部,倒数9个字符
    char ch;
    while (fin.get(ch))
    {
        cout << ch;
    }
    fin.close();
    cin.get();
}
/**********随机写入seekp***************/
void main2()
{

    ofstream fout("px.txt");
    fout << "1234567890abcdefghijklmn";
    fout.close();

    ofstream Fout("px.txt", ios::in | ios::out);//不会被清空
    char str[] = "ABCDEFG";
    Fout.seekp(0, ios::end);//随机位置进行读写
    long size = Fout.tellp();//当前位置距离begin有多少个字节,尾部可以获取文件大小

    cout << size<<endl;
    Fout << str;
    Fout.close();

    ifstream fin("px.txt");
    char ch;
    while (fin.get(ch))
    {
        cout << ch;
    }
    fin.close();

    cin.get();
}

⑥.二进制文件随机读写:

#include<iostream>
#include <fstream>
using namespace std;
/**********读写***************/
void main1a()
{
    double db[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1 };
    ofstream  fout("N.txt", ios::binary);
    fout.write((char *)db, 80);
    fout.close();
    double *p = new double[10];
    ifstream fin("N.txt", ios::binary);

    fin.read((char *)p, 80);
    fin.close();
    for (int i = 0; i < 10; i++)
    {
        std::cout << p[i] << endl;
    }

    std::cin.get();
}
/**********随机位置读取**************/
void main2b()//随机位置读取
{
    double db[10] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.1 };
    ofstream  fout("N.txt", ios::binary);
    fout.write((char *)db, 80);
    fout.close();
    double *p = new double[5];
    ifstream fin("N.txt", ios::binary);
    fin.seekg(-40, ios::end);//读写
    fin.read((char *)p, 40);
    fin.close();
    for (int i = 0; i < 5; i++)
    {
        std::cout << p[i] << endl;
    }

    std::cin.get();
}
/**********随机位置写入**************/
void main()
{
    //先读取
    double *p = new double[10];
    ifstream fin("N.txt", ios::binary);
    fin.read((char *)p, 80);
    fin.close();
    for (int i = 0; i < 10; i++)
    {
        std::cout << p[i] << endl;
    }
    //随机写入
    double db[] = { 100.9, 200.8, 300.7, 400.6, 500.5 };
    //ios::in | ios::out不再清零文件,否则会清零
    ofstream  fout("N.txt", ios::binary|ios::in | ios::out);
    //fout.seekp(40, ios::beg);
    fout.seekp(0, ios::end);//尾部追加写入
    fout.write((char *)db, 40);
    fout.close();

    //再次读取
    {
        double *p = new double[20];
        ifstream fin("N.txt", ios::binary);
        fin.read((char *)p, 160);
        fin.close();
        for (int i = 0; i < 20; i++)
        {
            std::cout << p[i] << endl;
        }
    }

        std::cin.get();
}

八、多线程初级
1、简单线程:

#include <iostream>
#include <thread>
#include <string>

using namespace std;

void helloworld()
{
    std::cout << "你好天朝" << endl;
}

void helloworldA()
{
    std::cout << "你好天朝A" << endl;
}

void helloworldB()
{
    std::cout << "你好天朝B" << endl;
}

void main1()
{

    std::thread t1(helloworld);//线程顺序执行,
    std::thread t2(helloworldA);
    std::thread t3(helloworldB);

    cin.get();
}

2、Join:

#include <iostream>
#include <thread>
#include <string>
#include <windows.h>

using namespace std;

void run(int num)
{
    Sleep(1000);
    std::cout << "你好天朝"<< num<< endl;
}
void main2()
{
    //thread t[10];
    thread *p[10];
    for (int i = 0; i < 10;i++)
    {
        p[i]=new thread(run, i);//循环创建线程
        p[i]->join();//等待模式执行
        p[i]->detach();//脱离当前主线程自由执行,乱序
    }
    cin.get();
}

3、线程通讯

#include <iostream>
#include <thread>
#include <string>
#include<windows.h>

using namespace std;

//两个线程并行访问一个变量

int g_num = 20;//找到或者找不到的标识

void goA(int num)
{

    for (int i = 0; i < 15; i++)
    {
        Sleep(1000);
        if (i == 6)
        {
            g_num = 5;
        }
        if (g_num ==5)
        {

          std::cout << "线程" << num << "结束   \n";
            return;
        }
        std::cout << "线程" << num <<"   "<< g_num << endl;
    }
}
void goB(int num)
{
    for (int i = 0; i <150; i++)
    {
        Sleep(1000);
        if (g_num == 5)
        {
            std::cout << "线程" << num << "结束   \n";
            return;
        }
        std::cout << "线程" << num << "   " << g_num << endl;
    }

}
void main3()
{
    thread t1(goA, 1);
    thread t2(goB, 2);
    t1.join();
    t2.join();

    std::cin.get();
}

4、线程锁定:

#include <iostream>
#include <thread>
#include <string>
#include<windows.h>
#include<mutex>


using namespace std;

//两个线程并行访问一个变量

int g_num = 20; //找到或者找不到的标识
mutex g_mutex;

void goA(int num)
{
    g_mutex.lock(); //你访问的变量,在你访问期间,别人访问不了

    for (int i = 0; i < 15; i++)
    {
        Sleep(300);
        g_num = 10;
        std::cout << "线程" << num << "   " << g_num << endl;
    }
    g_mutex.unlock();
}
void goB(int num)
{
    for (int i = 0; i < 15; i++)
    {
        Sleep(500);
        g_num = 11;
        std::cout << "线程" << num << "   " << g_num << endl;
    }
}
void main()
{
    thread t1(goA, 1);
    thread t2(goB, 2);
    t1.join();
    t2.join();

    std::cin.get();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值