| Online Eiffel Documentation

EiffelStudio

# Once features in multithreaded mode

## Manipulating Once features in multithreaded mode

Eiffel introduced the powerful mechanism of once routines. A once routine has a body that will be executed only once, at the first call. Subsequent calls will have no further effect and, in the case of a function, will return the same result as the first. This provides a simple way of sharing objects in an object-oriented context.

For multithreaded applications, the appropriate semantics is that once routines must be called once per thread (rather than once per process). This is the semantics supported by EiffelThread.

Then the once feature is not initialized once per process but once per thread. Your once feature will be called again in any new thread execution.

## Once per Process/Thread

Current once features in Eiffel are once per thread. This means that when a once feature is called in a thread, the Eiffel run-time will check whether it has been already computed in this thread. If not, the once feature will be initialized and computed. This seems to be a relevant way of managing once features in multithreaded mode: most of the time, a once called in a thread is not likely to share its result.

However, in some case, we need to share once features.

Moreover, an Eiffel programmer should be able to have an alternative between a once per thread or per process implementation.

## Using Once per process/thread features in EIFFEL

Here is what you will do to implement a once per process feature:

    class
    		TEST_ONCE_PER_PROCESS

    	feature

    		object_per_thread: OBJECT is
    				-- Once per thread.
    			once
    				create Result.make
    			end

    		object_per_process: OBJECT is
    				-- New 'object' (once per process)
    				-- that could be shared between threads
    				-- without reinitializing it.
    			indexing
    				once_status: global
    			once
    				create Result.make
    			end

    	end -- class TEST_ONCE_PER_PROCESS

You can do the same with once procedures.