Skip to content

Instantly share code, notes, and snippets.

@hansenms
Created February 21, 2023 18:10
Show Gist options
  • Save hansenms/87e468ff62a78fd4e4b0231ff4f12ad5 to your computer and use it in GitHub Desktop.
Save hansenms/87e468ff62a78fd4e4b0231ff4f12ad5 to your computer and use it in GitHub Desktop.
Boost Deadlock Example
# Boost Process Deadlock example
Build with:
```
g++ -o bptest main.cpp -lpthread
```
or just:
```
make
```
Run with:
```
buffer=$(tyger buffer create) && access_url=$(tyger buffer access "${buffer}" --write) && ./bptest $access_url
```
> Note: It is important to create a new buffer to make sure it is empty (causing the read to wait).
#include <iostream>
#include <thread>
#include <boost/process.hpp>
namespace bp = boost::process;
int main(int argc, char** argv)
{
std::cerr << "Buffer Proxy Test" << std::endl;
std::string access_url = argv[1];
bp::opstream os;
bp::child writer("buffer-proxy write " + access_url, bp::std_in < os);
auto writer_thread = std::thread([&os, &writer](){
std::cerr << "Writer thread started" << std::endl;
std::string data("hello world");
os << data;
// We will put a delay here to make sure we start the reader
// before the writer finishes. This causes a deadlock.
std::this_thread::sleep_for(std::chrono::seconds(1));
os.flush();
os.pipe().close();
writer.wait();
std::cerr << "Writer thread finished" << std::endl;
});
bp::ipstream is;
bp::child reader("buffer-proxy read " + access_url, bp::std_out > is);
auto reader_thread = std::thread([&is, &reader](){
std::cerr << "Reader thread started" << std::endl;
std::string data;
std::getline(is, data);
std::cerr << "Read: " << data << std::endl;
reader.wait();
std::cerr << "Reader thread finished" << std::endl;
});
writer_thread.join();
reader_thread.join();
return 0;
}
bptest: main.cpp
g++ -o bptest main.cpp -lpthread
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment