pthreads - Is it a good idea to embed a pthread_mutex_t obj into a C++ obj instance? I think (sadly) no bat -


This question was made to implement the following simple mailbox interface:

   

To implement this I have created a C ++ class, where I have kept my suff and a mute x, in which every access must have serial access. When I try to write DeleteMailBox the problems increase because I can not remove locked mutes, but if I unlock it, I can not guarantee that someone else has access to the object being removed. . (In my opinion a muted locked thread should also have the possibility to remove it).

Destruction should always be synchronized externally

the race you concern It is that you can call two threads DeleteMailBox and TxMailBox together at mailbox as you have seen, Mailbox object can not protect itself against this race even if you can delete locked mutes, then concurrently by another thread. Education efforts will now attempt to lock a lost Mutks, which is a data race.

This is an elemental limit Object-oriented design in multithreaded contexts, if a user requests the destruction of an object, then depend on the user to ensure it. It happens that someone will not try to reach that point concurrently at that point.

Note that in the user this reference can be an external entity. For example, in C ++ 11 you often have to solve the problem weak_ptr , but in principle it is a very trivial problem that needs careful consideration.


Comments

Popular posts from this blog

java - Can't add JTree to JPanel of a JInternalFrame -

javascript - data.match(var) not working it seems -

javascript - How can I pause a jQuery .each() loop, while waiting for user input? -