Is there a template that can be used to create threads when we program in OO language ?
How to go about designing a threading package for an OO language?
C++0x will support threads in the standard library.
As of now, each platform has its own way of implementing threads (Windows, POSIX) but you can use something such as boost::thread to not have to worry about platform-specific stuff.
In Java, there is a Thread class.
In general, to put a class into another thread, you will create a thread while passing that class into the thread. Then the thread will call a function in that class. Here is some pseudo-C++-code:
main()
{
Object myObject;
thread = CreateThread(threadFunction, myObject);
thread.join(); // wait for thread
}
threadFunction(Object theObject)
{
theObject.doSomething();
}
This is all simplified by the use of boost (or C++0x threads) in C++, and the Thread class in Java handles this for you.
A large problem in threaded applications is synchronization of threads. This includes problems like race conditions and deadlocks, to name a couple.
Methods/object exist to help these problems, such as a mutex. A mutex can be locked by one thread, and any other threads that try to lock the mutex will be blocked until the original thread releases the mutex.
A semaphore is a generalized mutex.
There are other useful concepts as outlined in Eric's post.