How to create a thread in a class? - multithreading

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?

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.

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.

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

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.

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

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.

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.

Related

What is the difference between "thread and QThread" and "mutex and QMutex" in Python?

I'm doing research on multithreads in python. Can you explain the differences between "thread and QThread" and "mutex and QMutex"?
Threads are a POSIX-defined concept, that your operating system (OS) provides to your userspace applications.
But OS threads are a bit cumbersome to handle (their only interface being POSIX syscalls), so programming languages offer wrappers around native threads, but confusingly call them thread too. Different programming language thus have different wrappers, which are more coherent with the rest of the language ecosystem.
That's why in Python you have the threading.Thread class, which simplifies using threads. And while there is std::thread in C++, when you program with the framework Qt, you prefer to use QThread as they offer more functionalities, and integrate better with the rest of the QObjects.
But ultimitately, all of them are still doing the same thing, which is controlling OS threads, just in a slightly different way (programming language).

what to substitute for TThread

I have source code for USB communication ("USBThread class") written in Borland C++ and uses Visual Component Library("vcl.h").
Now my task is to port that to Visual C++, because we did not buy Borland C++.
But this "USBThread class" has inheritance from a base class in "vcl.h", called "TThread".
May I ask , in Visual C++, What kind of base class I can use to substitute "TThread' as new inheritance source?
The original code uses "WaitFor" and "Terminate" methods coming TThread,
I do not know how to implement these two menthods with Visual C++.
Thanks!
This is likely to be a difficult exerice imo but it looks like Boost.Thread using join for WaitFor and interrupt for Terminate would get you started.
I am basing this on review of the docs for VCL found here. I say this is likely to be difficult because VCL may have behaviour that is undocumented or otherwise unexpected.
Although you could translate the VCL's TThread class into C++, it would not work very well because it relies on some Delphi semantics that simply do not translate to C++ at all (in particular the TObject::AfterConstruction() method). You are best off simply re-writing USBThread to use Win32 thread functions directly, namely CreateThread() and WaitForSingleObject(). For Terminate(), you simply set a bool flag somewhere that your thread procedure can look at periodically and stop its work when set to true.

What does built in support for multithreading mean?

Java provides built-in support for multithreaded programming.
That is what my book says. I can do multithreaded programming in C, C++ also. So do they also provide built-in support for multithreading?
What does built in support for multithreading mean? Isn't it the OS that ACTUALLY provides support for multithreading?
Are there any programming languages that cannot support multithreading? If so why? (I am asking this question because, if the OS provides support for multithreading then why cant we do multithreaded programming on all languages that are supported on that OS?)
The issue is one of language-support vs. library support for multithreading.
Java's use of the keyword synchronized for placing locks on objects is a language-level construct. Also the built-in methods on Object (wait, notify, notifyAll) are implemented directly in runtime.
There is a bit of a debate regarding whether languages should implement threading though keywords and language structures and core data types vs. having all thread capabilities in the library.
A research paper espousing the view that language-level threading is beneficial is the relatively famous http://www.hpl.hp.com/personal/Hans_Boehm/misc_slides/pldi05_threads.pdf.
In theory, any language built on a C runtime can access a library such as pthreads, and any language running on a JVM can use those threads. In short all languages that can use a library (and have the notion of function pointers) can indeed do multithreading.
I believe they mean that Java has keywords like volatile and synchronized keyword built-in, to make multithreading easier, and that the library already provides threading classes so you don't need a 3rd party library.
The language needs constructs to create and destroy threads, and in turn the OS needs to provide this behaviour to the language.
Exception being Java Green Threads that aren't real threads at all, similarly with Erlang I think.
A language without threading support, say Basic implemented by QBasic in DOS. Basic is supposed to be basic so threads and processes are advanced features that are non-productive in the languages intent.
C and C++ as a language have no mechanism to:
Start a thread
Declare a mutex, semaphore, etc
etc
This is not part of the language specification. However, such facilities exist on every major operating system. Unlike in Java, these facilities are different on different operating systems: pthread on Linux, OS X and other UNIX-derivatives, CreateThread on Windows, another API on real-time operating systems.
Java has a language definition for Thread, synchronized blocks and methods, 'notify' 'wait' as part of the core Object and the like, which makes the language proper understand multithreading.
It means that there is functionality in the language's runtime that models the concepts of threads and all that goes with that such as providing synchronisation. What happens behind the scenes is up to the languages implementors... they could choose to use native OS threading or they might fake it.
A language that doesn't support it could be VB6 (at least not natively, IIRC)

Which scripting languages support multi-core programming?

I have written a little python application and here you can see how Task Manager looks during a typical run.
(source: weinzierl.name)
While the application is perfectly multithreaded, unsurprisingly it uses only one CPU core.
Regardless of the fact that most modern scripting languages support multithreading, scripts can run on one CPU core only.
Ruby, Python, Lua, PHP all can only run on a single core.
Even Erlang, which is said to be especially good for concurrent programming, is affected.
Is there a scripting language that has built in
support for threads that are not confined to a single core?
WRAP UP
Answers were not quite what I expected, but the TCL answer comes close.
I'd like to add perl, which (much like TCL) has interpreter-based threads.
Jython, IronPython and Groovy fall under the umbrella of combining a proven language with the proven virtual machine of another language. Thanks for your hints in this
direction.
I chose Aiden Bell's answer as Accepted Answer.
He does not suggest a particular language but his remark was most insightful to me.
You seem use a definition of "scripting language" that may raise a few eyebrows, and I don't know what that implies about your other requirements.
Anyway, have you considered TCL? It will do what you want, I believe.
Since you are including fairly general purpose languages in your list, I don't know how heavy an implementation is acceptable to you. I'd be surprised if one of the zillion Scheme implementations doesn't to native threads, but off the top of my head, I can only remember the MzScheme used to but I seem to remember support was dropped. Certainly some of the Common LISP implementations do this well. If Embeddable Common Lisp (ECL) does, it might work for you. I don't use it though so I'm not sure what the state of it's threading support is, and this may of course depend on platform.
Update Also, if I recall correctly, GHC Haskell doesn't do quite what you are asking, but may do effectively what you want since, again, as I recall, it will spin of a native thread per core or so and then run its threads across those....
You can freely multi-thread with the Python language in implementations such as Jython (on the JVM, as #Reginaldo mention Groovy is) and IronPython (on .NET). For the classical CPython implementation of the Python language, as #Dan's comment mentions, multiprocessing (rather than threading) is the way to freely use as many cores as you have available
Thread syntax may be static, but implementation across operating systems and virtual machines may change
Your scripting language may use true threading on one OS and fake-threads on another.
If you have performance requirements, it might be worth looking to ensure that the scripted threads fall through to the most beneficial layer in the OS. Userspace threads will be faster, but for largely blocking thread activity kernel threads will be better.
As Groovy is based on the Java virtual machine, you get support for true threads.
F# on .NET 4 has excellent support for parallel programming and extremely good performance as well as support for .fsx files that are specifically designed for scripting. I do all my scripting using F#.
An answer for this question has already been accepted, but just to add that besides tcl, the only other interpreted scripting language that I know of that supports multithreading and thread-safe programming is Qore.
Qore was designed from the bottom up to support multithreading; every aspect of the language is thread-safe; the language was designed to support SMP scalability and multithreading natively. For example, you can use the background operator to start a new thread or the ThreadPool class to manage a pool of threads. Qore will also throw exceptions with common thread errors so that threading errors (like potential deadlocks or errors with threading APIs like trying to grab a lock that's already held by the current thread) are immediately visible to the programmer.
Qore additionally supports and thread resources; for example, a DatasourcePool allocation is treated as a thread-local resource; if you forget to commit or roll back a transaction before you end your thread, the thread resource handling for the DatasourcePool class will roll back the transaction automatically and throw an exception with user-friendly information about the problem and how it was solved.
Maybe it could be useful for you - an overview of Qore's features is here: Why use Qore?.
CSScript in combination with Parallel Extensions shouldn't be a bad option. You write your code in pure C# and then run it as a script.
It is not related to the threading mechanism. The problem is that (for example in python) you have to get interpreter instance to run the script. To acquire the interpreter you have to lock it as it is going to keep the reference count and etc and need to avoid concurrent access to this objects. Python uses pthread and they are real threads but when you are working with python objects just one thread is running an others waiting. They call this GIL (Global Interpreter Lock) and it is the main problem that makes real parallelism impossible inside a process.
https://wiki.python.org/moin/GlobalInterpreterLock
The other scripting languages may have kind of the same problem.
Guile supports POSIX threads which I believe are hardware threads.

What high level languages support multithreading? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm wondering which languages support (or don't support) native multithreading, and perhaps get some details about the implementation. Hopefully we can produce a complete overview of this specific functionality.
Erlang has built-in support for concurrent programming.
Strictly speaking, Erlang processe are greenlets. But the language and virtual machine are designed from the ground up to support concurrency. The language has specific control structures for asynchronous inter-process messaging.
In Python, greenlet is a third-party package that provides lightweight threads and channel-based messaging. But it does not bear the comparison with Erlang.
I suppose that the list of languages that are higher-level than Haskell is pretty short, and it has pretty good support for concurrency and parallelism.
With CPython, one has to remember about the GIL. To summarize: only one processor is used, even on multiprocessor machines. There are multiple ways around this, as the comment shows.
Older versions of C and C++ (namely, C89, C99, C++98, and C++03) have no support at all in the core language, although libraries such as POSIX threads are available for pretty much every platform in common user today.
The newest versions of C and C++, C11 and C++11, do have built-in threading support in the language, but it's an optional feature of C11, so implementations such as single-core embedded systems can choose not to support it while supporting the rest of C11 if they desire.
Delphi/FreePascal also has support for threads.
I'll assume, from other answers, that it's only native on the Windows platforms.
Some nice libraries that implement better features on top of the TThread Object:
OmniThreadLibrary
BMThread
Clojure is an up and coming Lisp-dialect for the JVM that is specifically designed to handle concurrency well.
It features a functional style API, some very efficient implementations of various immutable data structures, and agent system (bit like actors in Scala and processes in Erlang). It even has software transactional memory.
All in all, Clojure goes to great lenght to help you write correct multithreaded and concurrent code.
I believe that the official squeak VM does not support native (OS) threads, but that the Gemstone version does.
(Feel free to edit this if not correct).
You need to define "native" in this context.
Java claims some sort of built-in multithreading, but is just based on coarse grained locking and some library support. At this moment, it is not more 'native' than C with the POSIX threads. The next version of C++ (0x) will include a threading library as well.
I know Java and C# support multithreading and that the next version of C++ will support it directly... (The planned implementation is available as part of the boost.org libraries...)
Boost::thread is great, I'm not sure whether you can say its part of the language though. It depends if you consider the CRT/STL/Boost to be 'part' of C++, or an optional add-on library.
(otherwise practically no language has native threading as they're all a feature of the OS).
This question doesn't make sense: whether a particular implementation chooses to implement threads as native threads or green threads has nothing to do with the language, that is an internal implementation detail.
There are Java implementations that use native threads and Java implementations that use green threads.
There are Ruby implementations that use native threads and Ruby implementations that use green threads.
There are Python implementations that use native threads and Python implementations that use green threads.
There are even POSIX Thread implementations that use green threads, e.g. the old LinuxThreads library or the GNU pth library.
And just because an implementation uses native threads doesn't mean that these threads can actually run in parallel; many implementations use a Global Interpreter Lock to ensure only one thread can run at a time. On the other hand, using green threads doesn't mean that they can't run in parallel: the BEAM Erlang VM for example can schedule its green threads (more precisely green processes) across mulitple CPU cores, the same is planned for the Rubinius Ruby VM.
Perl doesn't usefully support native threads.
Yes, there is a Perl threads module, and yes it uses native platform threads in its implementation. The problem is, it isn't very useful in the general case.
When you create a new thread using Perl threads, it copies the entire state of the Perl interpreter. This is very slow and uses lots of RAM. In fact it's probably slower than using fork() on Unix, as the latter uses copy-on-write and Perl threads do not.
But in general each language has its own threading model, some are different from others. Python (mostly) uses native platform threads but has a big lock which ensures that only one runs (Python code) at once. This actually has some advantages.
Aren't threads out of fashion these days in favour of processes? (Think Google Chrome, IE8)
I made a multithreading extension for Lua recently, called Lua Lanes. It merges multithreading concepts so naturally to the language, that I would not see 'built in' multithreading being any better.
For the record, Lua's built in co-operative multithreading (coroutines) can often also be used. With or without Lanes.
Lanes has no GIL, and runs code in separate Lua universes per thread. Thus, unless your C libraries would crash, it is immune to the problems associated with thread usage. In fact, the concept is more like processes and message passing, although only one OS process is used.
Finally Go is here with multi-threading with its own pkg Goroutine.
People say it is on the structure of C-language.
It is also easy to use and understand .
Perl and Python do. Ruby is working on it, but the threads in Ruby 1.8 are not really threads.

Resources