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

Refine buffer channel #8098

Merged

Conversation

chengduoZH
Copy link
Contributor

@chengduoZH chengduoZH commented Feb 3, 2018

  • Send sends only one element, we should wake up only one receiver thread by calling notify_one, instead of notify_all.
  • There are two possibilities for the return of Receive or Send:
    • the Channel was closed.
    • the Receive or Send of this Channel was called.
      we should separate the two possibilities.

cv_channel_.wait(channel_lock,
[this]() { return item == nullptr || closed_; });
}
if (closed_) return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to make sure that writer_found_ is always false at the end of this function. I would rewrite it as follows:

bool ret = false;
if (!closed_) {
    std::unique_lock<std::mutex> channel_lock(mu_ch_);
    item = data;
    channel_lock.unlock();
    cv_channel_.notify_one();
    channel_lock.lock();
    cv_channel_.wait(channel_lock,
                     [this]() { return item == nullptr || closed_; });
    ret = true
  }
  writer_found_ = false;
  return ret;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

}
cv_channel_.notify_one();
}
if (closed_) return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly I would prefer to rewrite this as follows:

bool ret = false;
if (!closed_) {
    std::unique_lock<std::mutex> lock_ch{mu_ch_};
    // Reader should wait for the writer to first write its data
    cv_channel_.wait(lock_ch, [this]() { return item != nullptr || closed_; });
    if (!closed_) {
      *data = std::move(*item);
      item = nullptr;
      lock_ch.unlock();
      ret = true;
    }
    cv_channel_.notify_one();
  }
  reader_found_ = false;
  return ret;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

abhinavarora
abhinavarora previously approved these changes Feb 3, 2018
Copy link
Contributor

@abhinavarora abhinavarora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@chengduoZH chengduoZH force-pushed the feature/refine_buffer_channel branch 2 times, most recently from 606d8c2 to fc58597 Compare February 3, 2018 07:22
Copy link
Contributor

@abhinavarora abhinavarora left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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

Successfully merging this pull request may close these issues.

2 participants