I'm writing portable code for multicore machines and I want kernel level threads so the threads can use more than one cpu. After reading QThread documentation on Qt Assistant I still haven't found any hints.
On Windows XP the multithreading example (mandelbrot) from the QtSDK used only one core. So I guess on XP only user level threads are possible. I haven't tested that on Linux or OSX so far since there isn't the full SDK installed.
EDIT: The example given in the SDK is stupid - it only uses one thread for those calculation so the binding to only one core was misleading. Buildig a sample myself I could use all cores, so on XP with mingw/GCC Qt uses kernel level threads.
So, what kind of threads are used by QThread? Is it possible to specify what kind of thread to use?
Multiple processes are also an option in combination with shared memory.
Edit
http://doc.qt.io/qt-4.8/thread-basics.html gives a nice introduction.
I don't know about Windows, but on Unix it is using pthreads. QT isn't exposing API for CPU affinity because it needs to be platform- and hardware-independent. The QThread distribution across CPUs is left to the OS scheduler, you can't hint it via some QT API.
From QThread Class Reference:
A QThread represents a separate thread of control within the program; it shares data with all the other threads within the process but executes independently in the way that a separate program does on a multitasking operating system.
In your terms, it's a "kernel" thread.
Also, the conclusion that "only user-level threads are possible" on Windows XP is surely incorrect.
Related
Can a fiber created in thread A switch to another fiber created in thread B? To make the question more specific, some operating systems have fibers natively implemented (windows fibers),
other need to implement it themselves (using setjump longjump in linux etc.).
Libcoro for example wraps this all up in a single API (for windows it’s just a wrapper for native fibers, for Linux it implements it itself etc.)
So, if it's possible to migrate fibers between threads, can you give me an example usage in windows (linux) in c/c++?
I found something about fiber migration in the boost library documentation, but it's not specific enough about it's implementation and platform dependence. I still want to understand how to do it myself using only windows fibers for example (or using Libcoro on linux).
If it's not possible in a general way, why so?
I understand that fibers are meant to be used as lightweight threads for cooperative multitasking over a single thread, they have cheap context switching compared to regular threads, and they simplify the programming.
An example usage is a system with several threads, each having several fibers doing some kind of work hierarchy on their parent thread (never leaving the parent thread).
Even though it's not the intended use I still want to learn how to do it if it's possible in a general way, because I think I can optimize the work load on my job system by migrating fibers between threads.
The mentioned boost.fiber uses boost.context (callcc/continuation) to implement context switching.
Till boost-1.64 callcc was implemented in assembler only, boost-1.65 enables you to choose between assembler, Windows Fibers (Windows) or ucontext (POSIX if available; deprecated API by POSIX).
The assembler implementation is faster that the other two (2 orders of magnitude compared to ucontext).
boost.fiber uses callcc to implement lightweight threads/fibers - the library provides fiber schedulers that allow to migrate fibers between threads.
For instance one provided scheduler steals fibers from other threads if its run-queue goes out of work (fibers that are ready/that can be resumed).
(so you can choose Windows Fibers that get migrated between threads).
i was wondering how to force a program in C++ in Visual Studio, run on specific core/cores (on computers who has more than one).
i found this article, but in refers to C in Linux (and i am using Visual Studio on windows)
also, does the windows version I'm using, matter?
It is possible to use the Windows API function SetThreadIdealProcessorEx(). This function is applicable to Windows 7 or later. On older systems, it is possible to use SetThreadIdealProcessor(), albeit with some more limitations.
This, according to remarks at the first link
Specifying a thread ideal processor provides a hint to the scheduler about the preferred processor for a thread. The scheduler runs the thread on the thread's ideal processor when possible.
I'm not aware of any function that forces the scheduler to run a thread on a specified processor. So giving a hint, which the scheduler will attempt to act on, is probably the closest you can get to meeting your requirement.
It would probably be advisable to also use SetProcessorAffinityMask() as well, which works to specify processors on which a process may run, since it would seem unlikely that a thread can run on a processor that is not within its parent process's affinity mask.
Read the documentation for these functions carefully, because the system itself can impose limits on which processors a process may run.
I have seen in some posts it has been said that to use multiple cores of processor use Boost thread (use multi-threading) library. Usually threads are not visible to operating system. So how can we sure that multi-threading will support usage of multi-cores. Is there a difference between Java threads and Boost threads?
The operating system is also called a "supervisor" because it has access to everything. Since it is responsible for managing preemptive threads, it knows exactly how many a process has, and can inspect what they are doing at any time.
Java may add a layer of indirection (green threads) to make many threads look like one, depending on JVM and configuration. Boost does not do this, but instead only wraps the POSIX interface which usually communicates directly with the OS kernel.
Massively multithreaded applications may benefit from coalescing threads, so that the number of ready-to-run threads matches the number of logical CPU cores. Reducing everything to one thread may be going too far, though :v) and #Voo says that green threads are only a legacy technology. A good JVM should support true multithreading; check your configuration options. On the C++ side, there are libraries like Intel TBB and Apple GCD to help manage parallelism.
is there any way to set Processor affinity for ITK (3.20.1) threads. I was looking into ITK::MultiThreader class which has "SetGlobalDefaultNumberOfThreads". but i am not finding any function/method which is saying anything about affinity.
any help would be appreciated..
In the version that I have installed (3.18), it seems that this feature is not supported. However, looking at the MultiThreader header file, I noticed that the implementation relies on the native OS thread mechanisms. This means that on linux, pthreads are actually spawned, and pthread_t are directly used for ThreadProcessIdType, which means that you might be able to use the linux proprietary extensions for core affinities.
Quoting myself from this answer to another question:
Unfortunately, the posix thread API doesn't provide a way to set cpu affinity for threads. You may use the non portable extension provided on the linux platform pthread_attr_setaffinity_np, with the cpuset family of functions to configure a thread affinity.
Since the thread will be already running, you will need to get the thread attributes before being able to set its affinity. Linux provides one more non portable function for that: pthread_getattr_np.
Also, the cpuset feature must be built in the kernel, however this should be the case on most platforms with multicore support.
references:
cpuset
pthread_attr_setaffinity_np
pthread_getattr_np
Is there any possible way of disabling multi core functionality on windows and just using a single core using C\C++? Any library that allows it?
My application access one of our chip modules used to communicate with the host. We suspect someone else is accessing this module and changes it. The strange thing is that it only happens on a multi core system (Windows 7 64bit), and when you set windows to only use a single core (How to disable a core) everything works great.
It sounds to me that windows shouldn't allow any program to accomplish it programmatic, but I hope I'm mistaking.
EDIT: I'm not looking for suggestion regrading my threading skills. My problem is possibly more hardware or firmware related (Maybe the 2nd core is also accessing my chip module).
2nd EDIT: This is NOT a software problem! I only want to know if it's possible to disable multi core using C\C++. I don't seek threading advice as I'm 100% sure the problem doesn't lay there.
3rd EDIT: issue was SOLVED. The problem was with another process that my customer ran that accessing the same shared memory my application was accessing.
As I previously mentioned, there was no problem with my thread and I never got the answer I looked for: A simple Yes or No regarding weather it is possible to disable one of the cores using C++.
Processor affinity was not helpfull in my special case.
I think some workaround can be in setting affinity for main thread of your task for one core, than create threads with infinite loops for other cores and set them highest possible priority. But usually somthing wrong is in software if it cannot run on multicore hardware.
Even on a single core, you can be interrupted at any point. So I suspect your problem is to due to switching cores.
If you suspect the hardware driver isn't thread safe use the following to set interrupt affinity.
http://www.microsoft.com/whdc/system/sysperf/intpolicy.mspx
Unless your C/C++ library is using threading, I can't imagine how the number of cores could affect the runtime behavior. If your app does use threads, they will probably execute a bit differently on one core vs multiple cores, but if you can get an error on a multicore system you can probably get the same error on a single core system.
You can set processor affinity when launching your application. This locks your application to one processor. This may be a simple solution to the problem.
Multithreading is a beast. Even "foolproof" sometimes bites you in the ...uhm... "back".
When you're using threads, you should do it right, because the OS has only a few rules it has to follow and can schedule your threads as it pleases. IIRC you can set priorities etc, maybe express something like preferences for certain CPUs in some OSes.
Even when you're using only one additional thread, you still have two, because the main app runs as well.
Maybe have a look at the debugging tools that specialize on thread debugging. Maybe they can help you with that.
As a start I'd add some mutexes or synchronized areas that access the module.