[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

how to free a structure unlocked by mutex in C

Hi , i haved used mutex (pthread programming ) to lock then unlocked a structure in C .But now i cannot access it so i figured out that i have to free it .how do i do that .thanks
  • 4
  • 3
  • 2
  • +3
1 Solution
Kent OlsenData Warehouse Architect / DBACommented:
Hi webusername,

You have to unlock it before you can free it.  Make sure that you're unlocking the region.

Good Luck!
Can you please explain your question a little bit more?

I assume you have shared structure of some form like;
struct {
         pthread_mutex_t mutex;
        char data[SIZE];
      // other parts of the structure
} shared;

now when one thread acquires the mutex lock by a call

the other thread cant access the structure unless the first
thread unlocks it. So make sure that you unlock it. That is
you need to unlock the structure before you do anything else.

hope this helps.
Like van_dy, I am assuming the mutex is embedded in the
structure you wish to free.  However, if you wish to free the
structure, then you must have dynamically allocated it earlier.  
Since you allocated it dynamically rather than statically or auto,
all threads access the structure via a pointer:

struct shared * aStruct = (struct shared *)calloc(1, sizeof(struct shared));
pthread_mutex_init(&aStruct->mutex, ...);

Then at destruction time, all you have to do is lock the mutex,
disassociate the structure from the pointer (so that no other
threads can find it), then unlock the mutex, destroy the mutex,
and free the memory:

if (aStruct != NULL) {
      struct shared * tmp;
      tmp = aStruct;
      aStruct = NULL;
      pthread_mutex_destroy(&tmp->mutex);      // and the pthread_mutex_attr too, if appropriate

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

webusernameAuthor Commented:
Actually the mutex is not embedded into the structure, but it is appliead on it
Then what is the problem?

lock the mutex
free the memory
NULL the pointer to memory
unlock the mutex

Add checks for NULL pointer accordingly:

if (ptr) {      // If ptr not NULL, free the memory
      if (ptr) {      // Check again, memory might have been freed while we blocked on mutex
            ptr = NULL;

webusernameAuthor Commented:
i have this structure declared as data d (that is not a pointer) so when i do free(data) such that data is not of type void *
You have the mutex outside you struct? In that case do you
have more than one such dynamically allocated structures,
possibly in a linked list or as an array of pointers which are
all locked and unlocked by a single mutex?  It would be
much easier  if you could clarify this, because the way you
are going to free the structures will depend on the way you actually
stored them. However brett's solutions for both the cases are
correct indeed.

         You will not need to free(data) unless u have allocated it dynamically.
webusernameAuthor Commented:
i am doing something like this
data d;
                                                  d = whatever;

later on , when i try to run a statement that involves d ,the program is frozen at that point so i figured out that d is not freed even though i have used unlock
hi webusername,
        you see, mutex locking is an advisory kind of locking. threads access
their shared data by locking and unlocking with an understanding that every
thread will try to acquire the lock before accessing the data and free it once it
is done. However it is possible for a rogue thread to disobey this and directly
access the data without acquiring the mutex lock.  I hope you see my point now.
You can access 'data d' anywhere and anytime you want, access to it is not prevented
through the mutex lock. threads only cooperate through the mutex lock while accessing
the data so that they arent modifying or doing whatever withit simultaneously.

       If your program 'freezes' as you say, then i think it has got some other problem
rather than this mutex lock one.

hope this helps,
Hi webusername,
> i have this structure declared as data d (that is not a pointer) so
> when i do free(data) such that data is not of type void *

That's not a problem. malloc() and free() use void* to express that anything can be generated or freed. You don't need to cast to void* before doing free():

char* myString = (char*) malloc(12345);
strcpy(myString,"all bits'n'bytes");

Every kind of pointer is fine for a void pointer. It's a kind of opaque pointer.


Would you please post your code?
That certainly would help us all on helping you.

Featured Post

Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

  • 4
  • 3
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now