thread-local

Why do some webservers complain about memory leaks they create?

狂风中的少年 提交于 2019-11-29 14:08:29
问题 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

Is there no way to iterate over or copy all the values of a Java ThreadLocal?

↘锁芯ラ 提交于 2019-11-29 11:07:51
Context: static ThreadLocal<MyType> threadLocalMyType = ... What i'd like is to say something like: for (ThreadLocalEntry e: threadLocalMyType.getMapLikeThing() { // Thread t = e.getKey(); // I don't need the thread value right now, but it might be useful for // something else. MyType theMyType = e.getValue(); // [...do something with theMyType...] } One way would be to handle this manually: use a wrapper of ThreadLocal (extend it) whenever a value is set, keep a ( static ) Map of Threads and values Alternatively, with some reflection ( getDeclaredMethod() and setAccessible(true) ), you can:

Threadlocal counter in Clojure

独自空忆成欢 提交于 2019-11-29 06:57:06
I have a web app where i want to be able to track the number of times a given function is called in a request (i.e. thread). I know that it is possible to do in a non-thread local way with a ref, but how would I go about doing it thread locally? There's a tool for this in useful called thread-local . You can write, for example, (def counter (thread-local (atom 0))) . This will create a global variable which, when deref ed, will yield a fresh atom per thread. So you could read the current value with @@counter , or increment it with (swap! @counter inc) . Of course, you could also get hold of

When is a `thread_local` global variable initialized?

柔情痞子 提交于 2019-11-29 05:38:48
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 it prints Constructor / Destructor 10 times if the user inputs a non zero number. Additionally clang

How to improve performance of SimpleDateFormat wrapped in ThreadLocal?

二次信任 提交于 2019-11-29 00:47:16
问题 This is on Java 7 (51) on RHEL with 24 cores We are noticing a rise in average response times of a java SimpleDateFormat wrapped in thread local as we increase the thread pool size. Is this expected? or, I am just doing something stupid ? Test program public class DateFormatterLoadTest { private static final Logger LOG = Logger.getLogger(DateFormatterLoadTest .class); private final static int CONCURRENCY = 10; public static void main(String[] args) throws Exception { final AtomicLong total =

threadlocal variables in a servlet

橙三吉。 提交于 2019-11-29 00:16:02
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 variable in the further executions of methods called from the servlet in a thread safety way until the request ends the execution Short answer: Yes. A bit longer one: This is how Spring does its magic. See RequestContextHolder (via DocJar). Caution is needed though - you

Pre-initializing a pool of worker threads to reuse connection objects (sockets)

僤鯓⒐⒋嵵緔 提交于 2019-11-28 18:53:43
I need to build a pool of workers in Java where each worker has its own connected socket; when the worker thread runs, it uses the socket but keeps it open to reuse later. We decided on this approach because the overhead associated with creating, connecting, and destroying sockets on an ad-hoc basis required too much overhead, so we need a method by which a pool of workers are pre-initializaed with their socket connection, ready to take on work while keeping the socket resources safe from other threads (sockets are not thread safe), so we need something along these lines... public class

Java ThreadLocal static?

*爱你&永不变心* 提交于 2019-11-28 17:10:09
问题 Setting a value in Thread Local: //Class A holds the static ThreadLocal variable. Class A{ public static ThreadLocal<X> myThreadLocal = new ThreadLocal<X>(); .... } //A Class B method sets value in A's static ThreadLocal variable class B{ { public void someBmethod(){ X x = new X(); A.myThreadLocal.set(x); } } //Class C retrieves the value set in A's Thread Local variable. Class C { public void someCMethod(){ X x = A.myThreadLocal.get(); } ... } Quesiton : Now assuming this is a web

Are C++ exceptions sufficient to implement thread-local storage?

瘦欲@ 提交于 2019-11-28 07:18:49
I was commenting on an answer that thread-local storage is nice and recalled another informative discussion about exceptions where I supposed The only special thing about the execution environment within the throw block is that the exception object is referenced by rethrow. Putting two and two together, wouldn't executing an entire thread inside a function-catch-block of its main function imbue it with thread-local storage? It seems to work fine, albeit slowly. Is this novel or well-characterized? Is there another way of solving the problem? Was my initial premise correct? What kind of

Multiple independent embedded Python Interpreters on multiple operating system threads invoked from C/C++ program

陌路散爱 提交于 2019-11-28 06:52:00
Embedding Python interpreter in a C/C++ application is well documented . What is the best approach to run multiple python interpreter on multiple operating system threads (i.e. one interpreter on one operating system thread within the same process) which are invoked from the C/C++ application? Such applications may also have problems related to memory fragmentation and limitations of Py_Finalize() . One such approach can be the following: Python thread and hence GIL disabled in pyconfig.h to keep it simple (#undef WITH_THREAD) All mutable global variables of Python Interpreter source code