Working with the wrong conversion from const

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

I had a problem during compilation that was reported, but no one wants to fix because ARM is an unsupported platform.

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

I managed to get around this problem and successfully compile, but my knowledge about C ++ is somewhat lacking, and I don’t quite understand that I really broke something or not.

I read a lot of invalid conversions from const char * to char * questions on SO, 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*' 

(actually it was not pthread_mutex_t on my platform, but the problem is the same - the actual type lost for the endless abyss, which is the scroll buffer)

Violation Code:

  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 that this should be a mutex, I have a feeling that I probably did this without streaming security. Or is this normal?

Is there a better solution?

+6
source share
1 answer

It seems in the class you declared the element data as:

 pthread_mutex_t time_collector_lock; 

therefore, in the function of the const member, the data of this member becomes as if you declared as:

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

which causes the problem because you cannot pass a pointer to a const object to my_atomic_rwlock_rdlock() , which expects a pointer to a non-constant object.

The mutable keyword can keep you here. Declare the element data as a mutable object as:

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

Now you can use the element data in the const member function:

 uint32 count(uint index) const { my_atomic_rwlock_rdlock(&time_collector_lock); //ok now! 
+10
source

Source: https://habr.com/ru/post/920570/


All Articles