There are two main situations in which Synchronization locks are used. These situtatins involved granting exclusive access to blocks of code and exclusive access to variables among threads. The manner in which we have implemented user level locks in S makes the second situation a simple extension of the first.

Consider the example, we we have an object in some database or frame accessed by each of a collection of threads. For example, a parent thread In the parent thread, we evaluate expression


 c = matrix(dim(A)[1]*dim(B)[2])     # the matrix into which the sub-threads write

 k = dim(c)[2]
 threadLock.assign(l,"num.threads",1)

   # create a thread for each column in the answer and have it
   # compute the entries for that column.
 for(i in 1:k) {
   thread(Quote(
           n=length(column)
           tmp = numeric(n)
           for(i in 1:n) 
             tmp[i] = sum(a[i,]*column)

           getLock(lock)
             c[,k] = tmp
             num.threads = num.threads - 1
           yieldLock(lock)
           return(T)
          ),
          data(column=B[,k], k=k,lock=l)   # local variables supplied
                                    # by the parent thread
         )
 }

  getLock(l, condition=Quote(num.threads == 0))
 
We can either store the entries in the column locally and then assign the column vector or we can assign each entry as we calculate it. Note that we have passed all the necessary inputs to the thread from the parent. If we use this approach throughout, and the S internals perform the correct reference counting, the copying of objects is minimal and the objects become local to the thread avoiding the need for locking these as shared objects. The locking in our example is only necessary when we assign the elements of the resulting matrix.

This example has a potentially fatal flaw. Consider the case in which the call to thread() in the loop fails for some reason.

Because the S language is significantly more flexible than many other languages and because of its functional nature, we can modify the code above to have fewer side effects in each thread (namely the assignment of the column entries) and simplify the code.

Compare the following code with the previous version.


 k = dim(B)[2]
 group.lock = threadLock()

   # create a thread for each column in the answer and have it
   # compute the entries for that column.
 for(i in 1:k) {
  th = thread(Quote(
           n=length(column)
           tmp = numeric(n)
           for(i in 1:n) 
             tmp[i] = sum(a[i,]*column)
           return(tmp)
          ),
          data(column=B[,k], k=k,lock=group.lock)   # local variables supplied
                                    # by the parent thread
         )

    threadLock(lock)
     group = addElement(group,th)
     threadLock.assign(lock,"group")  # don't need this unless we want
                                            # somewhere to share the object
                                            # but if the parent thread's frame
                                            # is in the children's search path
                                            # we wouldn't need such an object
    yieldLock(lock)
 }

  values = join(group)  # waits for each thread to return and
                        # returns a list of the return values from each thread 

  if(any(sapply(values,is.null))) {
    stop("Computation failed!")
  }

  c = matrix(dim(A)[1]*dim(B)[2])     # the result 

  for(i in 1:dim(c)[2]) 
    c[,i] = values[[i]]

  return(c)
Note that we need to lock the threadGroup object (group)
Last modified: Sat Feb 15 09:37:05 1997