general question it can be in c i guess also
if i have ( in my case http requst class ) that invoked from wrapper function
this wrapper function is public API . then inside the wrapper function i init new Request object that suppose to do request with the parameters coming from the wrapper function
do i need to wrap the request object in thread ( i have thread pool class that execute worker threads )
does creating object on the stack for each request will do ?
for example:
public void Wrapper(String a,String b)
{
// im doing ..
MyRequst req = new MyRequest(a,b); // will do the http requst
}
or to do :
public void Wrapper(String a,String b)
{
// im doing ..
MyThreadPool.GetInstance().RunTask(new MyRequest(a,b)); // will do the http request
}
The question isn’t very clear, but from what can be inferred, the pertinent question is whether creating local variables is sufficient for thread-safety. The answer is yes.
From Java Concurrency in Practice:
It should be remembered that all objects are stored on the heap. The items on the stack are primitives and references to objects on the heap, and are termed as local variables and are always one-word wide (except for long and double values); these variables are not to be confused with the concept of method-local variables in the Java programming language (which people incorrectly assume to be stored on the stack).
By using local variables, one ensures that the objects on the heap are accessible only to the current thread of execution, unless of course, any attempt was made to share such objects with other threads (in which case appropriate synchronization techniques needs to be employed).