Advertisement

05.13.2008 at 03:06PM PDT, ID: 23399628
[x]
Attachment Details

thread safety and malloc/free

Asked by chsalvia in C Programming Language

As far as I know, the C standard is silent about the thread-safety of malloc and free (and anything else concerning threads, for that matter.)

I've developed a lot of multithreaded programs using POSIX threads, and I have never had a problem with malloc/free.  So I assume that on my implementation malloc/free are thread safe.  (I'm using GCC 4.1.2 on Linux Debian 4)

However, I've read complaints on the Internet about random crashes with multi-threaded programs that use malloc/free.

So, is the use of mutexes in combination with malloc/free a requirement for writing portable code?  I never use mutexes for malloc/free, so I fear porting my code to other platforms/compilers may require a bit of work.  Start Free Trial
 
Keywords: thread safety and malloc/free
 
Loading Advertisement...
 
[+][-]05.13.2008 at 03:51PM PDT, ID: 21559955

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.13.2008 at 04:39PM PDT, ID: 21560189

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.14.2008 at 12:31AM PDT, ID: 21561845

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: C Programming Language
Sign Up Now!
Solution Provided By: Infinity08
Participating Experts: 4
Solution Grade: A
 
 
[+][-]05.14.2008 at 06:16AM PDT, ID: 21563774

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]05.14.2008 at 06:26AM PDT, ID: 21563869

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.14.2008 at 06:46AM PDT, ID: 21564093

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
Microsoft
  • Internet Protocols
  • Applications
  • Development
  • OS
  • Hardware
  • Windows Security
Apple
  • Operating Systems
  • Hardware
  • Programming
  • Networking
  • Software
Internet
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Spy / Ad Blockers
  • Web Browsers
  • New Net Users
  • Web Development
  • Chat / IM
  • Anti Spam
  • Web Servers
  • Anti-Virus
  • Email Clients
Gamers
  • Tips
  • Online / MMORPG
  • Puzzle
  • Emulators
  • Action / Adventure
  • Role Playing
  • Consoles
  • Game Programming
  • Strategy
  • Sports
  • Misc
  • Computer Games
Digital Living
  • Hardware
  • Automotive
  • New Net Users
  • New Users
  • Software
  • Digital Music
  • Gaming World
  • Home Security
  • Apple
  • Networking Hardware
Virus & Spyware
  • Vulnerabilities
  • IDS
  • Encryption
  • Anti-Virus
  • Operating Systems Security
  • Software Firewalls
  • WebApplications
  • Cell Phones
  • Operating Systems
  • Internet
  • Hardware Firewalls
Hardware
  • Displays / Monitors
  • Handhelds / PDAs
  • Components
  • Peripherals
  • Laptops/Notebooks
  • Servers
  • Misc
  • Apple
  • Embedded Hardware
  • Networking Hardware
  • Storage
  • Desktops
  • New Users
Software
  • System Utilities
  • Industry Specific
  • Network Management
  • Photos / Graphics
  • Page Layout
  • VMware
  • Misc
  • Web Development
  • OS
  • CYGWIN
  • Voice Recognition
  • Virtualization
  • Message Queue
  • Quality Assurance
  • Security
  • Firewalls
  • MultiMedia Applications
  • Development
  • Database
  • Office / Productivity
  • Business Management
  • OS/2 Apps
  • Server Software
  • Internet / Email
ITPro
  • OS
  • Storage
  • Encryption
  • Operating Systems Security
  • Apple Hardware
  • Laptops & Notebooks
  • Servers
  • Networking Hardware
  • Peripherals
  • Devices
  • Displays / Monitors
  • WebTrends / Stats
  • Search Engines
  • Firewalls
  • Web Computing
  • WebApplications
  • IDS
  • Vulnerabilities
  • Email Clients
  • File Sharing
  • Spy / Ad Blockers
  • Web Browsers
  • Web Servers
  • Networking
  • Anti-Virus
  • Consulting
  • Chat / IM
  • Anti Spam
Developer
  • Web Servers
  • Web Browsers
  • Game Programming
  • Dev Tools
  • Industry Specific
  • Office / Productivity
  • Database
  • CYGWIN
  • Web Development
  • Search Engines
  • File Sharing
  • WebTrends / Stats
  • Programming
  • Content Management
  • Application Servers
  • Protocols
Storage
  • Removable Backup Media
  • Storage Technology
  • Servers
  • Grid
  • Remote Access
  • Backup / Restore
  • Misc
  • Hard Drives
OS
  • Miscellaneous
  • Security
  • Development
  • Linux
  • VMware
  • MainFrame OS
  • Unix
  • Apple
  • OS / 2
  • AS / 400
  • BeOS
  • Microsoft
  • VMS / OpenVMS
Database
  • Oracle
  • Miscellaneous
  • MySQL
  • Software
  • Sybase
  • Contact Management
  • PostgreSQL
  • Data Manipulation
  • Clarion
  • InterSystems Cache
  • Siebel
  • MUMPS
  • OLAP
  • SQLBase
  • SAS
  • GIS & GPS
  • 4GL
  • Berkeley DB
  • DB2
  • Informix
  • Interbase / Firebird
  • FoxPro
  • Reporting
  • LDAP
  • Filemaker Pro
  • MS SQL Server
  • dBase
  • MS Access
Security
  • Misc
  • Web Browsers
  • Software Firewalls
  • Operating Systems Security
  • File Sharing
  • Spy / Ad Blockers
  • Vulnerabilities
  • WebApplications
  • IDS
  • Anti-Virus
  • Encryption
  • Anti Spam
  • Email Clients
  • VPN
  • Chat / IM
Programming
  • Editors IDEs
  • Installation
  • Handhelds / PDAs
  • Multimedia Programming
  • System / Kernel
  • Automation
  • Algorithms
  • Game
  • Signal Processing
  • Project Management
  • Open Source
  • Database
  • Misc
  • Languages
  • Processor Platforms
  • Theory
Web Development
  • Scripting
  • Blogs
  • Web Servers
  • Software
  • Search Engines
  • Web Graphics
  • Web Services
  • Images
  • Internet Marketing
  • Images and Photos
  • Components
  • Document Imaging
  • Web Languages/Standards
  • Illustration
  • WebApplications
  • Fonts
  • WebTrends / Stats
  • Authoring
  • Digital Camera Software
  • Miscellaneous
Networking
  • Protocols
  • Apple Networking
  • Network Management
  • Message Queue
  • Application Servers
  • Content Management
  • File Servers
  • Email Servers
  • Misc
  • Java Editors & IDEs
  • Wireless
  • Networking Hardware
  • Backup / Restore
  • System Utilities
  • ISPs & Hosting
  • Web Servers
  • Storage Technology
  • Removable Backup Media
  • Servers
  • Web Computing
  • Broadband
  • Grid
  • OS / 2
  • Novell Netware
  • Unix Networking
  • Windows Networking
  • Security
  • Telecommunications
  • Operating Systems
  • Linux Networking
Other
  • Lounge
  • Business Travel
  • Community Support
  • New Net Users
  • Philosophy / Religion
  • Math / Science
  • Miscellaneous
  • URLs
  • Expert Lounge
  • Politics
  • Puzzles / Riddles
  • Automotive
Community Support
  • Suggestions
  • New to EE
  • New Topics
  • CleanUp
  • Announcements
  • General
  • Feedback
  • Input
  • EE Bugs
 
05.13.2008 at 03:51PM PDT, ID: 21559955
I think this is a problem of specific implementations of C compiler/library.
ANSI C is not aware about threads, it is a POSIX extension.
So, you should provide some kind of protection by yourself.
Also, make sure your compiler has a thread-safe library.

Assisted Solution
 
05.13.2008 at 04:39PM PDT, ID: 21560189
Access to the free store is implementation dependent; however, I can tell you that Windows will protect it with a mutex providing you are using the multi-threading C runtime, otherwise all bets are off. For a C runtime to be Posix compliant it should be thread safe; however, you should always refer to the documentation for your C runtime library.
Assisted Solution
 
05.14.2008 at 12:31AM PDT, ID: 21561845
The GNU C library (glibc) is thread safe (and POSIX compliant) if you link the pthread library to the application (unless you're using a very old version of glibc). That includes malloc :)
Accepted Solution
 
05.14.2008 at 06:16AM PDT, ID: 21563774
Changing your implementations to be thread-safe won't require much work. I'd do it like this (pseudocode):
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
void* my_malloc(size_t size)
{
    // Acquire mutex ...
 
    void* p = malloc(size);
 
    // Release mutex ...
 
    return p;
}
 
#define malloc(size) my_malloc(size)
Open in New Window
Assisted Solution
 
05.14.2008 at 06:26AM PDT, ID: 21563869
>> Changing your implementations to be thread-safe won't require much work. I'd do it like this (pseudocode):
It's probably unnecessary. Like I said, you should check the C runtime docs but both Windows MT CRT and Linux glibc are thread safe. This being the case, doing this will just add an extra unnecessary level of complexity and contention into the code base.

 
05.14.2008 at 06:46AM PDT, ID: 21564093
Yes, I just showed the way to go if the code needs to be ported to thread unsafe implementations of standard library. Of course it's an unnecessary trick with most popular implementations.

If at some point this code must be compiled with unsafe malloc/free, the declaration of my_malloc() can be placed in an #ifdef block to ensure it's only used with that particular library or compiler, anyway.
 
 
20080716-EE-VQP-32 / EE_QW_2_20070628