Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working around invalid conversion from const

I am compiling Percona (MySQL variant) on my Raspberry Pi, which has an ARM processor.

I've encountered an issue during compilation that has been reported, but nobody is willing to fix because ARM is an unsupported platform.

https://bugs.launchpad.net/percona-server/+bug/1002848

I've managed to work around the issue and successfully compile, but my knowledge of c++ is somewhat lacking and I don't fully understand if I have actually broken something or not.

I've read through lots of the invalid conversion from const char* to char* questions on SO, which is where I got the idea for this workaround.

The error was as follows:

error: invalid conversion from 'const pthread_mutex_t*' to 'pthread_mutex_t*'

(it actually wasn't pthread_mutex_t on my platform, but the issue is the same - actual type lost to the infinite abyss that is the scrollback buffer)

The offending code was:

  uint32 count(uint index) const
  {
    my_atomic_rwlock_rdlock(&time_collector_lock);

I changed this to:

  uint32 count(uint index) const
  {
    my_atomic_rwlock_t dummy = time_collector_lock;
    my_atomic_rwlock_rdlock(&dummy);

time_collector_lock is defined as:

private:
  my_atomic_rwlock_t time_collector_lock;

Due to the fact this is supposed to be a mutex, I have a feeling I've probably made this non-thread-safe. Or is this ok?

Is there a better solution?

like image 363
Leigh Avatar asked Jul 16 '12 09:07

Leigh


1 Answers

It seems, in the class, you have declared the member data as:

pthread_mutex_t time_collector_lock;

so in the const member function, this member data becomes as if you've declared it as:

const pthread_mutex_t time_collector_lock; //(as-if declaration)

which is causing the problem, as you cannot pass pointer to const object to my_atomic_rwlock_rdlock() which is expecting pointer to non-const object.

The keyword mutable can save you here. Declare the member data as mutable object as:

 mutable pthread_mutex_t time_collector_lock;
//^^^^^^note this

Now you can use the member data in const member function as well:

uint32 count(uint index) const
{
   my_atomic_rwlock_rdlock(&time_collector_lock);  //ok now!
like image 124
Nawaz Avatar answered Oct 20 '22 15:10

Nawaz