Getting ENOMEM with pthread_mutex_init

Jilles Tjoelker jilles at stack.nl
Wed May 15 21:19:08 UTC 2013


On Sat, May 04, 2013 at 01:29:23PM +0200, Radio młodych bandytów wrote:
> I'm having troubles that I seem unable to resolve.
> My programs creates and destroys mutexes repeatably (and, obviously,
> uses them in between). Around the 60th lock created, I always get ENOMEM.
> I have free memory, lots of it. All locks get released properly.

> The relevant pieces of code:

> #define MUTEX pthread_mutex_t
> inline MUTEX create_mutex()
> {
>     MUTEX mutex;
>     int ret = pthread_mutex_init(&mutex, NULL);
>     if(ret != 0)
>         throw std::runtime_error("Failed to create a mutex");
>     return mutex;
> }

> inline void destroy_mutex(MUTEX *mutex)
> {
>     int ret = pthread_mutex_destroy(mutex);
>     if(ret != 0)
>         throw std::runtime_error("Failed to destroy a mutex");
> }

> Scheduler::Scheduler(char* in,
>                      char* out,
>                      BlockInfo* metadata,
>                      size_t isize,
>                      size_t block_size,
>                      size_t iters,
>                      size_t min_work_size) :
>         in(in),
>         current_in(in),
>         out(out),
>         current_out(out),
>         metadata(metadata),
>         current_metadata(metadata),
>         size(isize),
>         size_left(isize),
>         block_size(block_size),
>         iters_left(iters)
> {
>     lock = create_mutex();
>     work_size = (min_work_size / block_size) * block_size;
>     if (work_size < min_work_size)
>         work_size += block_size;
> }
> Scheduler::~Scheduler()
> {
>     destroy_mutex(&lock);
> }

> Any suggestions?

It is probably not the cause of your problem but using a copy of a
pthread_mutex_t for synchronization is not allowed. pthread_mutex_init()
should be called on the pthread_mutex_t that is part of the Scheduler.

-- 
Jilles Tjoelker


More information about the freebsd-threads mailing list