Erlang - Global variables .. yes, I know, I know

Well, I kept trying to figure it out.

I need this table to be global. I realized that it is much less efficient to pass the TableID around ... as part of my program.

So, I tried to create a new table and then look at it:

TableID = ets:new(tb, [set,public]), put({tableUniqueID}, TableID), 

Then I used:

 get({tableUniqueID}) 

And in the same function, it returns the TableID just fine ... but when I use it in another function, it returns undefined.

What?? I thought that to receive and deliver a key global.

ALSO before that, I realized that you "can" call the table search function as such:

 ets:lookup(get({tableUniqueID}), msgIn) 

The same thing works in functions, but not on the outside .. Get Put problem ..

Then I realized that another way to search for a table would be by calling the table atom

 ets:lookup(tb, msgIn) 

But this NEVER works, not inside a function, not from.

So, my main priority would be to understand why it is impossible to find a table with your atom. But it says it is everywhere, including leadership.

To get / put I could live without it, while I can store the table, and then search for the table by its atom identifier.

Can anyone shed light on this dilemma?

+6
erlang global
source share
3 answers

I GOT IT!!

Wish documents, say it under the search function. Even better, everyone who writes textbooks on ets, or, what's more, books

Decision -

 TableID = ets:new(tb, [set,public,named_table]) 

This named_table is an important part.

Some are digging man pages, but

;)

+6
source share

The correct answer to your problem is not to use the global table at all, but rather to transmit information. Especially since you mention efficiency in your original question. You create an overload point in your code that will make it worse on any multi-core machine.

The ets table is implemented as a process that all other processes must call to get the value.

+5
source share

Starting with Erlang 21.2, a new read-optimized version of the implementation of the "global" has appeared. Take a look at the persistent_term module

 persistent_term:put(Key, Value) 

This works great for global flags that are written only once, for example, only at startup, but read many times in different processes. All processes have quick read access to these global variables:

 Value = persistent_term:get(Key) 
0
source share

All Articles