gpt4 book ai didi

c++ - 覆盖 vector 的第一个元素会改变 vector 最后一个元素的内容

转载 作者:行者123 更新时间:2023-12-03 20:42:52 30 4
gpt4 key购买 nike

在编写代码时,我注意到运行代码会返回不正确的结果,结果发现我的代码中的某些内容正在更改我的协程的句柄 vector 并将其缩小到一行代码,我覆盖了句柄 vector 的现有元素带有新元素。
这样做也会更改 vector 的最后一个元素的内容(更具体地说,来自 myTask header 的 bool),但不会更改中间的元素。
有谁知道是什么原因造成的?任何帮助表示赞赏。
完整的实现代码:

#include <concepts>
#include <coroutine>
#include <exception>
#include <iostream>
#include <myTask.h>
#include <vector>

myTask<int> getVectorInt(std::vector<int>& array, int key, bool interleave)
{
std::cout << "started lookup of key: " << key << std::endl;
int result = array.at(key);
if (interleave == true)
{
std::cout << "about to suspend task with key: " << key << std::endl;
co_await std::suspend_always{};
std::cout << "resumed task with key: " << key << std::endl;
}
co_return result;
}

void interleavedExecution(std::vector<int>& lookup, std::vector<int>& keys, std::vector<int>& results)
{
// group size = number of concurrent instruction streams
int groupsize = 3;

// initialization of handle vector
std::vector<std::coroutine_handle<myTask<int>::promise_type>> handles;

// initialization of promise vector
std::vector<myTask<int>::promise_type> promises;

// creating/initializing first handles
for (int i = 0; i < groupsize; ++i)
{
handles.push_back(getVectorInt(lookup, keys.at(i), true));
}

int notDone = groupsize;
int i = groupsize;

// interleaved execution starts here
while (notDone > 0)
{
for (int handleIndex = 0; handleIndex < handles.size(); ++handleIndex)
{
if (!handles.at(handleIndex).promise().isDone())
{
handles.at(handleIndex).resume();
handles.at(handleIndex).promise().boolIsDone = true;
}
else
{
// pushing value back directly into results
results.push_back(handles.at(handleIndex).promise().value_);

if (i < keys.size())
{
// bug here, changes the last boolIsDone also to false (or messes with the last vector element)
handles.at(handleIndex) = getVectorInt(lookup, keys.at(i), true);

handles.at(handleIndex).promise().boolIsDone = false;
++i;
}
else { --notDone; }
}
}
}
}

template <typename T>
void outputVector(std::vector<T> toOutput)
{
std::cout << "Results: ";
for (int i = 0; i < toOutput.size(); ++i)
{
std::cout << toOutput.at(i) << ' ';
}
}

int main()
{
std::vector<int> lookup = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
std::vector<int> keys = {4, 2, 0, 6, 9, 0};
std::vector<int> results;

// correct output: 50, 30, 10, 70, 100, 10
// given output: 50, 30, 70, 10, 100, 10
interleavedExecution(lookup, keys, results);
outputVector(results);
}
带有 bool 值的 myTask header :
#include <concepts>
#include <coroutine>
#include <exception>
#include <iostream>

template <typename T>
struct myTask {
struct promise_type {
unsigned value_;
~promise_type() {
//std::cout << "promise_type destroyed" << std::endl;
}

myTask<T> get_return_object() {
return myTask<T> {
.h_ = std::coroutine_handle<promise_type>::from_promise(*this)
};
}
std::suspend_never initial_suspend() { return {}; }
std::suspend_never final_suspend() { return {}; }
void unhandled_exception() { std::terminate(); }

std::suspend_always return_value(unsigned value) {
value_ = value;
return {};
}

bool boolIsDone = false;

auto isDone() { return boolIsDone; }
};

std::coroutine_handle<promise_type> h_;
operator std::coroutine_handle<promise_type>() const {
//std::cout << "called handle" << std::endl;
return h_; }


};

最佳答案

原来改变了final_suspend()的返回类型来自 std::suspend_neverstd::suspend_always解决了这个问题。

关于c++ - 覆盖 vector 的第一个元素会改变 vector 最后一个元素的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66550871/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com