Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Creating mutex pointer in c++

795 views
Skip to first unread message

kushal bhattacharya

unread,
Apr 29, 2017, 3:50:30 AM4/29/17
to
Hi,
Is it possible to create a mutex pointer in c++?If so ,which is the best method to do so so that i could use it as a normal mutex to pass it to like lock_guard or to condition_variable's wait.Is it really possible ?
Thanks,
Kushal

Marcel Mueller

unread,
Apr 29, 2017, 10:11:28 AM4/29/17
to
On 29.04.17 09.50, kushal bhattacharya wrote:
> Hi,
> Is it possible to create a mutex pointer in c++?

What is a "mutex pointer"?


Marcel

Jorgen Grahn

unread,
Apr 29, 2017, 12:52:21 PM4/29/17
to
A pointer to a mutex, surely?

Mutex* my_mutex_pointer = &my_mutex;

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Chris M. Thomasson

unread,
May 1, 2017, 11:26:01 AM5/1/17
to
Not exactly sure what you mean. However, here is a quick example:
_____________________________
std::mutex mtx;
std::mutex* ptr = &mtx;

{
std::unique_lock<std::mutex> lock(*ptr);
}
_____________________________


kushal bhattacharya

unread,
May 3, 2017, 1:59:36 AM5/3/17
to
I was looking for this exact stuff i found it thnks :)

Bonita Montero

unread,
May 3, 2017, 1:27:57 PM5/3/17
to
> std::unique_lock<std::mutex> lock(*ptr);

Or with C++17's class template deduction:

std::unique_lock lock( *ptr );

kushal bhattacharya

unread,
May 4, 2017, 2:37:54 AM5/4/17
to
hi,
Is it any differnent than the previous approach?

Bonita Montero

unread,
May 4, 2017, 4:24:28 AM5/4/17
to
>>> std::unique_lock<std::mutex> lock(*ptr);

>> Or with C++17's class template deduction:
>> std::unique_lock lock( *ptr );

> hi,
> Is it any differnent than the previous approach?

It works only with compilers that support C++ with the class template
deduction feature. This is only a convenience-feature. The resulting
code is exactly the same.

Chris M. Thomasson

unread,
May 4, 2017, 4:07:36 PM5/4/17
to
I agree that it is a convenience. Not sure if I totally like it or not,
but its is very convenient for sure. My skills in C++ can be old-school
from time to time. I like Alf P. Steinbach's code because it helps me
visualize newer features. :^)

Common now, try to give proper attributes wrt quoting others. Pretty Please?

;^o

kushal bhattacharya

unread,
May 5, 2017, 2:31:30 AM5/5/17
to
exactly :-)

kushal bhattacharya

unread,
May 5, 2017, 3:30:55 AM5/5/17
to
thanks for this approach maam :)

Chris M. Thomasson

unread,
May 11, 2017, 5:18:50 PM5/11/17
to
Bonita is okay. She just should try to give a proper attribution for
once! Damn it, even a cut-and-paste of a name would be a first step.


0 new messages