Sponsored Content
Top Forums Programming question regarding multithreading and malloc() requests Post 302172599 by Praveen_218 on Tuesday 4th of March 2008 08:30:37 AM
Old 03-04-2008
Quote:
Originally Posted by JamesGoh
Is it generally not a good idea in a multithreaded program to make lots of malloc calls (dynamic memory requests) because of the limited nature of the memory heap ?

Out of curiosity (warning it may sound silly), but would using mutexs (mutual exclusion) help to minimise chances of runtime errors ?

regards


Its not about a good or a bad idea in using malloc() its about the requirements of the project. You cannot have a project using only static variables. In large projects, without malloc()/calloc(), its not possible to create dynamic variables and objects.

Yeah, the following problems are more prominent in multithread environments than in the single threaded environments ---:

1) Getting dangling pointers
2) Freeing a pointer multiple times (freeing in only one function; but getting the same function called in context of many threads)
3) Getting memory leaks easily.


It's always a trade off; speed vs size + flexibility --:
Accessing static variables are faster (created on the program stacks) than accessing dynamic variables (as these are created on the heaps) and you loose flexibilities (required for large server apps) and the binary size thus produced is bigger compared to using the dynamic variables (unsuitable for embedded applications).

However there are some good tools available like ValGrind, Dmalloc etc. that help in debugging memory related issues and they are very helpful if used along with GDB.


As of the second part of your question on mutexes; it's a bit vague as mutexes are used to control simultaneous access to shared resources where there is a race conditions.
If its not a condition of simultaneous access; one should ALWAYS check as follows --:

If (NULL != myPointer){
..... .....
//use the pointer
.... ....
}; //Before using any pointer to operate through it.

OR in mutual exclusion conditions----:

mutex_lock(&myMutex);
//Below pointer operates on shared memory area.
if (NULL != myPointer){
..... .....
//use the pointer
.... ....
}; //Before using any pointer to operate through it.
mutex_unlock(&myMutex);
 

9 More Discussions You Might Find Interesting

1. Programming

Multithreading in Pro*C

:confused: Hi! I have created a Multhreaded Application in Pro*C (using pthreads) with about 5 Threads running simultaneously. The Application is basically to Update a Centralized Table in Oracle, which updates different rows in the Table (Each Thread updates different rows!). The... (16 Replies)
Discussion started by: shaik786
16 Replies

2. Programming

multithreading on OSX

Hi all, I have a query about multithreading. What I would like to do is, at the start of my main update() function, start a couple of threads in parallel, once they are all complete carry on with my main update function. void update() { thread1->update(); // fluid solver ... (3 Replies)
Discussion started by: memoid
3 Replies

3. UNIX for Advanced & Expert Users

multithreading in UNIX

Hi, Can you please give me a suitable reference to learn multithreading programming in C in UNIX? Thanks (3 Replies)
Discussion started by: naan
3 Replies

4. Shell Programming and Scripting

Multithreading program

Hi I need to insert 1million records into MySQL database, but it is taking lot of time as there is no bulk insert support. I want to spawn 10 processes which will insert 100k records each parallely. Can somebody help me with a example program to execute this task through shell scripting. (5 Replies)
Discussion started by: sach_roger
5 Replies

5. Programming

MultiThreading using Pthreads

Situation: i have multiple pthread_create calls like this: pthread_create(...., ThreadFunc1,.....); pthread_create(...., ThreadFunc2,.....); . . which i am using to create multiple threads.All the "ThreadFunc<i>" functions are actually calling same function "Receive" of a class using same... (3 Replies)
Discussion started by: Sastra
3 Replies

6. IP Networking

how to do udp broadcast with multithreading

hello to all i want to use multithreading to my UDP broadcast server client program. will anyone help me by proving C code. i am working in fedora. also my requirement is POSIX compliance.please help me..... (0 Replies)
Discussion started by: moti12
0 Replies

7. Programming

how to do udp broadcast with multithreading

hello to all i want to use multithreading to my UDP broadcast server client program. will anyone help me by proving C code. i am working in fedora. also my requirement is POSIX compliance.please help me..... (6 Replies)
Discussion started by: moti12
6 Replies

8. What is on Your Mind?

Alarm interrupt and multithreading

Hi Friends any know how became a friend in this Android Programming Language (0 Replies)
Discussion started by: ljarun
0 Replies

9. Programming

Help with multithreading

I take this question of the The Linux Programming Interface: A Linux and Unix System Programming page 652 exercise 30.1 I want someone to explain the under line statement because it sounds complex to me couldn't understand anything 30-1 Modify the program (thread_incr.c) so that each loop in... (3 Replies)
Discussion started by: fwrlfo
3 Replies
MALLOC_USABLE_SIZE(3)					     Linux Programmer's Manual					     MALLOC_USABLE_SIZE(3)

NAME
malloc_usable_size - obtain size of block of memory allocated from heap SYNOPSIS
#include <malloc.h> size_t malloc_usable_size (void *ptr); DESCRIPTION
The malloc_usable_size() function returns the number of usable bytes in the block pointed to by ptr, a pointer to a block of memory allo- cated by malloc(3) or a related function. RETURN VALUE
malloc_usable_size() returns the number of usable bytes in the block of allocated memory pointed to by ptr. If ptr is NULL, 0 is returned. ATTRIBUTES
For an explanation of the terms used in this section, see attributes(7). +---------------------+---------------+---------+ |Interface | Attribute | Value | +---------------------+---------------+---------+ |malloc_usable_size() | Thread safety | MT-Safe | +---------------------+---------------+---------+ CONFORMING TO
This function is a GNU extension. NOTES
The value returned by malloc_usable_size() may be greater than the requested size of the allocation because of alignment and minimum size constraints. Although the excess bytes can be overwritten by the application without ill effects, this is not good programming practice: the number of excess bytes in an allocation depends on the underlying implementation. The main use of this function is for debugging and introspection. SEE ALSO
malloc(3) COLOPHON
This page is part of release 4.15 of the Linux man-pages project. A description of the project, information about reporting bugs, and the latest version of this page, can be found at https://www.kernel.org/doc/man-pages/. GNU
2017-09-15 MALLOC_USABLE_SIZE(3)
All times are GMT -4. The time now is 01:28 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy