<div dir="ltr">Roberto,<div><br></div><div>I use global for a very similar use case. I have a distributed, in-memory cache that uses Mnesia. The first node of the cluster creates the schema, subsequent nodes add themselves to the alrady created cluster. Thus I have need to make sure that the test for being the first node and the cluster creation is protected.</div><div><br></div><div>I use global:trans/4. </div><div>The first parameter is an id = {resource, PID of lock requestor}</div><div>The second is a function which is executed should the lock be acquired</div><div>The third parameter indicates which nodes the lock should range over</div><div>The fourth indicates how long to wait for the lock.</div><div><br></div><div>My code pings all configured node names to try join the mesh of participating nodes, then it synchronizes with the global name server across all nodes in the cluster, then locks the 'resource' jc_mnesia' and, assuming the lock is acquired, executes the function. Any other node that tries to execute this same code will block until the first node is out of the function.</div><div><br></div><div>Feel free to look at my code (<a href="https://github.com/jr0senblum/jc">https://github.com/jr0senblum/jc</a>) or email me with any questions. I *think* this works... it's been in production for a while... so... there's that....</div><div><br></div><div>    ... snip...</div><div><div>    [Node || Node <- Nodes, pong == net_adm:ping(Node) ],</div></div><div><div>    global:sync(),</div><div>    global:trans({jc_mnesia, self()}, </div><div><span class="" style="white-space:pre">            </span> fun() -></div><div><span class="" style="white-space:pre">                       </span> mnesia:start(),</div><div><span class="" style="white-space:pre">                   </span> case [Node || Node <- nodes(), jc_loaded(Node)] of</div><div><span class="" style="white-space:pre">                     </span>     [] -> % No nodes have jc service, this one is first</div><div><span class="" style="white-space:pre">                              </span> mnesia:create_schema([]),</div><div><span class="" style="white-space:pre">                         </span> dynamic_db_init([]),</div><div><span class="" style="white-space:pre">                              </span> Indexes = application:get_env(jc, indexes, []),</div><div><span class="" style="white-space:pre">                           </span> [jc_store:start_indexing(Map, Path) || </div><div><span class="" style="white-space:pre">                          </span>     {Map, Path} <- Indexes];</div><div><span class="" style="white-space:pre">                 </span>     Ns -> % Not the first node up, join existing cluster</div><div><span class="" style="white-space:pre">                             </span> dynamic_db_init(Ns)</div><div><span class="" style="white-space:pre">                       </span> end,</div><div><span class="" style="white-space:pre">                      </span> true = global:del_lock({jc_mnesia, self()}), </div><div><span class="" style="white-space:pre">                    </span> ok</div><div><span class="" style="white-space:pre">                </span> end,</div><div><span class="" style="white-space:pre">              </span> [node() | nodes()], </div><div><span class="" style="white-space:pre">             </span> infinity).</div></div><div><br></div><div><br></div><div><br><span style="font-size:12.8px">Dear list,</span><br style="font-size:12.8px"><span style="font-size:12.8px">I'm trying to get an understanding of what global:set_lock/1,2,3 exactly</span><br style="font-size:12.8px"><span style="font-size:12.8px">does.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">I read from the docs:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Sets a lock on the specified nodes (or on all nodes if none are specified)</span><br style="font-size:12.8px"><span style="font-size:12.8px">on ResourceId for LockRequesterId.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Let's say that I want to perform a series of operations on mnesia schemas</span><br style="font-size:12.8px"><span style="font-size:12.8px">and want to avoid all other nodes accessing mnesia tables while one node is</span><br style="font-size:12.8px"><span style="font-size:12.8px">busy at it.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Is it enough to write:</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">global:trans({{?MODULE, lock_mnesia_for_a_while}, self()},</span><br style="font-size:12.8px"><span style="font-size:12.8px">    fun() -></span><br style="font-size:12.8px"><span style="font-size:12.8px">        do_things_on_mnesia()</span><br style="font-size:12.8px"><span style="font-size:12.8px">    end).</span><br style="font-size:12.8px"><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">I don't get how this could lock mnesia for the other nodes.</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Can some kind soul point me in the right direction?</span><br style="font-size:12.8px"><br style="font-size:12.8px"><span style="font-size:12.8px">Thank you,</span><br style="font-size:12.8px"><span style="font-size:12.8px">r.</span><br style="font-size:12.8px"><div><span style="font-size:12.8px"><br></span></div><div><span style="font-size:12.8px">Robert</span></div><div><span style="font-size:12.8px"><br></span></div></div></div>