I have something like this:
typedef struct
{
pthread_mutex_t mtx;
/* Other stuff */
} some_struct_t;
void some_func (some_struct_t *s)
{
pthread_mutex_lock (&s->mtx);
/* Some stuff */
pthread_mutex_unlock (&s->mtx);
}
some_func does not modify s, and I would like to change the signature to
void some_func (const some_struct_t *s);
but the calls to pthreads functions won't allow me to do that without warnings.
Is there any common idiom I could use to express that the some_struct_t will be logically constant inside the function?
Any way to use some_func from inside another function which has a const some_struct_t *s without doing a cast?
some_funcmodifies themtxmember, so it cannot be const.But you can make
mtxa pointer. You then still change the mutex, but that will no longer be covered by the const.Now
some_funcno longer modifiess, but initializing asome_structvariable (and cleaning it up) has become a little bit more involved.