What is native Linux API used for Boost named_condition class implementation? In Windows, I guess, this is CreateEvent, I want to know what Linux feature can be used to get this functionality.
Posix Threads Condition Variables
Related
Can someone please explain in what scenario's it would be beneficial to use std::mutex vs pthread_mutex_t. I dont understand why we would ever use pthread_mutex_t. Thanks
The pthread_mutex_t is a POSIX solution (available for linux and other UNIX systems) that existed before c++11 introduced synchronisation primitives into the c++ library. You should use std::mutex now, amongst other things it is more cross-platform (can be used under Windows also).
std::mutex is just a thin wrapper around pthread_mutex on systems supporting pthreads.
In general, the operations on the std:: thread primitives are quite limited vs the native versions (pthreads or windows threads). If you don't need those features, you should always use the std:: versions, but if you do need the advanced features, then you have no choice but to use the native version.
native handle() method exists for exactly this reason.
std::mutex is from standard library, so if you use it your code will compile also on platforms where pthreads are not provided. For example under windows std::mutex uses native WinAPI mutex implementation.
Its best to always use std::mutex.
See pthread_mutexattr for an idea of the other flavors of POSIX mutexes available besides the default. Though one of the main ones-- recursive vs. nonrecursive-- is available with std::recursive_mutex, this is not true for things like BSD priority ceilings, etc.
When implementing condition variables into a Win32 C++ program, would it be better to use Win32 functions, classes, and data types (e.g. CreateThread, SleepConditionVariableCS, WaitForSingleObjectEx, ReleaseMutex, CONDITION_VARIABLE) or those from the C++11 standard libraries (e.g. thread, wait, join, unlock, condition_variable)?
Since the answer to this question is probably not binary, what considerations should one take into account when making such a decision?
The C++ synchronization mechanisms are designed to C++ principles. They free their resources in the destructor, and they also use RAII to ensure safe locking. They use exceptions to signal errors.
Essentially, they are much harder to use incorrectly than the function-based native Windows API. This means that if you can use them (your implementation supports them), you always should use them.
Oh, and they are cross-platform.
One consideration should be what your compiler can handle. For example, when you install MinGW on Windows, you can choose whether to install the API for POSIX threads or Win32 threads. On the other hand, if you use TDM-GCC, you should be aware that versions 4.7.1 and lower use Win32 threads, while versions 4.8.1 and higher use POSIX threads. And as woolstar mentioned above, if you're using Microsoft's compiler, you should check to see whether the bugs in its support for these classes have been worked out.
If your compiler supports POSIX threads, you can use the C++ thread classes of the Standard Library (e.g. thread, mutex, condition_variable). If your compiler supports Win32 threads, you can use the Win32 thread functions.
In my case, I originally had TDM-GCC 4.7.1 and tried to use the C++ Standard Library classes, but that didn't work (for reasons explained above). So I installed MinGW by itself and chose "posix" in the "threads" option of the installer. Then I was able to use those classes.
New to linux and c++.
I wante to create an application that only needs to run on linux (ubuntuz) and i need to use basic read-write locks.
I saw that there are a few libraries that give "concurrency capabilities".
e.g. to use mutexes, there are at least 3 options:
pthread_mutex_lock (pthread.h)
boost::mutex
std::mutex (mutex)
could someone explain the differences between the various approaches?
pthreads is a C-API and is available on all posix conformant systems (pthreads stands for Posix THREADS).
boost::mutex is a C++-only API that depends on the "boost"-library (you cannot use it in C-code; you add a dependency on "boost")
lots of features from boost will eventually end up in the C++ standard library, e.g. threading; with C++11 you have std::mutex, but you will need a compiler recent enough to support that recent addition. e.g. if you want your application to be backportable to older distributions you might want to avoid it.
std::mutex is part of the standard library. Every C++ compiler/library vendor is free to implement it in any way they like. A library implementation for Posix will more likely than not just use pthreads under the hood, while for instance a Windows library would use the Windows API primitives.
If you can, use std::mutex, but if you don't have C++11 support yet, write your own SBRM classes that wrap the pthread mutex and locking primitives, or use Boost if that's feasible (e.g. if your project already uses Boost anyway). It'll all come down to the same thing anyway.
I have seen tutorials on the internet for making multithreaded applications in C++ on Windows, and other tutorials for doing the same on Linux, but not for both at the same time. Are there functions that would work even if they were compiled on either Linux or Windows?
You would need to use a library which contains an implementation for both pthread on Linux and the Win32 threading library on Windows (CreateThread and friends).
Boost thread is a popular choice which abstracts the system away.
You can use POSIX threads and use this library to get pthreads on Windows.
http://sourceware.org/pthreads-win32/
(This is probably only a good option if you're already very used to doing threading on a POSIX system...)
You can start with boost::thread. The library provides an abstraction layer and works internally against native threading APIs of each supported platform.
You should be looking at the boost library.
Or you can use ZThread, its pretty lightweight as opposed to boost::thread
You can also look at QThread from Nokia's Qt
A portable option is also present in TBB's threads. Of course, TBB encourages you to use the concept of tasks rather than threads, but if you ever need just threads, then this example could help (you'll have to convert the deprecated headers and thread declarations to the new ones).
I suggest TinyThread++ or TinyCThread.
I started using TinyCThread, I found it amazingly simple and it supports many systems including Windows and Linux.
Is there a portable way of getting thread and/or process identifier (string, int, ...) with C++?
You have a few ways, but all imply the use of an external library abstracting the thread for you.
Among the popular choices, two are:
The Boost.Thread library. This is the most portable but imply working with Boost, which is a HUGE library
The Qt library. This is less portable and imply to work with Qt, a big library.
If you already use any on these two libraries, I would recommend sticking with it. Otherwise, look at what other tools they provide and make a choice.
There is no portable way when portable means a way that works on every platform for that a C++ compiler exists. Such a way had to be part of the C++ standard, in which case it really would work everywhere (just like the other parts of the C++ standard work everywhere). Everything not in the standard is not guaranteed to work on any platform, unless the platform states to support this standard.
Every solution people have suggested here is a solution that uses an external library and thus can only work on platforms supported by that library; and no library is available for every existing platform.
Probably the one that will get you farthest is POSIX, after all every UNIX like system tries to support at least some POSIX (the more the better), very little can call themselves really being 100% POSIX-compliant platforms (e.g. A/UX, AIX, HP-UX, IRIX, Mac OS X 10.5, MINIX, QNX, Solaris, UnixWare, VxWorks, ... to name a few, there are more of course). However, there are quite a couple of platforms that offer at least some POSIX support, some more, some less and some are almost POSIX-compliant (e.g. FreeBSD, Linux, NetBSD, BeOS, OpenBSD, ... and others).
Windows is unfortunately far away from being one. NT used to be partly POSIX conform, but now it has more or less vanished (Win2000/20003, WinXP, and Vista can still be set into a POSIX emulating mode, translating some POSIX call to internal API calls by installing Microsoft Windows Services for UNIX - SFU 3.5 or higher), however there are ways to get some POSIX functionality on Windows through external libraries as well (Cygwin offers LGPL libraries you can link with your app to enable a fair amount of POSIX functions on Windows).
The advantage of POSIX is not only that it is relatively widespread, but also that it is standardized and you can easily look up the standard on the Internet. Using POSIX calls you can get a thread id and a process id.
I always thought threads were external from C++. In Java, there's a native thread built-in to the language.
You'd have to find a portable thread library.
The only way is to use portable library. Id recommend Qt (it doesn't have to be GUI app) or maybe wxWidgets. If you are developing a game check out SDL
Also check out boost libs they might have something.
You may also use part of ACE library, which implements a platform independent wrapper. Finding PID would be one of the files in the library (maybe ACE_Process/ ACE_Thread).
I'm not sure how portable they are but Posix threads may be another option you want consider. See also here. I agree with Steve's comment--portable to which platforms?
getpid() is a portable way to get the process ID.
I don't think you're going to find a portable method unless it is through a wrapper library. Each threading system (eg. Windows, or POSIX) are going to have their own mechanism.