gcc atomic built-in functions: any known conflicts within multithreading environments? - multithreading

So, I want to avoid future problems when using __sync_fetch_and_add in a context of Boost-based multithreaded application.
Any chance that a low-level threading implementation used by Boost (pthreads here) would affect the functionality of buitins?

The builtins are the intrinsics.
They don't make assumptions about the libraries that will be used in applications.
There is no way it can interfere.
(On a tangent: Some libraries, like Boost Asio, optionally can use C++11 atomics instead of boost::detail::atomic_count (doc))

Related

Is it possible to use SIMD instructions in Rust?

In C/C++, you can use intrinsics for SIMD (such as AVX and AVX2) instructions. Is there a way to use SIMD in Rust?
The answer is yes, with caveats:
It is available on stable for x86 and x86_64 through the core::arch module, reexported as std::arch.
Other CPUs require the use of a nightly compiler for now.
Not all instructions may be available through core::arch, in which case inline assembly is necessary, which also requires a nightly compiler.
The std::arch module only provides CPU instructions as intrinsics, and requires the use of unsafe blocks as well as specific feature on the functions containing those instructions to properly align arguments. The documentation of std::arch is a good starting point for compile-time and run-time detection of CPU features.
As noted in the documentation, higher level APIs will likely be available at some point in the future under std::simd (and possibly core::simd); a sneak preview being available in the stdsimd crate:
Ergonomics
It's important to note that using the arch module is not the easiest thing in the world, so if you're curious to try it out you may want to brace yourself for some wordiness!
The primary purpose of this module is to enable stable crates on crates.io to build up much more ergonomic abstractions which end up using SIMD under the hood. Over time these abstractions may also move into the standard library itself, but for now this module is tasked with providing the bare minimum necessary to use vendor intrinsics on stable Rust.
Note: you may also possibly use the FFI to link in a library that does so for you; for example Shepmaster's cupid crate uses such a strategy to access cpu features at runtime.

How pthread_once() is implemented internally?

Do we use any locking mechanism inside pthread_once()? What is the cost of using pthread_once() instead of using pthread_mutex_lock() and pthread_mutex_unlock() in the threadsafe singleton class?
The spec does not define how pthread_once and pthread_mutex_lock must be implemented, but only how they must behave, so different platforms will have different implementations.
It is generally possible to make pthread_once simpler than a mutex (since all it requires is an atomic test-and-set operation, and no blocking), but I would also suspect that pthread_mutex_lock likely received more optimization because it is much more widely used.
If you care about performance, you will have to write a benchmark and run it on the platform you are targeting, and choose the one that's faster.
You can read directly the glibc implementation of pthread_once
You are lucky because recently glibc team unified all the different implementations across the the supported architectures. They were able to replace pure assembly with modern C code that also make use of C11 atomics support

C++ 98 and threading

I am working with OpenCV, an open source image processing library, and due to complexities in my algorithm I need to use multiple threads for video processing.
How multi-threading is done on C++ 98? I know about C++ 11 has a built in support library for threading (std::thread) but my platform (MSVC++ 2010) does not have that. Also I read about Boost library, which is a general purpose extension to C++ STL, has methods for multi-threading. I also know with MSDN support (windows.h) I can create and manage threads for Windows applications. Finally, I found out that Qt library, a cross platform GUI solution, has support for threading.
Is there a naive way (not having any 3rd party libraries) to create a cross-platform multi-threading application?
C++98 does not have any support for threading, neither in the language nor the standard library. You need to use a third party library and you have already listed a number of the main candidates.
OpenCV relies on different external systems for multithreading (or more accurately parallel processing).
Possible options are:
OpenMP (handled at the compiler level);
Intel's TBB (external library);
libdispatch (on systems that support it, like MacOS, iOS, *BSD);
GPGPU approaches with CUDA and OpenCL.
In recent versions of OpenCV these systems are "hidden" behind a parallel_for construct.
All this applies to parallel processing, i.e., data parallel tasks (roughly speaking, process each pixel or row of the input in parallel). If you need application level multithreading (like for example having a master thread and workers) then you need to use frameworks such as POSIX's threads or Qt.
I recommend boost::thread which is (mostly) compatible with std::thread in C++11. It is cross-platform and very mature.
OpenCV's parallelism is internal and does not directly mix with your code, but it may use more resources and cores than you might expect (as a feature), but this might be at the expense of other external processes.

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)

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