This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
2
Memory safety in c++ multithreading
Post Flair (click to view more posts with a particular flair)
Post Body
Hey everyone,
I am a webdev who is using cpp a little bit for some critical components of my application. I am building a postgres listen/notify server and want it to be multithreaded. So far this is what I have:
class Notifier
{
private:
shared_ptr<json> payload_json;
public:
Notifier(const string &payload)
{
payload_json = make_shared<json>(json::parse(payload));
}
void notify()
{
// do something with payload_json
}
};
class Listener : public notification_receiver
{
private:
public:
Listener(connection_base &c) : notification_receiver(c, "notifications")
{
cout << "Listening for notifications..." << endl;
}
void operator()(string const &payload, int backend_pid)
{
try
{
auto notifier = new Notifier(payload);
thread t(&Notifier::notify, notifier);
t.detach();
}
catch (json_exception &e)
{
cerr << "JSON PARSE ERROR: " << e.what() << endl;
}
}
};
void NotificationService::run()
{
Listener l(*conn);
while (true)
{
conn->await_notification();
}
}
My question is, does making payload_json
a unique_ptr cause it to free payload_json
after the thread is done? Any tips or advice is appreciated!
Thanks!
Author
Account Strength
100%
Account Age
9 years
Verified Email
Yes
Verified Flair
No
Total Karma
13,510
Link Karma
6,342
Comment Karma
7,168
Profile updated: 2 days ago
Posts updated: 1 month ago
Subreddit
Post Details
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 1 year ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/cpp_questio...