I have a problem, I have this pointers in my thread code and they are being modify in there, but when it return to the main the changes are not there like this:
Threads
void threaded_function(Model_factory &mf, ppa::Node *root) { // threads management
try { // n try...
Main
int main(int argc, char *argv[]) { ...
In the main I am creating a node root, then in threads the node is being given sequences and has a bool that changes to true like:
ppa::Node *root;
And in the threads is working (a thread group) I can get and set that bool as I wish, but when the thread group finishes with join all (this is boost) the pointer root give me 0 on this line
cout << root->has_sequence() << endl;
After this goes on and the node is again filled with something, so what I want to ask is why is my node pointer not reflecting the changes in the threads, is it design or am I wrong (likely the second) and what should I do a global root node will that fix my issue, but why?
If you have a race condition, you have to suspect that you are accessing critical sections unlocked. I notice that you are manually locking and unlocking your mutex variable. This could lead to errors in your code. For instance, this construct looks particularly alarming:
Instead of locking and unlocking manually, you may consider using
boost::recursive_mutexandboost::recursive_mutex::scoped_lockto manage lock acquisition and release.If you really insist on designing a recursive algorithm that allows reentrancy from multiple threads, perhaps you can make use of a
boost::shared_mutex, andboost::shared_lock/boost::upgradeable_lockfor the read lock, andboost::unique_lock/boost::upgrade_to_unique_lockfor write access. You can follow this link for an example.