Hi All,

Synchronization sounds familiar but not Gosu. Isn’t it?

I know a lot of people still unaware about Gosu programming language. So let me give you a brief idea what’s Gosu programming language is. Gosu has been developed on top of Java. So it runs on JVM. The features of Gosu has been incorporated mainly from Java and C#. There are other languages too from which small or big features has been incorporated. It has got dynamic type checking.

If you want to read/learn more about Gosu, please check it’s official website – http://gosu-lang.github.io/ or check the wiki to get a general idea – http://en.wikipedia.org/wiki/Gosu_(programming_language)

Now, back to the topic. We must have came through a scenario where only one thread is supposed to utilize a particular resource at a given point of time i.e. making the code thread safe. In that scenario we use the keyword “synchronize”, for example in Java. So, Synchronization is:

Locking access between threads to shared resources

such as static variables.

Implementation: The simplest form is to define a static variable for a lock in your class definition. Then, define a function that performs the task you must synchronize putting all the content of method inside using clause.

// in your class variable definitions...
var _lock : ReentrantLock = new ReentrantLock()

function useReentrantLock() {
        using( _lock ) {
                // do your main work here
        }
}

The using statement automatically cleans up the lock, even if there code throws exceptions. So, above code is same as:

uses java.util.concurrent

static var _lock = new ReentrantLock()

function useReentrantLock() {
            _lock.lock()
             try {
                        //do something
             } finally {
                       _lock.unlock()
             }
}

Re-entrant objects are objects that help manage safe access to data that is shared by re-entrant or concurrent code execution. For example, if you must store data that is shared by multiple threads, ensure that you protect against concurrent access from multiple threads to prevent data corruption. The most prominent type of shared data is class static variables, which are variables that are stored on the Gosu class itself.

For Gosu to recognize a valid reentrant object, the object must have one of the following attributes:

  • Implements the java.util.concurrent.locks.Lock interface. This includes the Java classes in that
    package: ReentrantLock, ReadWriteLock, Condition.
  • You cast the object to the Gosu interface IMonitorLock . You can cast any arbitrary object to IMonitorLock. It is useful to cast Java monitor locks to this Gosu interface.
  • Implements the Gosu class gw.lang.IReentrant . This interface contains two methods with no arguments: enter and exit. Your code must properly lock or synchronize data access as appropriate during the enter method and release any locks in the exit method.

 

So, that’s it.

Hope this article will help you.

Critics/feedbacks are welcome.

Have a nice day ahead.

(Courtesy: Gosu Reference Guide)

Loading