Sponsored Content
Top Forums Programming Deallocating memory in multi-threaded environment. Post 302518387 by DGPickett on Friday 29th of April 2011 01:17:42 PM
Old 04-29-2011
C# and JAVA have garbage collection, and there are open source c# out there.

If you allocate using a flat file and mmap()/mmap64(), unused memory is virtual at best, and you have a hard copy you can interrogate during and after a process run. If there is temp file space used that is not mapped, who cares?

Architecturally, locks are less of a need if you send messages from each thread in its own simple queue (one writer per queue, one reader) to a manager for service. The manager can just poll the queues for services, and send any response back on a reverse queue.

I think, generally, you are right that you need a lock outside the thing deallocated, with a pointer to it, to ensure that you can deallocate it in peace. You have a struct with a count, a pointer originally null and a mutex. Everyone that takes a copy of the pointer locks, checks for null pointer, increments, takes the copy and unlocks. When done with the object, they lock, decrement and unlock. Someone has to malloc and load the pointer to make it usable. Someone has to deallocate it when nobody cares any more, but the counter only tells you how many copies are in use, not if there is no future interest. Even if no copies are outstanding, the users could want a copy, so destruction on 0 copies is an application-specific question. But the parent structure is not deallocated. It could be in a tree, so only the root is always there. That might not scale well. With most containers, usually there is a non-dynamic pointer at the top, like head=null for an empty linked list.

I have not found locks and mutexes to be important to life as it know it. Threads can be managed much like IPC, or using monotonically increasing controls so a thread may be behind, temporarily, but never ahead, like a modulo-two sized ring buffer controlled by a long long and a bit mask (size - 1) as an in-process queue. If each thread feeds off a dispatcher thread via an input queue, it just dequeues work and writes other queues, possibly to a work collector thread. Threads with nothing to do need something like a block on the queue, hard to achieve internally. Sleep of any length may delay processing. That is a classic poll-interrupt-block conundrum.
 

10 More Discussions You Might Find Interesting

1. Programming

problem deallocating memory for 3d aray

Can anyone tell me why this section of code causes a segmentation fault when 'deallocate' is called?. Although it works when n>=number. This code segment is intended to be used in a program in which the tensor is very large, (5x512x512) or greater. i tryed to compile this using gcc version... (2 Replies)
Discussion started by: bongobonga
2 Replies

2. Programming

multi-threaded server, pthreads, sleep

I am trying to writa a multi-client & multi-threaded TCP server. There is a thread pool. Each thread in the pool will handle requests of multiple clients. But here I have a problem. I find a solution but it is not how it must be... i think. When threads working without sleep(1) I can't... (0 Replies)
Discussion started by: Parahat Melayev
0 Replies

3. Programming

HOWTO: Calculate the balance of work in multi-threaded app.

I was wondering if anyone could give me a good idea how to calculate how balanced the threading is on a multi-threaded application. I want a percentage, such as "threads are 80% balanced." This is the way I am currently going about it, maybe it is good, maybe not. First, whenever a thread... (2 Replies)
Discussion started by: DreamWarrior
2 Replies

4. AIX

multi threaded program is hanging

I have a Multithreaded program which is hanging on AIX. OS Version: AIX 5.2 and thread library version : 5.2.0.75 We Initiate the process with 50 threads..when we are disconnecting from the process it hangs.There is lots of other stuff involved here.I am just sending the piece of the problem with... (0 Replies)
Discussion started by: hikrishn
0 Replies

5. Programming

Questions about timer in multi-threads environment

Hi I have questions about timer function in multi-threads environment. My application has multi-threads, in one thread, I set a timer, when the timer expires, the timer function will interrupt other thread and execute. I am not clear which thread will be interrupted by the timer function ?... (2 Replies)
Discussion started by: longskys
2 Replies

6. Shell Programming and Scripting

In need of multi threaded perl assistance

I need to write a perl script to execute external programs and grab the output and return code. Each program should be killed if it has not completed within X seconds. Imagine that the script goes something like this : @commands = &get_commands(); foreach $cmd (@commands) { $pid =... (4 Replies)
Discussion started by: SandmanCL
4 Replies

7. UNIX for Advanced & Expert Users

Multi-threaded encryption @ Fedora 11

Hello, are any of the encryption programs capable of true multi-threading ? Friend of mine tells me that he's been running some testing on Fedora 11 and that the kernel doesn't support multi-threading at that level. I've been looking into TrueCrypt, encfs and both calm to support... (0 Replies)
Discussion started by: TehOne
0 Replies

8. Linux

Multi-threaded encryption @ Fedora 11

Hello, are any of the encryption programs capable of true multi-threading ? Friend of mine tells me that he's been running some testing on Fedora 11 and that the kernel doesn't support multi-threading at that level. I've been looking into TrueCrypt, encfs and both calm to support... (1 Reply)
Discussion started by: TehOne
1 Replies

9. Programming

multi-threaded memory leak

Hello All : I write a .c program to test the exactually resource the memory leak as follows: 1 #include <stdio.h> 2 #define NUM 100000 3 void *Thread_Run(void * arg){ 4 //TODO 5 //pthread_datch(pthread_self()); 6 int socket= (int)arg; 7 ... (1 Reply)
Discussion started by: aobai
1 Replies

10. Shell Programming and Scripting

script for multi-threaded bash processes

hey everyone, I'm having some trouble breaking down some code. It's simple a control script that takes machines meant to be backed up from a list. Then according to that will run multi-threaded processes up until the specified thread limit. for example if there are 4 machines to be backed up,... (2 Replies)
Discussion started by: terrell
2 Replies
RUNQUEUE(9)						   BSD Kernel Developer's Manual					       RUNQUEUE(9)

NAME
chooseproc, procrunnable, remrunqueue, setrunqueue -- manage the queue of runnable processes SYNOPSIS
#include <sys/param.h> #include <sys/proc.h> extern struct rq itqueues[]; extern struct rq rtqueues[]; extern struct rq queues[]; extern struct rq idqueues[]; struct thread * choosethread(void); int procrunnable(void); void remrunqueue(struct thread *td); void setrunqueue(struct thread *td); DESCRIPTION
The run queue consists of four priority queues: itqueues for interrupt threads, rtqueues for realtime priority processes, queues for time sharing processes, and idqueues for idle priority processes. Each priority queue consists of an array of NQS queue header structures. Each queue header identifies a list of runnable processes of equal priority. Each queue also has a single word that contains a bit mask identify- ing non-empty queues to assist in selecting a process quickly. These are named itqueuebits, rtqueuebits, queuebits, and idqueuebits. The run queues are protected by the sched_lock mutex. procrunnable() returns zero if there are no runnable processes other than the idle process. If there is at least one runnable process other than the idle process, it will return a non-zero value. Note that the sched_lock mutex does not need to be held when this function is called. There is a small race window where one CPU may place a process on the run queue when there are currently no other runnable processes while another CPU is calling this function. In that case the second CPU will simply travel through the idle loop one additional time before noticing that there is a runnable process. This works because idle CPUs are not halted in SMP systems. If idle CPUs are halted in SMP sys- tems, then this race condition might have more serious repercussions in the losing case, and procrunnable() may have to require that the sched_lock mutex be acquired. choosethread() returns the highest priority runnable thread. If there are no runnable threads, then the idle thread is returned. This func- tion is called by cpu_switch() and cpu_throw() to determine which thread to switch to. choosethread() must be called with the sched_lock mutex held. setrunqueue() adds the thread td to the tail of the appropriate queue in the proper priority queue. The thread must be runnable, i.e. p_stat must be set to SRUN. This function must be called with the sched_lock mutex held. remrunqueue() removes thread td from its run queue. If td is not on a run queue, then the kernel will panic(9). This function must be called with the sched_lock mutex held. SEE ALSO
cpu_switch(9), scheduler(9), sleepqueue(9) BSD
November 3, 2000 BSD
All times are GMT -4. The time now is 08:09 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy