1) if this class is allocated on the heap (temp * T = new temp ()), will it be thread safe (for each instance, are not all instances together?
Yes. Since mx is not a static member of the class, there will be one lock per instance of the class.
2) if I create boost :: mutex mx → boost :: mutex * mx and allocate it in the constructor so that it is allocated on the heap, will the code be thread safe as well?
Yes. But thread safety is supported only for each instance.
3), if now the answer is 1 and 2, how can I make the thread of each instance safe?
The answers are yes, so you're fine.
In case someone else asks how to make all instances threads safe with one lock - you can make mx a static class variable.
Aater suleman
source share