Sunday, August 1, 2010

Assignment: Case Study 6

We are using a third party image library and we wrapped it up in a clean c++ interface (One Abstrsact class with 15 functions) but during testing found that it is not thread safe and it is used at a lot of places. So my architect suggested that let us spawn an exe for each thread that wants to use it and kill the exe when the thread dies. He suggested that exe can take an XML that describes the operation that I want to invoke and the parameters, with the in and out image path. The problem is that I will have to change my code at lot places to achieve it and I don’t have a lot of time to make this change and test my module. What do you suggest?

8 comments:

  1. Provide another object to control access to it.
    Proxy design pattern.

    ReplyDelete
  2. Identify shared resources.
    We can make implement mutex for such functions to make them thread safe., which makes you write additional code only for this.

    ReplyDelete
  3. I will go with gopi's solution. Create another class derived from the existing class and override all the functions and lock them.

    ReplyDelete
  4. I feel it will be more of a Decorator pattern implementation than just a proxy.

    ReplyDelete
  5. I will go with proxy pattern and wrap in a proxy class with same interface and basically spawn a exe in it for each thread so it can work with diffrent threads simultaneoulsy.

    ReplyDelete
  6. class IImageLib
    {
    virtual LoadImage() = 0;

    };
    class ConcreteImageLib: IImageLib
    {
    LoadImage()
    {
    }

    };
    class ConcreteImageLibProxy:IImageLib
    {
    private:
    ConcreteImageLib *m_wrapImageLib;
    public:
    ConcreteImageLibProxy()
    {
    m_wrapImageLib = new ConcreteImageLibProxy;
    }
    LoadImage()
    {
    Lock()
    m_wrapImageLib->LoadImage();
    Unlock()
    }


    }

    ReplyDelete
  7. Hi Anup,
    Following are the links which can help understand difference between Proxy and Decorator.

    http://powerdream5.wordpress.com/2007/11/17/the-differences-between-decorator-pattern-and-proxy-pattern/

    http://social.msdn.microsoft.com/Forums/en-US/architecturegeneral/thread/e6dae487-96b1-4b86-8351-055623dd2ae8

    ReplyDelete
  8. class imageLib
    {
    public:
    virtual int getRawImage(void *ptr) = 0;
    };
    class jpegLib:public imageLib
    {
    public:
    virtual int getRawImage(void *ptr)
    {return *ptr;}
    };
    class threadSafeJpegLib:public jpegLib
    {
    public:
    virtual int getRawImage(void *ptr)
    {
    MutexScopeLock lock(&obj);
    imageLib::getRawImage(ptr);
    }
    private:
    Mutex obj;
    }

    //Factory Method can be now changed to return object of threadSafeJpegLib instead of jpegLib

    ReplyDelete