Does the C language have support for multi-threading? - c

Since C Language does not provide any object oriented concepts, I wonder whether it also does not have support for multi-threading? I searched on the web - can anyone give me answer regarding this?

With C11, the language has full support for threading, including the memory model it inherited from C++11. It has facilities for threads, condition variables, mutexes and thread local storage, for instance.
Prior to C11, people typically used pthreads on unix systems and CreateThread on windows, which was supported via implementation defined behavior (not the C standard). Multithreading behavior would mostly defer to the behavior of that hardware.

C is un-doubtfully have multi-threading support. Check out pthread. And here is an tutorial on pthread:
https://computing.llnl.gov/tutorials/pthreads/

Whether a language is Object Oriented or not doesn't affect it's support for threading.
Yes you can use threads with C and there are various libraries you can use to do this, pthreads being one of them.

C1X will support threading, but right now, there is no such thing in c99.
Peeople do use less portable extensions like POSIX threads (pthreads), forking etc.
Standard C1X is still a draft and support from compilers is somewhat lacking, gcc partially support it, but I heard threading isnt complete yet (I mean, unstable, dev version of gcc, not 4.6).

Check these out:
Read pthreads
See OpenMP
Have a look at Cilk
And there is no relation between multithreaded computation and object oriented features. It will depend how you design your code, which will tell if it is object oriented or not.

Related

How is the POSIX threading api versioned and has it been updated since 1995?

I'm just now getting into threading and specifically I want to learn POSIX threads, and everything about them. As implemented in Linux. I'm looking for resources to learn about them but many of those resources are very old. Some of them date back to
1996 with PThreads Programming: A POSIX Standard for Better Multiprocessing (A Nutshell Handbook)
1997 with Programming with POSIX Threads
I am not asking for resources. Being that these two books are so old, I'm wondering how on topic they'd be. How is the POSIX API versioned? On Wikipedia all I see is this,
POSIX Threads is an API defined by the standard POSIX.1c, Threads extensions (IEEE Std 1003.1c-1995).
I'm not sure if that means that there are no updates since 1995 to the threading parts of POSIX or not? Is there any way to judge the relevancy of the material?
At the very bottom of the Wikipedia page you reference in the "External Links" section, you will find a link to the current Posix specification of pthreads.h, which includes a history of changes. There have been a few, but the basic principles are intact. So the books you mention are probably still good learning materials. (I still have a well-thumbed copy of Programming with POSIX Threads on my bookshelf.)
As is mentioned in the comments below, C11 provides atomics and thread-local storage, which is implemented by GCC since 4.9. (Thread-local storage was previously available in GCC as an extension, so it is not that new.) The existence of thread-local storage reduces the need for the Pthreads thread-local storage interfaces, but that is a small part of the Pthreads library (and, although thread-local storage is easier to use and syntactically more convenient, it doesn't really change program structure much.)
C11 also specifies an optional threads.h header which contains threading function similar to Pthreads. However, glibc does not include this header, and the use of Pthreads is still pretty well universal, although both open- and closed-source threads.h implementation do exist. (For open source implementations, see the musl library and/or the FreeBSD implementation, available since 10.0.) Conceptually, the C11 interfaces are very similar to Pthreads; obviously, they have different names and in some cases they are simplified. However, once you've mastered Pthreads, you should have little trouble understanding any C11-threads programs you come across.

There are how many ways to lock in c

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.)

posix threads (pthreads) standard

I'm looking for the pthreads standard for some work I'm doing on parallelism. I have found that it is supposed to be IEEE 1003.1c "EEE Standard for Information Technology--Portable Operating System Interface (POSIX(R)) - System Application Program Interface (API) Amendment 2: Threads Extension (C Language)". However, when I get to it, it says that the standard has been "superseded", but they don't say by what (or I failed to see where they do say it). Does anyone know of the link to the superseding standard? Thanks.
It's been superseded by a newer edition (2008). It's available as IEEE 1003.1-2008 or ISO/IEC 9945-2009 (for enough money) or The Open Group Single Unix Specification, version 7 (for free). All three have identical content.
Yes, that's a very old document. What you probably want is the full POSIX 2008 spec.
Since you tagged Linux in your question, I recommend that in addition to what Jerry Coffin answered, you look at the Linux pthreads man page. The current Linux pthreads implementation (NPTL) is not exactly POSIX conformant; there are small details that are handled differently.
There are also extensions like the __thread keyword (to designate per-thread variables, much simpler than pthread_key_create(); explained in detail for GCC here) that are available in Linux and other operating systems, and are likely to be included in POSIX at some point in the future.
If you are doing extremely performance-sensitive work, I recommend you also look at the atomic built-ins and vector extensions available in GCC, ICC, and Pathscale compilers (GCC vector extensions, legacy __sync builtins, C++11-style __atomic builtins, and x86-specific builtins). While these are not standardized yet, they are available in aforementioned compilers, and aside from the recent __atomic builtins, have been available for at least a decade. From personal experience, I can attest that these facilities can provide a significant performance boost (~40% in a classical molecular dynamics simulator I'm working on), while still being portable between non-Windows systems and compilers.

C: Multithreading

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.

C and POSIX Pthreads

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.

Resources