need to free a mutex?

I have a very simple (sample) C program as follows. I want to ensure I release any resources necessary so that valgrind does not complain. Do I need to free mutex1? Or do anything before the program terminates? Or is the mutex1 not allocate memory?

02 pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; 03 int counter=0; 04 05 /* Function C */ 06 void functionC() 07 { 08 pthread_mutex_lock( &mutex1 ); 09 counter++ 10 pthread_mutex_unlock( &mutex1 ); 11 }

-------------Problems Reply------------

No, it is fine as it is. It is not necessary to use pthread_mutex_destroy on a statically allocated mutex.

No, you don't need to free mutex1. PTHREAD_MUTEX_INITIALIZER is a macro that hides a struct initialisation.

mutex1 in your code is a global variable rather than a heap-allocated variable. You do not need to free it. The OS will free all resources that your app use when you app terminate.

Category:c# Views:0 Time:2012-02-01
Tags: mutex init free

Related post

  • Is this the proper use of a mutex? 2008-09-05

    I have a situation where I might have multiple instances of a program running at once, and it's important that just one specific function not be executing in more than one of these instances at once. Is this the proper way to use a mutex to prevent t

  • Difference between binary semaphore and mutex 2008-09-15

    Is there any difference between binary semaphore and mutex or they are essentialy same? --------------Solutions------------- They are NOT the same thing. They are used for different purposes! While both types of semaphores have a full/empty state and

  • Why do I need to SEM_PRIORITY_Q when using a VxWorks inversion safe mutex? 2008-09-20

    In VxWorks, I am creating a mutex with the SEM_INVERSION_SAFE option, to protect against the priority inversion problem. The manual says that I must also use the SEM_PRIORITY_Q option. Why is that? --------------Solutions------------- When creating a

  • Recursive Lock (Mutex) vs Non-Recursive Lock (Mutex) 2008-10-09

    POSIX allows mutexes to be recursive. That means the same thread can lock the same mutex twice and won't deadlock. Of course it also needs to unlock it twice, otherwise no other thread can obtain the mutex. Not all systems supporting pthreads also su

  • In a multi-threaded C++ app, do I need a mutex to protect a simple boolean? 2008-10-21

    I have a multi-threaded C++ app which does 3D rendering with the OpenSceneGraph library. I'm planning to kick off OSG's render loop as a separate thread using boost::threads, passing a data structure containing shared state in to the thread. I'm tryi

  • What is wrong with this tiny piece of mutex code? 2008-11-20

    // A Mutex allows threads mutually exclusive access to a resource. //----------------------------------------------------------------------- class Mutex { private: CRITICAL_SECTION m_mutex; public: Mutex() { InitializeCriticalSection(&m_mutex); }

  • C# mutex - error calling from ASP.NET and console application 2008-12-10

    I am using a global named mutex for file access synchronization between an ASP.NET application and a console application. While running the ASP.NET application, the console application fails to acquire mutex - as expected. While running the console a

  • C# - Locking issues with Mutex 2009-01-15

    I've got a web application that controls which web applications get served traffic from our load balancer. The web application runs on each individual server. It keeps track of the "in or out" state for each application in an object in the ASP.NET ap

  • C# - how can I get owner's name for a Mutex 2009-02-12

    I have a shared mutex between 2 applications. I want to create an administration console that shows the current owner's name of the mutex. How can I get the application name that currently owns the Mutex? --------------Solutions------------- I do not

  • Should Locks and Mutexes in C# be used together 2009-03-13

    Wouldn't this be overkill and only one of these necessary? I've searched and found different posts about Mutual Exclusion and locks in C# here and here. Example: In our app, we have a function that spins off multiple reconnection threads and inside t

  • Is using a Mutex to prevent multiple instances of the same program from running safe? 2009-03-14

    I'm using this code to prevent a second instance of my program from running at the same time, is it safe? Mutex appSingleton = new System.Threading.Mutex(false, "MyAppSingleInstnceMutx"); if (appSingleton.WaitOne(0, false)) { Application.EnableVisual

  • Why ActiveMQ, as opposed to a simple Queue/Mutex? 2009-04-17

    Tomorrow I am presenting my rationale for choosing an in-process message queue implementation, and I'm unable to articulate my reasoning. My co-designers are proposing that we implement a simple asynchronous queue using just a basic list of jobs and

  • What is mutex and semaphore in Java ? What is the main difference? 2009-04-21

    What is mutex and semaphore in Java ? What is the main difference ? --------------Solutions------------- Semaphore can be counted, while mutex can only count to 1. Suppose you have a thread running which accepts client connections. This thread can ha

  • Is there a tool to monitor synchronisation objects (mutex, events, semaphores) in Windows? 2009-04-29

    In Windows, is there a tool to monitor the status of process synchronisation objects? ie. event/mutex : signaled or not signaled semaphore : count Better yet, to log which thread did what, eg. "thread #5421 Signal Event - testevt" --------------Solut

  • Run single instance of an application using Mutex 2009-05-04

    In order to allow only a single instance of an application running I'm using mutex. The code is given below. Is this the right way to do it? Are there any flaws in the code? How to show the already running application when user tries to open the appl

  • Semaphore Mutex Concurrency Issue in multithreaded C program 2009-05-11

    How do i design a multithreaded C program to avoid Semaphore Mutex Concurrency --------------Solutions------------- There are a few ways. The best way is to eliminate all shared data between threads. While this isn't always practical, it's always goo

  • destructor of static class need a mutex? 2009-06-02

    We have a static (singleton) class which will be used in a mutithreaded environment. We use mutex in its constructor and other mrmber functions. However there is no mutex for the destructor. Destructor do some tasks like cleaning up of some other mem

  • .NET: Using Mutex to make sure a webservice is only called once at a time 2009-06-09

    I'm using a Mutex to make sure a webservice is only running once at a time, but I can't get it 100% right with WaitOnce and ReleaseMutex. I've got this: private static Mutex mutex = new Mutex(); [WebMethod] public bool TriggerAll() { bool ranJobs = f

  • Not locking mutex for pthread_cond_timedwait and pthread_cond_signal ( on Linux ) 2009-06-16

    Is there any downside to calling pthread_cond_timedwait without taking a lock on the associated mutex first, and also not taking a mutex lock when calling pthread_cond_signal ? In my case there is really no condition to check, I want a behavior very

  • Interprocess Mutex In Perl 2009-06-25

    I have a Perl CGI program that executes under mod_perl. Within the program, I would like to prevent a resource from accessing by multiple processes at the same time. # Semaphore Initialization Code # 10023 is unique id, and this id will be same acros

Copyright (C), All Rights Reserved.

processed in 0.094 (s). 11 q(s)