Sponsored Content
Top Forums Programming Deallocating memory in multi-threaded environment. Post 302518393 by DGPickett on Friday 29th of April 2011 01:28:45 PM
Old 04-29-2011
Yes, where did you fall off my wagon?

The big answer is no, this cannot work. You can only dealloc when you are sure all copies of the pointer, or any derived pointer, are out of use, forever. Threads need to use the pointer to see if it is locked.
 

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
ggLockCreate(3) 							GGI							   ggLockCreate(3)

NAME
ggLockCreate, ggLockDestroy, ggLock, ggUnlock, ggTryLock - Lowest common denominator locking facilities SYNOPSIS
#include <ggi/gg.h> void *ggLockCreate(void); int ggLockDestroy(void *lock); void ggLock(void *lock); void ggUnlock(void *lock); int ggTryLock(void *lock); DESCRIPTION
These functions allow sensitive resource protection to prevent simultaneous or interleaved access to resources. For developers accustomed to POSIX-like threading environments it is important to differentiate a gglock from a "mutex". A gglock fills *both* the role of a "mutex" and a "condition" (a.k.a. an "event" or "waitqueue") through a simplified API, and as such there is no such thing as a gglock "owner". A LibGG lock is just locked or unlocked, it does not matter by what or when as long as the application takes care never to create a deadlock that never gets broken. The locking mechanisms are fully functional even in single-threaded, uninterrupted-flow-of-control environments. They must still be used as described below even in these environments; They are never reduced to non-operations. The locking mechanisms are threadsafe, and are also safe to call from inside LibGG task handlers. However, they are not safe to use in a thread that may be cancelled during their execution, and they are not guaranteed to be safe to use in any special context other than a LibGG task, such as a signal handler or asyncronous procedure call. Though the LibGG API does provide ample functionality for threaded environments, do note that LibGG does not itself define any sort of threading support, and does not require or guarantee that threads are available. As such, if the aim of an application developer is to remain as portable as possible, they should keep in mind that when coding for both environments, there are only two situations where locks are appropriate to use. These two situations are described in the examples below. Cleanup handlers created with ggRegisterCleanup(3) should not call any of these functions. LibGG must be compiled with threading support if multiple threads that call any of these functions are to be used in the program. When LibGG is compiled with threading support, the ggLock, ggUnlock, and ggTryLock functions are guaranteed memory barriers for the purpose of multiprocessor data access synchronization. (When LibGG is not compiled with threading support, it does not matter, since separate threads should not be using these functions in the first place.) ggLockCreate creates a new lock. The new lock is initially unlocked. ggLockDestroy destroys a lock, and should only be called when lock is unlocked, otherwise the results are undefined and probably undesir- able. ggLock will lock the lock and return immediately, but only if the lock is unlocked. If the lock is locked, ggLock will not return until the lock gets unlocked by a later call to ggUnlock. In either case lock will be locked when ggLock returns. ggLock is "atomic," such that only one waiting call to ggLock will return (or one call to ggTryLock will return successfully) each time lock is unlocked. Order is *not* guaranteed by LibGG -- if two calls to ggLock are made at different times on the same lock, either one may return when the lock is unlocked regardless of which call was made first. (It is even possible for a call to ggTryLock to grab the lock right after it is unlocked, even though a call to ggLock was already waiting on the lock.) ggTryLock attempts to lock the lock, but unlike ggLock it always returns immediately whether or not the lock was locked to begin with. The return value indicates whether the lock was locked at the time ggTryLock was invoked. In either case lock will be locked when ggTryLock returns. ggUnlock unlocks the lock. If any calls to ggLock or ggTryLock are subsequently invoked, or have previously been invoked on the lock, one of the calls will lock lock and return. As noted above, which ggLock call returns is not specified by LibGG and any observed behavior should not be relied upon. Immediacy is also *not* guaranteed; a waiting call to ggLock may take some time to return. ggUnlock may be called, successfully, even if lock is already unlocked, in which case, nothing will happen (other than a memory barrier.) In all the above functions, where required, the lock parameter *must* be a valid lock, or the results are undefined, may contradict what is written here, and, in general, bad and unexpected things might happen to you and your entire extended family. The functions do *not* vali- date the lock; It is the responsibility of the calling code to ensure it is valid before it is used. Remember, locking is a complicated issue (at least, when coding for multiple environments) and should be a last resort. RETURN VALUE
ggLockCreate returns a non-NULL opaque pointer to a mutex, hiding its internal implementation. On failure, ggLockCreate returns NULL. ggTryLock returns GGI_OK if the lock was unlocked, or GGI_EBUSY if the lock was already locked. ggLockDestroy returns GGI_OK on success or GGI_EBUSY if the lock is locked. EXAMPLES
One use of gglocks is to protect a critical section, for example access to a global variable, such that the critical section is never entered by more than one thread when a function is called in a multi-threaded environment. It is important for developers working in a single-threaded environment to consider the needs of multi-threaded environments when they provide a function for use by others. static int foo = 0; static gglock *l; void increment_foo(void) { ggLock(l); foo++; ggUnlock(l); } In the above example, it is assumed that gglock is initialized using ggLockCreate before any calls to increment_foo are made. Also note that in the above example, when writing for maximum portability, increment_foo should not be called directly or indirectly by a task han- dler which was registered via ggAddTask because a deadlock may result (unless it is somehow known that increment_foo is not being executed by any code outside the task handler.) Another use of gglocks is to delay or skip execution of a task handler registered with ggAddTask(3). It is important for developers work- ing in a multi-threaded environment to consider this when they use tasks, because in single-threaded environments tasks interrupt the flow of control and may in fact themselves be immune to interruption. As such they cannot wait for a locked lock to become unlocked -- that would create a deadlock. static gglock *t, *l, *s; int misscnt = 0; void do_foo (void) { ggLock(t); /* prevent reentry */ ggLock(l); /* keep task out */ do_something(); ggUnlock(l); /* task OK to run again */ if (!ggTryLock(s)) { /* run task if it was missed */ if (misscnt) while (misscnt--) do_something_else(); ggUnlock(s); } ggUnlock(t); /* end of critical section */ } /* This is called at intervals by the LibGG scheduler */ static int task_handler(struct gg_task *task) { int do_one; /* We know the main application never locks s and l at the * same time. We also know it never locks either of the * two more than once (e.g. from more than one thread.) */ if (!ggTryLock(s)) { /* Tell the main application to run our code for us * in case we get locked out and cannot run it ourselves. */ misscnt++; ggUnlock(s); if (ggTryLock(l)) return; /* We got locked out. */ } else { /* The main application is currently running old missed * tasks. But it is using misscnt, so we can't just ask * it to do one more. * * If this is a threaded environment, we may spin here for * while in the rare case that the main application * unlocked s and locked l between the above ggTryLock(s) * and the below ggLock(l). However we will get control * back eventually. * * In a non-threaded environment, the below ggLock cannot * wedge, because the main application is stuck inside the * section where s is locked, so we know l is unlocked. */ ggLock(l); do_something_else(); ggUnlock(l); return; } /* now we know it is safe to run do_something_else() as * do_something() cannot be run until we unlock l. * However, in threaded environments, the main application may * have just started running do_something_else() for us already. * If so, we are done, since we already incremented misscnt. * Otherwise we must run it ourselves, and decrement misscnt * so it won't get run an extra time when we unlock s. */ if (ggTryLock(s)) return; if (misscnt) while (misscnt--) do_something_else(); ggUnlock(s); ggUnlock(l); } In the above example, the lock t prevents reentry into the dofoo subroutine the same as the last example. The lock l prevents do_some- thing_else() from being called while do_something() is running. The lock s is being used to protect the misscnt variable and also acts as a memory barrier to guarantee that the value seen in misscnt is up-to-date. The code in function dofoo will run do_something_else() after do_something() if the task happened while do_something() was running. The above code will work in multi-threaded-single-processor, multi- threaded-multi-processor, and single-threaded environments. Note: The above code assumes do_something_else() is reentrant. SEE ALSO
pthread_mutex_init(3) libgg-1.0.x 2005-08-26 ggLockCreate(3)
All times are GMT -4. The time now is 03:11 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy