There are how many ways to lock in c - 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.)

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.

A POSIX compliant OS usually extends an existing implementation of the C Standard Library?

One of the Units of Functionality that POSIX states an OS needs to provide to be POSIX compliant is POSIX_C_LANG_SUPPORT. Basically this is the whole C Standard Library with some more things.
My question is simple: developers of POSIX compliant OSes usually just download an open source version of C Standard Library (e.g. glib or uClibc) and adapt it to fit POSIX or they implement everything from scratch? Is there any advantage in rewriting the C Library instead of just picking one of the very known implementations and adjust it to my needs?
Really, it is done in the inverse way.
We have different Unix versions: two main families: SystemV and BSD, different manufacturers, and so there was a need to standardize. US government wanted also standardized programs, so POSIX (version 1) was created, by standardizing OS interfaces (a step further than just C standard).
Windows NT is also POSIX (version 1) compatible, just because government wanted standardized tools. So POSIX was designed very very broad.
Then with time, there were need to standardize some more Unix (and similar) systems. Not as just one system, one API, but as common API, and so programs (e.g. GUI libraries or databases) could eventually use extension, but also make sure that program that follow the standard works on compatible system.
This was SUS (Single Unix Specification). This required a UNIX like system (unlike POSIX 1).
Then POSIX became not so important: application that in theory could work on all POSIX systems didn't really work on POSIX Windows.
So the new version of POSIX merged old POSIX plus SUS plus new useful function missing in SUS.
Now Linux is important, so Linux implementations (e.g. glibc) is taken into account when updating POSIX. You will see in the mailing list, that POSIX is defined by "vendors" of different Unix and similar systems.
So, it is not that operating systems extend POSIX, it is just that POSIX takes the most useful and standard options from different OS. It creates new interfaces just when existing interfaces are so incompatible, that by standardizing, it will break existing programs.
For the "second" question: when you develop a new operating system, you choose what way to go. Usually it is just derivation and fork (and distributions): again from the two Unix families, of just deriving Linuxes from RedHat or Debian). Sometime system is build from scratch, because of the design. Kernel provides most of system calls, so e.g. glibc needs a lot of systemcall (given by kernel) implemented in a similar way as POSIX. Glibc is not complete. Note: early Linux distributions used other libraries. GLibc was also written from scratch.
Well, we are all dwarves standing on the shoulders of giants.
Writing a new OS is a huge undertaking, so the wise one will re-use whatever (design, libraries, compilers, other software) they can. It's still in all likelyhood far too much work, so why make it even harder by rewriting everything from scratch?

Concurrent programing in plain C - Windows / Linux

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

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.

Does the C language have support for multi-threading?

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.

Resources