thread-local

What is "thread local storage" in Python, and why do I need it?

In Python specifically, how do variables get shared between threads? Although I have used threading.Thread before I never really understood or saw examples of how variables got shared. Are they shared between the main thread and the children or only among the children? When would I need to use thread local storage to avoid this sharing...

What are best practices for using thread local storage in .NET?

I have a requirement in my application that I think can be met by using thread local storage, but I'm wondering if it's one of those things that's best to avoid. I have read a few articles on the subject: http://www.dotnetcoders.com/web/Articles/ShowArticle.aspx?article=58 http://msdn.microsoft.com/en-us/library/system.threadstaticatt...

threadlocal variables in a servlet

Are the threadlocals variables global to all the requests made to the servlet that owns the variables? I am using resin for the server. Thanks for awnser. I think I can make my self more clear. The specific Case: I want to: initialize a static variable when the request starts the execution. be able to query the value of the vari...

Is thread-local storage persisted between backgroundworker invocations?

Are backgroundworker threads re-used? Specifically, if I set a named data slot (thread-local storage) during the DoWork() method of a backgroundworker, will the value of that data slot persist, potentially to be found be some other thread at a later time? I wouldn't have thought so, but I have this bug... EDIT: This blog post suggests...

Threads in Spring

I have a Web application using spring and hibernate and struts (it runs on Tomcat) The call sequence is something like this... Struts action calls spring service bean which in turn calls Spring DAO bean. The DAO implementation is a Hibernate implementation. The question is Would all my spring beans be running in the same thread ? Can ...

Why would shl_load() fail for libraries with Thread Local Storage?

Threads in Perl by default take their own local storage for all variables, to minimise the impact of threads on existing non-thread-aware code. In Perl, a thread-shared variable can be created using an attribute: use threads; use threads::shared; my $localvar; my $sharedvar :shared; HP-UX runtime loader does not support dynamic load ...

ThreadLocal; isn't it the same as creating a copy of the variable each time?

Hi, I am still confused of the concept of ThreadLocal. I have read the JavaDoc, and other related questions posted around here, but the jargons used and all didn't help me much. I kind of get the idea of ThreadLocal, that is, each thread has its own copy of the variable. So...how does this make it different from say...constructing th...

ThreadLocal Resource Leak and WeakReference

My limited understanding of ThreadLocal is that it has resource leak issues. I gather this problem can be remedied through proper use of WeakReferences with ThreadLocal (although I may have misunderstood this point.) I would simply like a pattern or example for correctly using ThreadLocal with WeakReference, if one exists. For instance,...

ThreadLocal + java.sql.Connection + servlet filter = 2009?

I am writing some servlets with plain old mostly-JDBC patterns. I realized that I have several objects that would like to share a single transaction, and I'd like to enforce that one HTTP transaction = one database transaction. I think I can do this via passing a Connection around in a ThreadLocal variable, and then having a servlet fil...

How to put variables on the stack/context in Python

In essence, I want to put a variable on the stack, that will be reachable by all calls below that part on the stack until the block exits. In Java I would solve this using a static thread local with support methods, that then could be accessed from methods. Typical example: you get a request, and open a database connection. Until the re...

How to override ObjectOutputStream.writeStreamHeader()?

The method ObjectOutputStream.writeStreamHeader() can be overridden to prepend or append data to the header. However, if that data is based on an argument passed to the derived class's constructor like: public class MyObjectOutputStream extends ObjectOutputStream { public MyObjectOutputStream( int myData, OutputStream out ) throws...

How is Java's ThreadLocal implemented under the hood?

How is ThreadLocal implemented? Is it implemented in Java (using some concurrent map from ThreadID to object), or does it use some JVM hook to do it more efficiently? ...

Thread local storage in Python

How do I use thread local storage in Python? Related What is “thread local storage” in Python, and why do I need it? - This thread appears to be focused more on when variables are shared. Efficient way to determine whether a particular function is on the stack in Python - Alex Martelli gives a nice solution ...

On which platforms is thread local storage limited and how much is available?

I was recently made aware that thread local storage is limited on some platforms. For example, the docs for the C++ library boost::thread read: "Note: There is an implementation specific limit to the number of thread specific storage objects that can be created, and this limit may be small." I've been searching to try and find out the ...

Synchronized and local copies of variables

I'm looking at some legacy code which has the following idiom: Map<String, Boolean> myMap = someGlobalInstance.getMap(); synchronized (myMap) { item = myMap.get(myKey); } The warning I get from Intelli-J's code inspections is: Synchronization on local variable 'myMap' Is this the appropriate synchronization and why? Map<String...

How to force a Java thread to close a thread-local database connection

When Using a thread-local database connection, closure of the connection is required when the thread exists. This I can only do if I can override the run() method of the calling thread. Even that is not a great solution, since at time of exit, I don't know if a connection has ever been opened by that thread. The problem is in fact more...

Why is java.lang.ThreadLocal a map on Thread instead on the ThreadLocal?

Naively, I expected a ThreadLocal to be some kind of WeakHashMap of Thread to the value type. So I was a little puzzled when I learned that the values of a ThreadLocal is actually saved in a map in the Thread. Why was it done that way? I would expect that the resource leaks associated with ThreadLocal would not be there if the values are...

any harm in put django request object in thread local dict?

I have some custom render_to_response methods which now needs request object, I could pass request to each one of them but instead I am saving request object in a middleware to thread local space and accessing it elsewhere, can it affect me anyway? ...

Thread-local, class instance-local storage?

Is there a good, platform-agnostic way to implement a variable that's local to both a thread and a class instance, i.e. if you have T threads and I class instances, you have TxI instances of that variable? I'm using the D programming language, version 2, but a good language-agnostic answer would also be useful. Here are some constraint...

What are the advantages of instance-level thread-local storage?

This question led me to wonder about thread-local storage in high-level development frameworks like Java and .NET. Java has a ThreadLocal<T> class (and perhaps other constructs), while .NET has data slots, and soon a ThreadLocal<T> class of its own. (It also has the ThreadStaticAttribute, but I'm particularly interested in thread-local ...