I have just started reading about threading in C, using pthreads. I know that Pthreads are available for Windows, but do multithreaded WIndows based C/C++ applications mostly use Pthreads?
Also in Unix/Linux are Pthreads the main way developers write multithreaded C/C++ code?
No, most will use the thread abstraction of the application/gui library they are using, e.g. MFC. Or in the plain C case, using the windows API directly.
Pthreads stands for "POSIX" threads, which is basically standarized unix(-like), a standard that has little meaning on Windows outside dedicated POSIX emulations like cygwin.
The new C++ Standard, C++11, has support for portable threads. I'd definitely go with that, except that Visual Studio hasn't implemented them yet.
Related
I have a function, lets say, callMe(args). I would like to be able to call my function 100 times in the same time (concurrent programing). My function is written in plain C. I want to make it concurrent for Windows and Linux.
Is there any built in support for this in standard C library? Or can you provide some advices, how should I do this in Linux/Windows?
Is pthread good for Linux environments for this?
You can't write a concurrent program in pure standard C99. Concurrent programming is not in standard C99; it requires some operating system specific libraries. However, the latest C11 standard (which has very few implementations today, February 2014 - I know none!) is beginning to add support for threads. On Linux systems I would suggest today to stick to pthreads.
You could use a cross-platform library (like glib from GTK) to ease porting between Windows and Linux. On Linux, it is wrapping pthreads.
I don't know Windows, but Glib -and GTK for GUI applications- is rumored to enable source-compatible coding between Windows & Linux
If your computation is embarrassingly data parallel (like some matrix numerical computations are), consider also OpenCL (notably because it can run on GPGPUs) or OpenMP. If it fits in a message passing paradigm, consider MPI
OpenCL, OpenMP, MPI are rumored to enable source-compatible coding for Linux and for Windows.
In C++11 (which is a different language than C) you have thread support, and the GCC 4.8.2 compiler is supporting them quite well. And Qt or Poco or Boost is a cross-platform C++ library, notably wrapping concurrent primitives.
Whatever concurrent mechanism you are using, beware of synchronization issues.
You always have synchronization with concurrency, at least to wait for the results of each concurrent or parallel sub-computation
You have to use different functions for different platforms.
Its similar like different browsers uses different kind of css property names.
So for linux/mac you can use pthread
And for windows you use CreateThread
I am quite a newbie to c programming. Until now i only found pthread_mutex_lock can make the code region run only by one thread. Does there are any other ways to implement a lock? Or every other way to do a lock is still use pthread_mutex_lock function?
Threads were only introduced into the ISO C standard with C11, a rather recent edition to the standard so not necessarily widely supported yet.
You need to look into threads.h and the mtx_* functions for an understanding of that.
Before then, pthreads was probably your best bet with its wide implementation although, not being standard C (a), its support wasn't mandated.
For example, Windows has its own way of doing threading, using functions like CreateThread.
However, there are various third-party products such as pthreads-win32 that aim to give pthreads support to Windows, to assist in porting of applications from POSIX-compliant operating systems.
(a) It is a POSIX standard (part of IEEE 1003.1) so that may be good enough for some people.
There is no way to lock in the c language. Operating systems might provide support for locking (without regard for the language), and libraries such as pthreads can take advantage of operating system services, however this is beside the language. (By contast, other languages have native locking built into them, such as through Java's synchronized keyword.)
Is multithreading supported in C? If yes, then how do I try? Is there any open source library that lets me do it and is the library supported on Mac OS X?
I haven't found any article saying that it's supported.
C is not intrinsically a multithreaded language; however there are many libraries which add threading functionality.
pthreads is a library compatible with any POSIX system, so it is supported on OSX. I found https://hpc-tutorials.llnl.gov/posix/ to be a good place to start.
Win32 has a threading library for C described at https://learn.microsoft.com/en-us/cpp/parallel/multithreading-with-c-and-win32.
Glib adds threading supported, and has the advantage of being completely cross-platform, as long as glib is installed on the target machine. There is some information here: http://developer.gnome.org/glib/2.28/glib-Threads.html
C has no concept whatever of threads. There is no thread support in C Standard. There are extensions available that can implement multi threading - one of which is pthreads.
Be aware because C language has no natural support of threads you as the programmer have to take care of everything and you will not be protected against any of the pitfalls of multi-threaded programming.
the new dialect - C1X, will offer multi-threading out of the box, as stated from wikipedia:
Multithreading support (_Thread_local storage-class specifier, header including thread creation/management functions, mutex, condition variable and thread-specific storage functionality, as well as the _Atomic type qualifier and for uninterruptible object access).
currently of courae as mentioned above, multi-threading is not supported in the newest dialect of c - C99
Pthreads. OSX has posix support.
I would guess that the majority of multithreaded programming on Mac OS X is done in Objective-C or C++, not plain C. (I realize that this isn't exactly an answer to the question that you asked, but you might want to know about alternatives.) In Objective-C, you'd use NSThread or, in Snow Leopard and later, Grand Central Dispatch (GCD). In C++, you could use the threads library from boost.org, which has the advantage of being cross-platform.
I've done threads in Java, but I'm a complete noob to threads in C. My first question, after googling some, is: Which thread library do I use? Does it matter? It seems I have thread.h and pthread.h to choose from.
OS is, and will be, Linux. More specifically, it's Ubuntu at the moment and will either stay like that, or become RHEL. But I guess the distro doesn't matter?
The POSIX thread libraries (pthread.h) are a standards based thread API for C/C++ and is what I would use. There are also several tutorials available such as this one or this one.
I will admit that I am not familiar with thread.h.
The standard thread interface on POSIX systems such as linux is pthread, for POSIX thread.
Currently I am developing a program for academic purpose. We use Turbo C because we are requesting for interrupt directly with the hardware.
We can do anything for our project, and I've already chosen a topic. The thing is I think I really need 2 threads in my program. Are there any thread libraries that are compatible with Turbo C in this case?
We use Turbo C because we are
requesting for interrupt directly with
the hardware
Many Windows programs interact directly with the hardware without being written in a DOS environment in Turbo C. Take a look at the WDK for more information about this.