Solved

how to free a structure unlocked by mutex in C

Posted on 2004-10-16
12
224 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:webusername
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 45

Expert Comment

by:Kdo
ID: 12327633
Hi webusername,

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


Good Luck!
Kent
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12327664
Hi,
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
like
pthread_mutex_lock(&shared.mutex);

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.
van_dy.
0
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 500 total points
ID: 12328259
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;
      pthread_mutex_lock(&aStruct->mutex);
      tmp = aStruct;
      aStruct = NULL;
      pthread_mutex_unlock(&tmp->mutex);
      pthread_mutex_destroy(&tmp->mutex);      // and the pthread_mutex_attr too, if appropriate
      free(tmp);
}

0
 

Author Comment

by:webusername
ID: 12329156
Actually the mutex is not embedded into the structure, but it is appliead on it
0
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 12331005
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
      pthread_mutex_lock(...);
      if (ptr) {      // Check again, memory might have been freed while we blocked on mutex
            free(ptr);
            ptr = NULL;
      }
      pthread_mutex_unlock(...);
}


0
 

Author Comment

by:webusername
ID: 12331206
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 *
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 5

Expert Comment

by:van_dy
ID: 12331212
Hi,
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.

regards,
van_dy
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12331215
webuser,
         You will not need to free(data) unless u have allocated it dynamically.
0
 

Author Comment

by:webusername
ID: 12331224
i am doing something like this
data d;
                    pthread_mutex_lock(&lock);
                                                  d = whatever;
                     pthread_mutex_unlock(&lock);

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
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12331238
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,
regards.
0
 
LVL 12

Expert Comment

by:stefan73
ID: 12345443
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");
free(myString);

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

Cheers!

Stefan
0
 
LVL 9

Expert Comment

by:Cayce
ID: 12349039
Would you please post your code?
That certainly would help us all on helping you.
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now