question regarding multithreading and malloc() requests


 
Thread Tools Search this Thread
Top Forums Programming question regarding multithreading and malloc() requests
# 1  
Old 03-04-2008
question regarding multithreading and malloc() requests

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
# 2  
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);
# 3  
Old 03-04-2008
Dynamically allocated memory (heap) in a threaded environment, as noted ,is neither a good or bad idea.

What can be a bad idea is relying on static (stack) allocations anywhere.
# 4  
Old 03-04-2008
Quote:
Originally Posted by Praveen_218

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.


Sorry, what I actually meant to say is whether using mutexes would be a good idea to help control access to the heap for simultaneous malloc() requests made by different threads.

However you did somewhat answer my question in your post, so thanks for that

Ramen Noodle, could you expand on your statement where you mention that using stack allocations anywhere is a bad idea ? btw is this related to the LIFO (last in, first out) nature of the memory stack in general ?

cheers
# 5  
Old 03-06-2008
No. This has more to do with the implementation restrictions of a threads stack
If it's pthreads and you aren't already familiar with the stack specific functions maybe
you could take a look there. man -k pthread | grep stack

Also have you considered using thread specific data instead of barrier type mutex reservations?
# 6  
Old 03-09-2008
Quote:
Originally Posted by ramen_noodle
No. This has more to do with the implementation restrictions of a threads stack
If it's pthreads and you aren't already familiar with the stack specific functions maybe
you could take a look there. man -k pthread | grep stack

Also have you considered using thread specific data instead of barrier type mutex reservations?
Out of curiosity, does the thread stack work in a similar way to the function call stack (LIF0) ?

Also would you know any good places I can visit to learn more about thread stacks ?

cheers
# 7  
Old 03-10-2008
By definition a stack is LIFO, right?
I don't know of any good sources for thread specific stack details. I do know that per thread stack size issues are fairly common, whether it be on a per process basis or on usage of the thread's stack internally.

This is one of the reason why high performance servers don't use individual threads for client handling, choosing instead high performance non-blocking event mechanisms like epoll and kqueue.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
Login or Register to Ask a Question