Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

协程共享栈缺陷 #373

Open
shuai132 opened this issue Oct 3, 2024 · 1 comment
Open

协程共享栈缺陷 #373

shuai132 opened this issue Oct 3, 2024 · 1 comment

Comments

@shuai132
Copy link
Contributor

shuai132 commented Oct 3, 2024

BUG:
协程栈上数据的地址传递给其他协程处理时,可能出现错误。
边界条件:协程无法独占共享栈时(co_stack_num满)。

TestCase:

#include "co/co.h"

int main(int argc, char **argv) {
  flag::init(argc, argv);
  FLG_cout = true;
  flag::set_value("co_sched_num", "1");
  flag::set_value("co_stack_num", "1");
  // flag::set_value("co_sched_log", "true");

  go([&] {
    char buf[] = "test";
    LOG << "buf:1: " << buf;
    go([&] {
      LOG << "buf:2.1: " << buf; // error here: expect `text`
      buf[0] = 'T';
      LOG << "buf:2.2: " << buf; // error here: expect `Text`
      co::sleep(1);
    });
    co::sleep(1);
    LOG << "buf:3: " << buf; // error here: expect `Text`
    co::sleep(1);
  });

  co::sleep(10);
  return 0;
}

说明:
协程栈有多种实现方式,我还是更倾向于优先保证C++常见写法的正确性。
目前共享栈的方式容易出现上述问题,需要使用者避免此类写法。

我们是否可以考虑改为虚拟内存自动扩容的协程栈方式? @idealvin

@idealvin
Copy link
Owner

idealvin commented Oct 6, 2024

64位系统可以考虑每个协程单独分配一个1M的栈

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants