Thread synchronization problem - multithreading

I use Qt and C++. I have a list of threads (Qlist<QtThread>).
I try to synchronize them. All threads calculate some values. And I want to take them.
Have you any ideas? thank a lot

You can use the finished() signal of a thread to execute a slot when your thread finishes executing.

Your question is a bit broad, but Qt has a lot of documentation on different thread synchronization methods.
The base of the docs is at Threading Support in Qt. The specific part you should find the info in is the Synchronizing Threads section. It lists the various mutex, locks, semaphores, wait conditions that are available in the Qt framework.
The documentation for all these classes has example usage code. Also have a look at the Threading and Concurrent Programming Examples, you'll probably find what you're after in there.

Related

Threads / Fibers - Clarification

I'm a bit confused regarding the concept of "fibers" as to their relation to 1) threads and 2) what they're seen as by the Kernel.
To my understanding, a fiber is a thread that was created by a thread, and managed by its creating thread (i.e. perhaps a scheduler?). However, for all intensive purposes, I'd consider it as still being a "thread" and seen by the kernel as such.
The explanation I'm receiving from a colleague is that fibers are completely invisible to the kernel and run entirely in user space, and are in no way "threads", as per his email here:
Threads are sometimes implemented in userspace libraries, thus called
user threads. The kernel is unaware of them, so they are managed and
scheduled in userspace. Some implementations base their user threads
on top of several kernel threads, to benefit from multi-processor
machines (M:N model). In this article the term "thread" (without
kernel or user qualifier) defaults to referring to kernel threads.
User threads as implemented by virtual machines are also called green
threads. User threads are generally fast to create and manage, but
cannot take advantage of multithreading or multiprocessing, and will
get blocked if all of their associated kernel threads get blocked even
if there are some user threads that are ready to run.
Fibers are an even lighter unit of scheduling which are cooperatively
scheduled: a running fiber must explicitly "yield" to allow another
fiber to run, which makes their implementation much easier than kernel
or user threads. A fiber can be scheduled to run in any thread in the
same process. This permits applications to gain performance
improvements by managing scheduling themselves, instead of relying on
the kernel scheduler (which may not be tuned for the application).
Parallel programming environments such as OpenMP typically implement
their tasks through fibers. Closely related to fibers are coroutines,
with the distinction being that coroutines are a language-level
construct, while fibers are a system-level construct.
I'm hoping to get a little bit of a better explanation in terms of exactly what a fiber is (is it an actual thread as far as the os/kernel are concerned, and simply managed by its creating thread?).
I've researched it extensively via Google searches, but have only found one simple answer everywhere I've looked, including here: "A fiber is a thread that's created and managed by a thread."
If anyone has anything more informative they can share or point me to, it would be greatly appreciated. My colleagues argument is that Golang's Goroutines use "fibers" and those fibers are invisible to the OS - thus, a "correct" implementation of fibers. I, personally, feel that Goroutines are more closely related to coroutines, and do not implement a fiber/thread scenario at all...

QProcess, QEventLoop - of any use for parallel-processing

I wonder whether I could use QEventLoop (QProcess?) to parallelize multiple calls to same function with Qt. What is precisely the difference with QtConcurrent or QThread? What is a process and an event loop more precisely? I read that QCoreApplication must exec() as early as possible in main() method, so that I wonder why it is different from main Thread.
could you point as some efficient reference to processes and thread with Qt? I came through the official doc and those things remain unclear.
Thanks and regards.
Process and thread are not Qt-specific concepts. You can search for "process vs. thread" anywhere for that distinction to be explained. For instance: What resources are shared between threads?
Though related concepts, spawning a new process is a more "heavyweight" form of parallelism than spawning a new thread within your existing process. Processes are protected from each other by default, while threads of execution within a process can read and write each other's memory directly. The protection you get from spawning processes comes at a greater run-time cost...and since independent processes can't read each other's memory, you have to share data between them using methods of inter-process communication.
Odds are that you want threads, because they're simpler to use in a case where one is writing all the code in a program. Given all the complexities in multithreaded programming, I'd suggest looking at a good book or reading some websites to start with. See: What are some good resources for learning threaded programming?
But if you want to dive in and just get a feel for how threading in Qt looks, you can spend time looking at the examples:
http://qt-project.org/doc/qt-4.8/examples-threadandconcurrent.html
QtConcurrent is an abstraction library that makes it easier to implement some kinds of parallel programming patterns. It's built on top of the QThread abstractions, and there's nothing it can do that you couldn't code yourself by writing to QThread directly. But it might make your code easier to write and less prone to errors.
As for an event loop...that is merely a generic term for how any given thread of execution in your program waits for work items to process, processes them, and can decide when it is no longer needed. If a thread's job were merely to start up, do some math, and exit...then it wouldn't need an event loop. But starting and stopping a thread takes time and churns resources. So typically threads live for longer periods of time, and have an event loop that knows how to wait for events it needs to respond to.
If you build on top of QtConcurrent, you won't have to worry about an event loop in your worker threads because they are managed automatically in a thread pool. The word count example is pretty simple to see:
http://qt-project.org/doc/qt-4.8/qtconcurrent-wordcount-main-cpp.html

What are some of the core principles needed to master multi-threading using Delphi?

I am kind of new to programming in general (about 8 months with on and off in Delphi and a little Python here and there) and I am in the process of buying some books.
I am interested in learning about concurrent programming and building multi-threaded apps using Delphi. Whenever I do a search for "multithreading Delphi" or "Delphi multithreading tutorial" I seem to get conflicting results as some of the stuff is about using certain libraries (Omnithread library) and other stuff seems to be more geared towards programmers with more experience.
I have studied quite a few books on Delphi and for the most part they seem to kind of skim the surface and not really go into depth on the subject. I have a friend who is a programmer (he uses c++) who recommends I learn what is actually going on with the underlying system when using threads as opposed to jumping into how to actually implement them in my programs first.
On Amazon.com there are quite a few books on concurrent programming but none of them seem to be made with Delphi in mind.
Basically I need to know what are the main things I should be focused on learning before jumping into using threads, if I can/should attempt to learn them using books that are not specifically aimed at Delphi developers (don't want to confuse myself reading books with a bunch of code examples in other languages right now) and if there are any reliable resources/books on the subject that anyone here could recommend.
Short answer
Go to OmnyThreadLibrary install it and read everything on the site.
Longer answer
You asked for some info so here goes:
Here's some stuff to read:
http://delphi.about.com/od/kbthread/Threading_in_Delphi.htm
I personally like: Multithreading - The Delphi Way.
(It's old, but the basics still apply)
Basic principles:
Your basic VCL application is single threaded.
The VCL was not build with multi-threading in mind, rather thread-support is bolted on so that most VCL components are not thread-safe.
The way in which this is done is by making the CPU wait, so if you want a fast application be careful when and how to communicate with the VCL.
Communicating with the VCL
Your basic thread is a decendent of TThread with its own members.
These are per thread variables. As long as you use these you don't have any problems.
My favorite way of communicating with the main window is by using custom windows Messages and postmessage to communicate asynchronically.
If you want to communicate synchronically you will need to use a critical section or a synchonize method.
See this article for example: http://edn.embarcadero.com/article/22411
Communicating between threads
This is where things get tricky, because you can run into all sorts of hard to debug synchonization issues.
My advice: use OmnithreadLibrary, also see this question: Cross thread communication in Delphi
Some people will tell you that reading and writing integers is atomic on x86, but this is not 100% true, so don't use those in a naive way, because you'll most likely get subtle issues wrong and end up with hard to debug code.
Starting and stopping threads
In old Delphi versions Thread.suspend and Thread.resume were used, however these are no longer recommended and should be avoided (in the context of thread synchronization).
See this question: With what delphi Code should I replace my calls to deprecated TThread method Suspend?
Also have a look at this question although the answers are more vague: TThread.resume is deprecated in Delphi-2010 what should be used in place?
You can use suspend and resume to pause and restart threads, just don't use them for thread synchronization.
Performance issues
Putting wait_for... , synchonize etc code in your thread effectively stops your thread until the action it's waiting for has occured.
In my opinion this defeats a big purpose of threads: speed
So if you want to be fast you'll have to get creative.
A long time ago I wrote an application called Life32.
Its a display program for conways game of life. That can generate patterns very fast (millions of generations per second on small patterns).
It used a separate thread for calculation and a separate thread for display.
Displaying is a very slow operation that does not need to be done every generation.
The generation thread included display code that removes stuff from the display (when in view) and the display thread simply sets a boolean that tells the generation thread to also display the added stuff.
The generation code writes directly to the video memory using DirectX, no VCL or Windows calls required and no synchronization of any kind.
If you move the main window the application will keep on displaying on the old location until you pause the generation, thereby stopping the generation thread, at which point it's safe to update the thread variables.
If the threads are not 100% synchronized the display happens a generation too late, no big deal.
It also features a custom memory manager that avoids the thread-safe slowness that's in the standard memory manager.
By avoiding any and all forms of thread synchronization I was able to eliminate the overhead from 90%+ (on smallish patterns) to 0.
You really shouldn't get me started on this, but anyway, my suggestions:
Try hard to not use the following:
TThread.Synchronize
TThread.WaitFor
TThread.OnTerminate
TThread.Suspend
TThread.Resume, (except at the end of constructors in some Delphi versions)
TApplication.ProcessMessages
Use the PostMessage API to communicate to the main thread - post objects in lParam, say.
Use a producer-consumer queue to communicate to secondary threads, (not a Windows message queue - only one thread can wait on a WMQ, making thread pooling impossible).
Do not write directly from one thread to fields in another - use message-passing.
Try very hard indeed to create threads at application startup and to not explicitly terminate them at all.
Do use object pools instead of continually creating and freeing objects for inter-thread communication.
The result will be an app that performs well, does not leak, does not deadlock and shuts down immediately when you close the main form.
What Delphi should have had built-in:
TWinControl.PostObject(anObject:TObject) and TWinControl.OnObjectRx(anObject:TObject) - methods to post objects from a secondary thread and fire a main-thread event with them. A trivial PostMessage wrap to replace the poor performing, deadlock-generating, continually-rewritten TThread.Synchronize.
A simple, unbounded producer-consumer class that actually works for multiple producers/consumers. This is, like, 20 lines of TObjectQueue descendant but Borland/Embarcadero could not manage it. If you have object pools, there is no need for complex bounded queues.
A simple thread-safe, blocking, object pool class - again, really simple with Delphi since it has class variables and virtual constructors, eg. creating a lot of buffer objects:
myPool:=TobjectPool.create(1024,TmyBuffer);
I thought it might be useful to actually try to compile a list of things that one should know about multithreading.
Synchronization primitives: mutexes, semaphores, monitors
Delphi implementations of synchronization primitives: TCriticalSection, TMREWSync, TEvent
Atomic operations: some knowledge about what operations are atomic and what not (discussed in this question)
Windows API multithreading capabilities: InterlockedIncrement, InterlockedExchange, ...
OmniThreadLibrary
Of course this is far from complete. I made this community wiki so that everyone can edit.
Appending to all the other answers I strongly suggest reading a book like:
"Modern Operating Systems" or any other one going into multithreading details.
This seems to be an overkill but it would make you a better programmer and
you defenitely get a very good insight
into threading/processes in an abstract way - so you learn why and how to
use critical section or semaphores on examples (like the
dining philosophers problem or the sleeping barber problem)

Multi Threading

How I can determine which thread is waiting for more time?
My requirement is, in a synchronized methods, when one thread finishes its work, I want to allow the thread which is waiting for the longest time. I hope my question make sense.
All depends on which language and/or environment you are using. So far as I know there's no intrinsic support for this in Java, if multiple threads are waiting to enter a synchronized method then the system will pick an arbitrary one to run when entry is possible.
If instead you use Java's wait() / notify() then you control which threads are notified and so can build your own priority mechanism, for example you could have a simple queue to which each thread adds itself before its wait() then you just take the top item from the queue and notify that thread.
You should not and almost certainly do not need to do this.
The threading environment will schedule threads for you.
If the software design is such that this appears to be a problem, then the design is incorrect for a pre-emptive threading environment.
What you may want to be doing is something more like managing and prioritizing units of work, where you for example service work in the order that it arrives.
In other words, the order of work processing should not in your design depend on which thread runs, but rather, on your design of how work is handed out to threads.
#djna Java doesn't let you choose which thread to notify. If 10 threads are in the queue any one of them can be notified.
This can be done by using the lock/condition interfaces in concurrent package.
Here you can associate each of these threads with a condition and then take out an item from that queue and signal the condition that is mapped with that thread/task.

What are threads?

What are threads?
Why do you think I should care about them?
Where would you suggest I go to learn more (I'm working in Delphi, if that matters).
Wikipedia: Thread (computing)
Wikipedia: Thread (computing) - Multithreading
Threading in Delphi
Threads allow you to utilize multiple processors or cores in a CPU, so they offer tremendous speedups when used properly on specific machines.
A little history from an "old timer": when I first heard about threads, they were referred to as "threads of control".
One of the earlier popular portable threading libraries was (and still is) "pthreads" or "POSIX Threads". It was derived from the "Concert Multithread Architecture" from Digital Equipment Corporation. It's notable that the documentation for pthreads still uses the term "thread of control". From an online man page for the pthread_create function:
pthread_create creates a new thread of control that executes concurrently with the calling thread.
You should care about threads because everything you do on a computer is done in a thread. Even the simplest "Hello, world" program contains at least a single thread.
Things get more interesting with multiple threads. In fact, things get too interesting if you're not very careful!
The number of 'things' your program/application can do at the same time without being influenced by the other 'things' it is doing at that specific time.
Example supporting 1. Lets say your program is a car, but you are not using threads, while opening the door, the door is stuck and you can't close it. Now you can't start your car because there is only one action available and you can't start any other before the previous is ended. If you would be using threads in your program ( the car ) you would be driving around with an open door ;-)
What a thread is is fairly easy, using it is language dependent so search for the right documentation and just have some fun :)

Resources