To expand on the existing answer:
ffmpeg's av_lockmgr_register()
is the way to deal with locking.
An ffmpeg build with threads (and a later version than LIBAVCODEC_VERSION_MAJOR 55
, LIBAVCODEC_VERSION_MINOR 38
and LIBAVCODEC_VERSION_MICRO 100
- roughly about October 2013, see ffmpeg commit adding default lockmgr) will have a default lock manager that you can just use.
If you need to be compatible with libav then (at the time of writing, September 2016) this does not yet have a default lock manager and you need to provide your own.
Here is a pure C pthread's implementation:
static int ffmpeg_lockmgr_cb(void **arg, enum AVLockOp op)
{
pthread_mutex_t *mutex = *arg;
int err;
switch (op) {
case AV_LOCK_CREATE:
mutex = malloc(sizeof(*mutex));
if (!mutex)
return AVERROR(ENOMEM);
if ((err = pthread_mutex_init(mutex, NULL))) {
free(mutex);
return AVERROR(err);
}
*arg = mutex;
return 0;
case AV_LOCK_OBTAIN:
if ((err = pthread_mutex_lock(mutex)))
return AVERROR(err);
return 0;
case AV_LOCK_RELEASE:
if ((err = pthread_mutex_unlock(mutex)))
return AVERROR(err);
return 0;
case AV_LOCK_DESTROY:
if (mutex)
pthread_mutex_destroy(mutex);
free(mutex);
*arg = NULL;
return 0;
}
return 1;
}
which is registered like so:
ret = av_lockmgr_register(ffmpeg_lockmgr_cb);
if (ret < 0)
{
fprintf(stderr, "av_lockmgr_register failed (%d)\n", ret);
abort();
}