mutex

R

Ron Eggler

Hi,

I wanna use a mutex to make a class thread safe. I initialize it in its
constructor like:

GPIOcontrol::GPIOcontrol()
{//constructor
pthread_mutex_init(GPIO_mtx, NULL);//initializing the mutex
}

But my application just terminates after issueing thiss line. I'm using gcc
4.1.3
I have no clue what it's doing, GPIO-mtx is declared like this as a private
member of this class:
pthread_mutex_t *GPIO_mtx; // mutex declaration
The weird thing is now, it works just fine by declaring the member like:
pthread_mutex_t GPIO_mtx; // mutex declaration
and calling the mutex_init like:
pthread_mutex_init(&GPIO_mtx, NULL);//initializing the mutex
Why is this????
That's weirdin me out. Anyone an idea?
 
V

Victor Bazarov

Ron said:
Hi,

I wanna use a mutex to make a class thread safe. I initialize it in
its constructor like:

GPIOcontrol::GPIOcontrol()
{//constructor
pthread_mutex_init(GPIO_mtx, NULL);//initializing the mutex
}

But my application just terminates after issueing thiss line. I'm
using gcc
4.1.3
I have no clue what it's doing, GPIO-mtx is declared like this as a
private member of this class:
pthread_mutex_t *GPIO_mtx; // mutex declaration

It's not a mutex declaration. It's a _pointer_to_mutex_ declaration.
Where is the actual mutex? What memory does this pointer point to?
The weird thing is now, it works just fine by declaring the member
like: pthread_mutex_t GPIO_mtx; // mutex declaration
and calling the mutex_init like:
pthread_mutex_init(&GPIO_mtx, NULL);//initializing the mutex
Why is this????
That's weirdin me out. Anyone an idea?

Learn to use pointers.

V
 
A

Andrey Tarasevich

Ron said:
...
But my application just terminates after issueing thiss line. I'm using gcc
4.1.3
I have no clue what it's doing, GPIO-mtx is declared like this as a private
member of this class:
pthread_mutex_t *GPIO_mtx; // mutex declaration

Mutex declaration? No. It is a 'pointer to mutex' declaration. It is
supposed to _point_ to mutex. It is your responsibility to make sure
that it points to an existing mutex object before you call any
mutex-related operations on it. You failed to do that. Your pointer most
likely contains garbage, hence the strange behavior of the program.
The weird thing is now, it works just fine by declaring the member like:
pthread_mutex_t GPIO_mtx; // mutex declaration
and calling the mutex_init like:
pthread_mutex_init(&GPIO_mtx, NULL);//initializing the mutex
Why is this????

In that case you declare the actual mutex object and operate on it,
which is supposed to work, not surprisingly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top