Menu

Thread safety

2016-06-13
2016-06-15
  • Peter Kootsookos

    Runing Photofilmstrip from the command line on a machine with 12 CPUs, I came across this problem. The method:

    def IncDoneCount(self):
        self.__doneCount += 1
    

    in JobManager.py is not thread safe. As a result, sometimes two threads call the method but only one increment occurs. That leaves the command line hanging, waiting for all threads ot finish. They have, it's just the count is off by one.

    I added:

    def synchronized(lock):
        """ Synchronization decorator """
        def wrap(f):
            def newFunction(*args, **kw):
                with lock:
                    return f(*args, **kw)
            return newFunction
        return wrap
    
    @synchronized(lock)
    def IncDoneCount(self):
        self.__doneCount += 1
    

    from this SO post with an appropriate lock variable defined in the class, which seems to have fixed the problem.

     
  • jensgoe

    jensgoe - 2016-06-15

    Hi Peter,
    thanks a lot for investigating this issue. Actually i experienced your reported problem sometimes but never suspected this method. Now i know incrementing a value is not an atomic operation.
    Thanks again.

     

Log in to post a comment.