On Thu, 09 Jan 2014 11:58:06 +0100, Sönke Ludwig wrote:

Am 09.01.2014 09:25, schrieb Stanislav:

Hello.
Another question about vibed.

I want to cache some info about authorized users (with singleton). Should i declare that is synchronized functions in singleton? Or vibed make this function blocking?

 struct User {}

 class MySingleton
 {
 public:
   static MySingleton get()
   {
    synchronized
    {
      if (instance_ is null)
      {
        instance_ = new MySingleton;
      }
    }
    return instance_;
   }
   synchronized User getUser() {...}
   synchronized void setUser( User add ) {...}
 private:
   this() {}
   MySingleton instance_;
 }

PS Sorry for bad english.

As long as no I/O, wait or yield operations, are used inside the class
and MySingleton isn't shared between threads (e.g. because
HTTPServerOption.distribute has been enabled), no explicit
synchronization is needed.

Otherwise, you should create a new TaskMutex in MySingleton's
constructor like this: new TaskMutex(this) - this will assign the
mutex as the singleton's monitor. Using the default Mutex on the other
hand can lead to bad performance or deadlocks because it blocks the
thread and thus the event loop.

Another question.
If i get object from REST function like

 void getItem() 
 {
   MySingleton mySingleton;
   mySingleton = mySingleton.get;
   if( mySingleton.getUser.privilegies == 0 ) {}
 }

does it shared between threads? Is REST API functions shared between threads?
If it is - should i write multithread safe functions for REST API?