by tylertreat · 12y ago · 0 comments
struct chan_t { pthread_mutex_t* m_mu;... }
And initialize it like so: pthread_mutex_t* mu =
(pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(mu, NULL);
chan->m_mu = mu;
eps's suggestion is to put the mutex directly in the struct: struct chan_t { pthread_mutex_t m_mu;... }
and initialize it by passing its address to pthread_mutex_init: pthread_mutex_init(&chan->m_mu, NULL);
chan->m_mu = mu;
that saves some mallocs and dereferences. If you are new to C, the notion of taking the address of fields in a struct may be unnerving, but you get used to it :)Valgrind is your friend :)
http://valgrind.org/docs/manual/quick-start.html#quick-start...
struct foo
{
pthread_mutex_t bar;
...
};Just allocate a pointer on the stack, read into it, and then assign the value into the provided space:
void *msg_ptr;
read(..., &msg_ptr, sizeof msg_ptr);
*data = msg_ptr;