How to define a common mutex between two classes in C++ - multithreading

I know how to define a mutex as a private variable in a class and then use it in the member functions of that class. But, now I want to define a mutex which can be used in two classes. I mean, I have two classes (class A and class B) and I want to define a mutex which is followed and used in member functions of both class A and class B. How can I do this?
Thanks in advance.

Related

How to draw two references from one class to another in a UML class diagram?

Imagine the following case: I have a class Method and a class DataType. A method has one return type (if we count void as a return type) and zero to many parameters. Both the return type and the parameters are instances of the class DataType.
How would I model this Situation in a UML class diagram? One line for two references or one for each?
Solution A, one line:
Solution B, two lines:
The best way would be to use role names instead:
Role names are explicit attributes in the opposite class, telling how the specific class is used. So Method.parameter is of type DataType.
Note that I changed the return type to be 0..1 rather than 1 since your text tells that it's optional.

Two classes differing only in base class

I have come across a problem where I need two classes that will have identical implementation and the only difference between them will be different name and base class. What is a reasonable way of doing this?
One obvious solution is to violate DRY and copy the implementations like this:
class FooA(BaseA):
def frobnicate(self):
print("frob")
class FooB(BaseB):
def frobnicate(self):
print("frob")
You can use multiple inheritance to implement interfaces and common functionality in a single mixin class. Given the clear desire to frobnicate in many classes, just implement a frobnicator. Python builds the class from right to left so mixins are left-most.
class Frobnicator(object):
def frobnicate(self):
print("frob")
class FooA(Frobnicator, BaseA):
pass
class FooB(Frobnicator, BaseB):
pass
Note that mixins usually do not implement their own __init__ - that's the job of the base class.

Representing class variables and methods in UML

Many programming languages allow you to define class/instance methods, and the same for attributes. E.g. Python, Smalltalk. I have the concepts. E.g. for instance variables, every object has it’s own copy of the variables. Class variables only have one copy of the variables shared with all instances of the class.
My doubt is: how do I represent class methods and class attributes in UML? I was thinking in represent it through static, as in C++, Java, and C#, but is it ok? "static" and "class" are the same in UML?
Thanks!
Static attributes/operations1 need to be underlined. See also Class diagrams
1The term method is used for behavior (the howto) in UML. An operation is the term used in UML for a BehaviorialFeature that can be called on an interface (which is what you find in the compartment underneath the attributes).

How to represent a static relationship in an UML class diagram

I'm having trouble finding a good answer for how to represent a relationship between two classes A and B, where an instance of A is a static (class scope) variable in B. For example:
class A {
}
class B {
static A a;
}
I'm not even sure if it is a regular association or a dependency (or something else?).
One idea would be to use a stereotype on the role name of A in the relationship, but I have never seen that done. And since I understand that it is 'rule' not to use both an attribute and a relationship to represent the same member I can't either underline an attribute called 'a' (since I rather want to model the contents of the class A).
Just use a stereotype <<static>> to model static relationships or attributes

UML Sequence Diagram Showing creation of a sub class?

In a UML diagram when you create an instance of a subclass do you usually include the implicit construction of the superclass prior to the sub class constructor?
I usually wouldn't include it. The purpose of the UML sequence diagram is to show what happens between components. It shouldn't be read isolated from other parts of a design, so if a reader is unsure about what any of the components is (i.e. an instance of the subclass and the superclass), he or she should look into the - hopefully - accompanying class diagram.
sequence just shows the sequence of the logic of the module in question. Do you feel there is need to identify which method is truly being called? Also I would guess that the purpose of having a parent clas have a reference to a subclass is that until runtime you won't know which subclass is actually being referred to. If this is not the case, then should the concrete subclass be referred to explictly? does the method being called whether on the subclass or parent class alter the sequence in some way?

Resources