thread-local

Why do some webservers complain about memory leaks they create?

扶醉桌前 提交于 2019-11-30 09:37:39
The title might be a bit strong, but let me explain how I understand what happens. I guess this happened with Tomcat (and the message cited comes from Tomcat), but I'm not sure anymore. TL;DR At the bottom there's a summary why I'm claiming that it is the web servers' fault. I might be wrong (but without the possibility of being wrong there would be no reason to ask): An application uses a library the library uses a ThreadLocal the ThreadLocal refers to an object from the library each object refers to its ClassLoader The webserver pools its worker threads for efficiency lends an arbitrary

ThreadLocal Resource Leak and WeakReference

泪湿孤枕 提交于 2019-11-30 08:38:01
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, in this code snippet where would the WeakReference be introduced? static class DateTimeFormatter { private static final ThreadLocal<SimpleDateFormat> DATE_PARSER_THREAD_LOCAL = new ThreadLocal<SimpleDateFormat>() { protected SimpleDateFormat initialValue() {

Can you use thread local variables inside a class or structure

五迷三道 提交于 2019-11-30 08:20:53
Like this. struct some_struct { // Other fields ..... __thread int tl; } I'm trying to do that but the compiler is giving me this error. ./cv.h:16:2: error: '__thread' is only allowed on variable declarations __thread int tl; Thread-local storage applies to static variables only. There is no point in making non-static structure or class members thread-local. Local (automatic) variables are always specific to the thread that executes the code, but global and static variables are shared among threads since they reside in the data or BSS segment. TLS provides a mechanism to make those global

When is a `thread_local` global variable initialized?

妖精的绣舞 提交于 2019-11-30 08:02:40
问题 Consider the following example (lock guards on cout omitted for simplicity). #include <future> #include <iostream> #include <thread> using namespace std; struct C { C() { cout << "C constructor\n";} ~C() { cout << "C destructor\n";} }; thread_local C foo; int main() { int select; cin >> select; future<void> f[10]; for ( int i = 0;i < 10; ++i) f[i] = async( launch::async,[&](){ if (select) foo; } ); return 0; } On both clang and gcc, this program outputs nothing if the user writes '0', while

Java Cached thread pool and thread local

回眸只為那壹抹淺笑 提交于 2019-11-30 07:30:32
问题 I have a question about java and concurrency. Let say I have a ThreadLocal variable called 'a'. And I use a CachedThreadPool to obtain new threads. When a thread is reused, what happens to the ThreadLocal variable 'a'? It maintains the same value (cause it is the same thread) or it starts empty (as if the thread was new)? 回答1: By default ThreadLocals are reused along with the thread. If you need them to be be reinitialized you can do so by overriding the methods noted below: from javadoc for

ThreadLocal usage in enterprise application

强颜欢笑 提交于 2019-11-30 06:50:40
If my web application and ejb application are on the same machine (on same JVM) and all the ejb calls are local calls , will the use of ThreadLocal create any issue while passing information from web to ejb? Any workaround if the ejb calls are remote? Will ThreadLocal information be available from web application to ejb application? Is use of ThreadLocal advisable in such scenario? For the first question, there is no problem as long as you remove the ThreadLocal variables at the end of every call. This is important because containers (servlet or ejb) typically use threadpools and therefore

ThreadLocal HashMap vs ConcurrentHashMap for thread-safe unbound caches

巧了我就是萌 提交于 2019-11-30 03:45:43
问题 I'm creating a memoization cache with the following characteristics: a cache miss will result in computing and storing an entry this computation is very expensive this computation is idempotent unbounded (entries never removed) since: the inputs would result in at most 500 entries each stored entry is very small cache is relatively shorted-lived (typically less than an hour) overall, memory usage isn't an issue there will be thousands of reads - over the cache's lifetime, I expect 99.9%+

When we should use scala.util.DynamicVariable?

混江龙づ霸主 提交于 2019-11-29 20:16:21
When I read the source of scalatra, I found there are some code like: protected val _response = new DynamicVariable[HttpServletResponse](null) protected val _request = new DynamicVariable[HttpServletRequest](null) There is an interesting class named DynamicVariable . I've looked at the doc of this class, but I don't know when and why we should use it? It has a withValue() which is usually be used. If we don't use it, then what code we should use instead, to solve the problem it solved? (I'm new to scala, if you can provide some code, that will be great) Vasil Remeniuk DynamicVariable is an

How does pthread_key_t and the method pthread_key_create work?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 19:29:18
问题 I am having some trouble figuring out how pthread_key_t and pthread_key_create work. From my understand, each thread has TLS (thread local storage) and that a key is used to access the thread local storage. What I do not get is when a key is created, does every thread get to use it? Lets say Thread 0 creates key 0, can Thread 1 then use key 0? If Thread 1 used key 0, would it access its own TLS or Thread 0's TLS? Is there some global array or something that keeps track of all the keys being

InheritableThreadLocal value not inherited by ExecutorService threads

拜拜、爱过 提交于 2019-11-29 15:42:51
问题 import java.util.concurrent.Executors import scala.concurrent.{ExecutionContext, Future} object TestInheritableThreadLocal { def main(args: Array[String]): Unit = { implicit val ec = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(2)) val tl: InheritableThreadLocal[String] = new InheritableThreadLocal[String]() tl.set("InitialValue") Future { println("111 " + Thread.currentThread() + tl.get()) Future { println("222 " + Thread.currentThread() + tl.get()) } } Thread.sleep(3000)