魔术师丶A 2022-03-25 17:38 采纳率: 50%
浏览 27
已结题

C++ 基于范围的for循环(range for ),这个范围是怎么确定的?

主线程给list 添加元素,其他七个线程打印,我每个线程给的是list的复制。

7个线程都不会退出,打印也不会送0开始,如果给list的引用会从0开始。

下面是代码

```c++
#include <iostream>
#include <windows.h>
#include <thread>
#include <mutex>
#include <atomic>
#include <list>

using namespace std;

mutex mu_all;
condition_variable cv_all;
atomic<bool> flag_all = false;

//class _Ty, class _Alloc = allocator<_Ty>
void func_1(list<int> Tem)
{
    thread th_1([&Tem]() {

        auto & b = flag_all;
        {
            unique_lock<mutex> lock_all(mu_all);
            cv_all.wait(lock_all, [&b]{return b.load(); });
        }

        for (auto item : Tem)
        {
            cout << "func_1->>>" << item << endl;
        }
        cout << "func_1->>>" << "==============================================" << endl;

    });
    th_1.detach();
}

void func_2(list<int> Tem)
{
    thread th_2([&Tem]() {

        auto& b = flag_all;
        {
            unique_lock<mutex> lock_all(mu_all);
            cv_all.wait(lock_all, [&b]{return b.load(); });
        }

        for (auto item : Tem)
        {
            cout<<"func_2->>>" << item << endl;
        }
        cout << "func_2->>>" << "==============================================" << endl;

    });
    th_2.detach();
}

void func_3(list<int> Tem)
{
    thread th_3([&Tem]() {

        auto& b = flag_all;
        {
            unique_lock<mutex> lock_all(mu_all);
            cv_all.wait(lock_all, [&b] {return b.load(); });
        }

        for (auto item : Tem)
        {
            cout << "func_3->>>" << item << endl;
        }
        cout << "func_3->>>" << "==============================================" << endl;

    });
    th_3.detach();
}

void func_4(list<int> Tem)
{
    thread th_4([&Tem]() {

        auto& b = flag_all;
        {
            unique_lock<mutex> lock_all(mu_all);
            cv_all.wait(lock_all, [&b] {return b.load(); });
        }

        for (auto item : Tem)
        {
            cout << "func_4->>>" << item << endl;
        }
        cout << "func_4->>>" << "==============================================" << endl;

    });
    th_4.detach();
}

void func_5(list<int> Tem)
{
    thread th_5([&Tem]() {

        auto& b = flag_all;
        {
            unique_lock<mutex> lock_all(mu_all);
            cv_all.wait(lock_all, [&b] {return b.load(); });
        }

        for (auto item : Tem)
        {
            cout << "func_5->>>" << item << endl;
        }
        cout << "func_5->>>" << "==============================================" << endl;

    });
    th_5.detach();
}

void func_6(list<int> Tem)
{
    thread th_6([&Tem]() {

        auto& b = flag_all;
        {
            unique_lock<mutex> lock_all(mu_all);
            cv_all.wait(lock_all, [&b] {return b.load(); });
        }

        for (auto item : Tem)
        {
            cout << "func_6->>>" << item << endl;
        }
        cout << "func_6->>>" << "==============================================" << endl;

    });
    th_6.detach();
}

void func_7(list<int> Tem)
{
    thread th_7([&Tem]() {

        auto& b = flag_all;
        {
            unique_lock<mutex> lock_all(mu_all);
            cv_all.wait(lock_all, [&b] {return b.load(); });
        }

        for (auto item : Tem)
        {
            cout << "func_7->>>" << item << endl;
        }
        cout << "func_7->>>" << "==============================================" << endl;
    });
    th_7.detach();
}
int main()
{
    list<int> i_lst;

    func_1(i_lst);
    func_2(i_lst);
    func_3(i_lst);
    func_4(i_lst);
    func_5(i_lst);
    func_6(i_lst);
    func_7(i_lst);

    for (int i = 0;;i++)
    {
        i_lst.emplace_back(i);

        if (i == 0)
        {
            flag_all = true;
            cv_all.notify_all();
        }
    }

    return 0;
}

```

  • 写回答

1条回答 默认 最新

  • _GX_ 2022-03-25 18:26
    关注

    如果函数参数按值传递,那么main函数传给每个函数的链表都是主函数i_list的拷贝,而且都是空链表,自然都没有数据输出
    如果函数参数该为引用,那么每个函数内部的链表都是引用主函数里的i_list,而且每个线程也是引用传递,即也是指向i_list,所以都是指向同一个链表。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月7日
  • 已采纳回答 3月30日
  • 创建了问题 3月25日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度