#Thread and Mutex as members

8 messages · Page 1 of 1 (latest)

ripe belfry
#

Hi all,

Do you know if the following is a good practice:

 - Having an object called A  which has three members: std::thread, std::mutex and a member of type object B
- This thread will be created by a method of object A (startThread for example). The thread will then spawn a function from object B which has as argument the std::mutex of object A
grave vergeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

burnt merlin
#

mutex as A member is fine if B is using A's data

#

though if B is meant to take exclusive control of A, then maybe it doesn't need to be its own class

ripe belfry
spare torrent
#

A mutex is a brick type (can't copy, can't move, it stays where it is). If you have that as a member that applies to the objects of that class too, so you can't put them in a vector anymore for example. Sometimes that's fine. If it's not you can use std::unique_ptr<std::mutex> to keep the class movable.

ripe belfry
grave vergeBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.