views:

576

answers:

7

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?

+2  A: 

Each framework has its own way of dealing with threads. I suggest you look up the java thread class in the the java documentation, and then perhaps look at the C++ standard headers for thread details.

Scott M.
+2  A: 

Support

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.

Methods

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.

Related Information

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.

GMan
+1  A: 

Each programming language has specific ways of handling threads.

C++ relies a lot on Boost so you might wanna check that

Java

Basically everything you learn about concurrency should apply whatever the OS or language you're using. There are roughly 4 problems you must learn to avoid

  • Deadlocks
  • Livelocks
  • Race Conditions
  • Hunger

This isn't directly related to your questions but these are subjects you should learn in parallel to learning a particular syntax in a given language. Java is of course quite easy while C++ might be a little trickier, your pick

There are also a number of "well known methods" for synchronizing threads such as

  • Events
  • Locks
  • Monitor
  • Mutex
  • Semaphore
  • Barrier
  • ...

this list goes on and on, but are basically helping "objects" or variables that will help you solve the 4 problems mentionned

Eric
+1  A: 

In C/C++, threads are often implemented using functions. ("Start a new thread and run this function inside, destroys the thread when the function ends.")

Thread frameworks often allow to pass parameters to the function. One way of doing OO with threads is to pass the object pointer ("this") as the function parameters and then call a specific method on the object.

Vincent Robert
A: 

If you are using MFC then MFC provides some framework helper APIs to work with multi threading. Have a look at this article: Multithreading with C++ and MFC

aJ
A: 

In Java you can use the Thread class or the Executor interface, to which you can send a runnable object. Check out the java.util.concurrent package. And the concurrency trail of the Java Tutorial. Good Luck.

jasonco
A: 

The main difference when it comes to OOP and multi-threading programming is that you would like your new thread to have as its start-up function a (non-static) method of an object.

Now there are a few programming models for this: implement a "Runnable" interface is the most common used one. Your class implements a standard "Runnable" interface that has a start-up method which is usually a virtual method called "run". You put your start-up code in that virtual method and when you need to spawn a thread you create a thread object (usually called Thread) and you pass to it an object of your class that implements the "Runnable" interface. Then you call the "start" method of that "Thread" object and that will take care that the start-up code you have written in your class will be run a a new thread. In this example both "Runnable" and "Thread" are provided by the chosen threading library or by the standard library/framework of the programming language of your choice. Also please note that the names of these two classes can vary.

There is a big difference between C++ and Java when it comes to multi-threading programming: C++ has not support for multi-threading as opposed to Java which has built-in support for that.

So for C++ you will need to use a specialized threading library such as Pthreads (http://en.wikipedia.org/wiki/POSIX_Threads) or more general purpose libraries such as Boost, ACE, POCO which already have support for threading. The least recommended way is to call directly the multi-threading related services of the OS.

For Java, no matter what edition you are using you will already have the support for multi-threading programming built-in.

grivei