首页 > 编程语言 > 详细

C++多线程相关

时间:2020-03-07 01:11:30      阅读:94      评论:0      收藏:0      [点我收藏+]

C++对多线程新加的支持操作

 

内存池

 

 

 

面试题

1.题目:子线程循环 10 次,接着主线程循环 100 次,接着又回到子线程循环 10 次,接着再回到主线程又循环 100 次,如此循环50次,试写出代码

#include <iostream>             // std::cout
#include <mutex>
#include <thread>         // std::thread
#include <condition_variable>

using namespace std;

mutex mu;
int flag = 10;
condition_variable cv;
void f(int num)
{
    for (int i = 0; i < 50; i++)
    {
        unique_lock<mutex> lk(mu); 
        while (flag != num) // 这里不能用if,一定要用while
            cv.wait(lk); // 条件变量调用wait会自动调用锁
        for (int j = 0; j < num; ++j)
        {
            cout << j << " ";
        }
        cout << endl;
        flag = (num == 10) ? 100 : 10;
        cv.notify_one();
    }
}

int main()
{
    thread child(f, 10);
    f(100);
    child.join();
    return 0;
}

2.编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推

#include <iostream>             // std::cout
#include <mutex>
#include <thread>         // std::thread
#include <condition_variable>

using namespace std;

mutex mu;
int flag = 0;
condition_variable cv;
void f(int num)
{
    for (int i = 0; i < 10; i++)
    {
        unique_lock<mutex> lk(mu); 
        while (flag != num) // 这里不能用if 一定要用while
            cv.wait(lk); // 条件变量调用wait会自动调用锁
        char c = A + flag;
        cout << c;
        flag = (flag+1)%3;
        cv.notify_all(); //唤醒其他进程
    }
}

int main()
{
    thread child1(f, 0);
    thread child2(f, 1);
    f(2);
    child1.join();
    child2.join();
    return 0;
}

3.题目(google笔试题):有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:

A:1 2 3 4 1 2....
B:2 3 4 1 2 3....
C:3 4 1 2 3 4....

D:4 1 2 3 4 1....

#include <iostream>             // std::cout
#include <mutex>
#include <thread>         // std::thread
#include <condition_variable>

using namespace std;

mutex mu;
int flag = 0; // 改变flag的值,就改变了1234的输出顺序
condition_variable cv;

void f(int num)
{
    for (int i = 0; i < 100; i++)
    {
        unique_lock<mutex> lk(mu); 
        while (flag != num)
            cv.wait(lk); // 条件变量调用wait会自动调用锁
        cout << num + 1 << " ";
        flag = (flag + 1) % 4;
        cv.notify_all(); //唤醒其他进程
    }
}

int main()
{
    thread child1(f, 0);
    thread child2(f, 1);
    thread child3(f, 2);
    f(3);

    child1.join();
    child2.join();
    child3.join();
    cout << endl;

    //system("pause");
    return 0;
}

4.读写者问题:这也是一个非常经典的多线程题目,题目大意如下:有一个写者很多读者,多个读者可以同时读文件,但写者在写文件时不允许有读者去读文件,同样有读者读时写者也不能写

 

// future example
#include <iostream>             // std::cout
#include <mutex>
#include <thread>         // std::thread
#include <condition_variable>
#include <atomic>
#include <future>

using namespace std;


class rwlock {
private:
    mutex _lock;
    condition_variable _wcon, _rcon;
    unsigned _writer, _reader;
    int _active;
public:
    void read_lock() {
        unique_lock<mutex> lock(_lock);
        ++_reader;
        while (_active < 0 || _writer > 0)
            _rcon.wait(lock);
        --_reader;
        ++_active;
    }
    void write_lock() {
        unique_lock<mutex> lock(_lock);
        ++_writer;
        while (_active != 0)
            _wcon.wait(lock);
        --_writer;
        _active = -1;
    }
    void unlock() {
        unique_lock<mutex> lock(_lock);
        if (_active > 0) {
            --_active;
            if (_active == 0) _wcon.notify_one();
        }
        else {
            _active = 0;
            if (_writer > 0) _wcon.notify_one();
            else if (_reader > 0) _rcon.notify_all();
        }

    }
    rwlock() :_writer(0), _reader(0), _active(0) {
    }
};

void t1(rwlock* rwl) {
    while (1) {
        cout << "I want to write." << endl;
        rwl->write_lock();
        cout << "writing..." << endl;
        this_thread::sleep_for(chrono::seconds(5));
        rwl->unlock();
        this_thread::sleep_for(chrono::seconds(5));
    }
}

void t2(rwlock* rwl) {
    while (1) {
        cout << "t2-I want to read." << endl;
        rwl->read_lock();
        cout << "t2-reading..." << endl;
        this_thread::sleep_for(chrono::seconds(1));
        rwl->unlock();
    }
}

void t3(rwlock* rwl) {
    while (1) {
        cout << "t3-I want to read." << endl;
        rwl->read_lock();
        cout << "t3-reading..." << endl;
        this_thread::sleep_for(chrono::seconds(1));
        rwl->unlock();
    }
}

int main()
{
    rwlock* rwl = new rwlock();
    thread th1(t1, rwl);
    thread th2(t2, rwl);
    thread th3(t3, rwl);
    th1.join();
    th2.join();
    th3.join();

    system("pause");
    return 0;
}

 

C++多线程相关

原文:https://www.cnblogs.com/qiang-wei/p/12431377.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!