You can use the guardian template structure described int this blog post.
Basically, you will create a guardian<ptree>
instead of a plain ptree
. A guardian is a opaque structure that holds a mutex
alongside its data. The only way to access the data is via a guardian_lock
, that will lock the mutex
.
guardian<ptree> xml;
//thread 1
{
guardian_lock<ptree> lock(xml);
lock->put("a", "b");
}
//thread 2
{
guardian_lock<ptree> lock(xml);
lock->put("c", "d");
}
As you can only access the inner ptree
through the lock, and the lock locks the mutex, you will never have race conditions.