I want to use, say, QSemaphore with boost threads or c++ 11 threads. Is that allowed?
I'm working on pqConsole, where multithreading plays an important role.
Running a console for SWI-Prolog (multithreaded, implemented in C), the Qt GUI manages IO on behalf of user programs, executing in a background QThread, where the foreign language interface is instanced.
Threads can also be initiated from C side, and they get a dedicated console (see interactor/0), where the IO again is rendered on Qt GUI.
I'm using QMutex and QMutexLocker to syncronize (i.e. the simpler constructs), and AFAIK those are performing as expected.
Theoretically, yes, you can.
Note, you could also just use the boost semaphore available with that scenario. Also, QSemaphore currently does not use the pthread interface underneath, but some custom solution. There were discussions to refactor that later for utilizing the pthread features more.
Related
It says that Unity is not thread-safe. What does this exactly imply? If I have a multithreaded C++ DLL that I want to import and use in Unity, is there a procedure to make it compatible?
This is no problem, actually the single-thread limitation is typical for almost all user interface implementations. There are multiple ways how you can solve your problem. I don't know which solution is the best for Unity, I am just providing this as an example how you can proceed: You can for example have a single UI thread and call all Unity-related stuff only from this single thread. Use all other threads for whatever you want and let them communicate with your UI thread. The UI thread is then also de-facto mediator between Unity and the rest of your application.
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).
Our software consists of a Graphical User Interface in C++/Qt. The user interface controls several heavy computational algorithms in a separate library which uses C++/OpenMP for parallelization. In this library we cannot use Qt.
To keep our GUI responsive we use function pointers which call QApplication::processEvents();. This of course leads to spaghetti code. We would like to separate the GUI from the computation library, so that the function calls do not block the GUI any longer. What is the clean and prefered way to do this?
If you don't need to interrupt the openMP library calls, then I would go for a simple multi-threading approach: one thread deals with the GUI, another with the computational library. Naturally you cannot use openMP for this (this would not fare well with the computational openMP library), but must use other multi-threading methods. C++11 now comes with its own direct support of threads, so that's what I would do.
EDIT: read Anthony Williams "C++ concurrency in action"
QApplication launches control loop, which calls GUI methods. Naturally, any method taking long will block the queue. To prevent this, you need to spawn additional process/thread via fork/QThread. I think the QThread approach would be the cleanest way to acomplish your goal
I'm new to wxWidgets (C++), and threads for that matter. What should I be aware of concerning shared resources? Should I implement some sort of semaphore-based locking of resources that may be used by both the GUI thread and the worker thread(s)? Does wxWidgets offer some capability for dealing with this?
Not sure what your choice of threading library is at this point but in your case I'd use wxThread (see here & here for documentation).
What should I be aware of concerning
shared resources?
See the Important notes for multithreaded applications part here for wxWidgets specific multi-threading issues.
Other than that the 'usual' concerns about shared resources apply.
Should I implement some sort of
semaphore-based locking of resources
that may be used by both the GUI
thread and the worker thread(s)? Does
wxWidgets offer some capability for
dealing with this?
wxWidgets already implements a number of synchronization objects, see here.
As a side note prefer using the RAII locker helpers (wxMutexLocker, wxCriticalSectionLocker) instead of explicitly acquiring/releasing.
Well I have an application that uses both Objective C & c++ but for portability reasons I have tried to use c++ as much as possible.... Now I am confronted with some problem that requires threads I was thinking of using pthread instead of NSThread... is it Okay to use pthread? Will Apple punish me for using it by rejecting my app on the appstore...?
NSThread is built around pthread anyway
I can't see any reason why using pthread would lead to rejection from Apple's part
NSThread is mostly a wrapper around pthread semantics.
Advantages:
- NSThreadWillExitNotification notification when NSThread exits
- A NSMutableDictionary thread-local storage
Limitations:
- you can only create detached NSThread
Be aware that Cocoa needs to know that you want to do multi-threading. It is important to first detach a dummy NSThread so the application can be considered multi-threaded.
My app uses pthread API, changes the scheduling policy from SCHED_OTHER to SCHED_FIFO, and changes thread's priority.
It works well.
However, I avoid using Cocoa touch framework APIs in the thread because I don't know the side effect of pthread instead of NSThread.