LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2011, 08:21 AM   #1
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Rep: Reputation: 38
Pthread safe?


Hi,

is it safe to allocate memory(malloc) for some data and then create a pthread with this data and later in the new pthread free the memory?


like this:
void* func2(void* data)
{

.... free(data);
}

void func1()
{
...data = .... malloc...
... pthread_create(..., func2, data);
}


How do i call a certain object in another thread? Do i store obj pointer in a global variable? Are instances shared memory among threads?

Im little confused about threads!
 
Old 10-08-2011, 06:06 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by kalleanka View Post
is it safe to allocate memory(malloc) for some data and then create a pthread with this data and later in the new pthread free the memory?
It is safe, as long as no thread attempts to access the memory during or after the free() call. You can free() a pointer allocated using malloc() in any thread -- there is no need to do that in the same thread at all --, as long as you make sure no thread attempts to access the memory during or after the free().

Quote:
Originally Posted by kalleanka View Post
How do i call a certain object in another thread?
All you need is a pointer to the other object.

Quote:
Originally Posted by kalleanka View Post
Do i store obj pointer in a global variable?
That should work. Note that doing that is usually not thread-safe, unless you protect accesses to the global variable with a mutex, or use atomic operators (__sync_bool_compare_and_swap) provided by GCC and most other recent C compilers when modifying the global variable.

Quote:
Originally Posted by kalleanka View Post
Are instances shared memory among threads?
All threads see the same memory space. Whatever memory one thread sees, another thread sees at the same address (pointer), exactly the same way.
 
Old 10-09-2011, 10:18 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by Nominal Animal View Post
It is safe, as long as no thread attempts to access the memory during or after the free() call. You can free() a pointer allocated using malloc() in any thread -- there is no need to do that in the same thread at all --, as long as you make sure no thread attempts to access the memory during or after the free().
In addition, you should pthread_cleanup_push(&free, data); with the new pointer. Instead of an explicit free you would use pthread_cleanup_pop(1);, provided you haven't pushed any more handlers on top of that mentioned. Of course, you don't really need all this if you plan to never cancel a thread in your program, now or in the future. You also need a free after pthread_create if thread creation fails, whether or not you use cancelation.
Kevin Barry

Last edited by ta0kira; 10-09-2011 at 10:21 PM.
 
Old 10-14-2011, 04:55 AM   #4
kalleanka
Member
 
Registered: Aug 2003
Location: Mallorca, Spain
Distribution: xubuntu
Posts: 551

Original Poster
Rep: Reputation: 38
Thanks for the explanations and for good tips.
 
Old 10-14-2011, 02:11 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
If you are freeing a shared memory object, you should have some kind of mutual-exclusion mechanism surrounding the operation: a thread must acquire the mutex, free the memory, set the pointer that referred to it to NULL, then release the mutex.

(The "extra step" of setting the pointer value to NULL is not accidental. I firmly believe that a pointer's value should always be "known good." If it points to something, that object should be known good; otherwise, it should be null. The value should never be "stale." Take the extra time at startup of initializing all pointer tables to binary zeroes. Also take the extra time to use calloc() to allocate memory blocks and set them to known-zero. You'll be glad you did. It never hurts to allocate a couple extra unused known-zero bytes at the end of a data-structure, either ...)

In general, access to all shared memory structures (or any other sort of shared resource) should be protected by a mutual-exclusion mechanism. For instance, you can use a "read-many write-once" type of mechanism to allow many threads to secure the simultaneous right to read the data structure (thus maintaining stability and data-integrity), and for one thread at a time to secure the right to modify it. Otherwise, in the general case, you can never quite be sure if someone might not be harboring a pointer to something and another thread frees it ... or two threads race to pick up a pointer and both try to release it at the same time, and so on.

In general, the key to designing bulletproof multi-threaded applications is to be very cautious and disciplined about this issue, no matter exactly what the shared resource is.

Last edited by sundialsvcs; 10-14-2011 at 02:15 PM.
 
Old 10-14-2011, 09:39 PM   #6
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
I realize that this is all ready solved. I make it a point to always free memory in the same thread in which it was allocated. This makes it easier to tell that the memory was freed when reviewing the code. This also assures that a child thread can not free memory before a sibling thread is finished using it.

All threads share access to the memory allocated in the parent thread, so it is necessary to use mutex for data access.

The spawning thread usually needs to use a semaphore set at thread creation and adusted by every spawned thread after the child thread's last access of the memory to guarantee that all child threads have completed before freeing the memory.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
libtest.a uses pthread: user of libtest.a should not link pthread again debulu Programming 2 01-31-2007 09:23 PM
Is curses pthread safe? sledhead Programming 1 04-30-2005 11:30 AM
Pthread in c++ BoldKiller Programming 3 08-30-2004 05:52 AM
pthread zaman Programming 6 08-29-2004 04:04 PM
Pthread rch Programming 1 05-28-2003 02:20 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:50 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration