Correct use of boost notify_all in interprocess communication on linux kernels

tl;dr should boost notify_all() be used before or after releasing the lock?

Wikipedia has this comment on signal (in my view similar to broadcast):

it is often also acceptable to release the lock before signalling. Depending on the threading implementation, the ordering of this can have scheduling-priority ramifications. (Some authors[who?] instead advocate a preference for releasing the lock before signalling.) A threading implementation should document any special constraints on this ordering.

The std cpp implementation of the std::condition_variable calls notify_one after releasing the lock:

data = "Example data";
// send data to the worker thread
{
  std::lock_guard lk(m);
  ready = true;
  std::cout << "main() signals data ready for processingn";
}
cv.notify_one();

But when using the boost implementation we can find both variants in the internet (1, 2, 3, p. 127):

boost::interprocess::interprocess_sharable_mutex mutex;
boost::interprocess::interprocess_condition_any cond;

// Producer Process with variants A and B

void producerA() {
  const boost::posix_time::ptime abs_time = boost::get_system_time() + boost::posix_time::microseconds(maxLockTime.count());
  boost::interprocess::scoped_lock<boost::interprocess::interprocess_sharable_mutex> lock(mutex, boost::interprocess::defer_lock);
  if (lock.timed_lock(abs_time)) {
    // Write data
    // ...
    cond.notify_all();  // <--- Notify all consumers
  }
}

void producerB() {
  const boost::posix_time::ptime abs_time = boost::get_system_time() + boost::posix_time::microseconds(maxLockTime.count());
  boost::interprocess::scoped_lock<boost::interprocess::interprocess_sharable_mutex> lock(mutex, boost::interprocess::defer_lock);
  if (lock.timed_lock(abs_time)) {
    // Write data
    // ...
  }
  cond.notify_all(); // <--- Notify all consumers
}

// Consumer Process

void consumer() {
    const boost::posix_time::ptime abs_time = boost::get_system_time() + boost::posix_time::microseconds(maxLockTime.count());
    boost::interprocess::scoped_lock<boost::interprocess::interprocess_sharable_mutex> lock(mutex, boost::interprocess::defer_lock);
    bool hasNewMessage = cond.timed_wait(lock, abs_time, [=]() { ... });

    // Read data

    lock.unlock();
}

Is there a definitive answer to which variant is preferred, either producerA or producerB. In my local tests both producer work accordingly, but in my question I am interested in worst case scenario where system is heavily loaded.

I ran both producer on my system, there was no notable difference visible.

But I am investigating a bug on another remote system where we observe infinite timeouts on the notify_all() function call.

New contributor

Luigsech Ingemar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật