Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common patterns to work around the limitations of thread-local gc?

Tags:

In my process of learning Nim, I'm currently studying Nim's approaches to concurrent programming. I have seen a few comments about the limitations of a thread-local garbage collection (for instance here and there), but I still don't fully see all implications of this.

I'm wondering if there are some kind of well-established "design patterns" in Nim how to deal with these limitations? Maybe it is possible to consider a typical example that requires inter-thread sharing/interaction and to show possible idiomatic solutions to such a problem?

My own attempts to come up with good solutions to this were not really successful so far and have lead to this more specific question about TChannel.

like image 859
bluenote10 Avatar asked Apr 29 '15 18:04

bluenote10


People also ask

When should I use ThreadLocal?

Most common use of thread local is when you have some object that is not thread-safe, but you want to avoid synchronizing access to that object using synchronized keyword/block. Instead, give each thread its own instance of the object to work with.

What is the ThreadLocal class how and why would you use it?

The ThreadLocal class is used to create thread local variables which can only be read and written by the same thread. For example, if two threads are accessing code having reference to same threadLocal variable then each thread will not see any modification to threadLocal variable done by other thread.

Which class can be used to store thread specific values?

Java ThreadLocal class provides thread-local variables. It enables you to create variables that can only be read and write by the same thread.

Is ThreadLocal thread-safe?

It's safe because getMap returns the map for the given (i.e. current) thread.


1 Answers

A really broad answer is: This seems to go against the design principles of Nim. Nim completely avoids any inter-thread sharing/interaction problems by removing that as a capability. The no "stop the world" design principle. The built in asynchronous message passing should be adequate to.

What limitations are mentioned in the referenced articles seem to be limitations in design or approach to the problems.

Also in the time passed since you posted this question I see that there has been an answer to the linked TChannel question. Has this resolved your issue?

@bluenote10 Is it possible you can better define the type of problem you are facing? The way it seems now this question is really broad. Also sorry I don't have the rep to reply directly to you.

like image 84
Adam Brockway Avatar answered Sep 18 '22 15:09

Adam Brockway