views:

515

answers:

6

In my copy of C Programming Language (aka: K&R), there seems no mention of multithreading. Is the book less complete than I imagined? Did multithreading emerge after it was written? Am I thinking about this the wrong way?

Where does the concept of multithreading fit into the C world?


Edit: I think my original question have been:

  • you can write anything in C
  • multithreading exists
  • you cannot write multithreading in C <-- logical contradiction

What accounts for this contradiction? Where's the origin of multithreading? If POSIX, then what is POSIX written in if not C? A form of assembly that is inaccessible to C?

+13  A: 

C is a pretty low level language. Support for threads in a typical C program comes from the OS, not from the C runtime - if your environment doesn't support threads, then you'll have to implement them yourself, find a library that does it, or do without threads. This is in contrast to a language like Java, where the runtime environment provides many services that are guaranteed to be available to Java programs whether or not the underlying OS supports them in the manner that the Java platform exposes.

Now, having said that, I'm pretty sure that when the first edition of K&R was published, Unix did not support threads. Since C was first implemented as a systems language for the Unix environment, it's not surprising that it has no native thread support.

If you're writing code for a Unix like environment, look for POSIX threads if you need a well supported API for implementing multithreaded programs in C.

Ori Pessach
+2  A: 

The book is complete. C is capable of running threads, but only with support from the run-time it sits on. C doesn't support many things natively. For example, if you want to open a file or get input from the mouse, you'll need a library that gives you that support. This is good in a way because it means C can run on a small embedded computer and doesn't need lots of memory for features you may or may not want.

Multithreading was around way before C. ( That's according to this: http://www.cs.clemson.edu/~mark/multithreading.html )

You need a threading library. Eg, on windows you can:

#include "Windows.h"

int main()
{
   CreateThread(/*Google the function for details of the parameters.*/);   
   return 0;
}

You'll need to download the windows platform sdk to do that. Most platforms have some kind of sdk that'll have a library with some functions for creating threads. Most have a CreateThread style function, where you pass in the address of a function you want the newly started thread to begin running in parallel to your current thread that began on the main function.

A standardized threading library you might want to look up is posix.

Scott Langham
+2  A: 

If I remember correctly, multithreading actually came into common use much later than the C programming language. The POSIX Threads library is the typical way to do multithreading in a Unix/Linux program, and is not part of the standard library.

Rick Copeland
The wonderful thing about standards is that there are so many to choose from. Threads are not part of ISO C (ISO/IEC 9899:1999); POSIX threads are part of POSIX (ISO/IEC 9945-1:2003 - though the POSIX standard is IEEE 1003.1-2008).
Jonathan Leffler
+1  A: 

Multithreading (or multiprocessing) surely emerged before that. However, multithreading support in a programming language is scarce even now, particularly C has none. So I think you should read a book on eg. POSIX threads, or whatever thread support your environment gives you (the threading libraries are very similar to each other nowadays, at least in the principles of their synchronization primitives; strange things like RCU are only used in specific environments).

jpalecek
+1  A: 

It does mention it on my copy (2nd edition), in the Introduction chapter (p. 2):

Similarly, C offers only straightforward, single-thread control flow: tests, loops, grouping, and subprograms, but not multiprogramming, parallel operations, synchronization, or coroutines.

+2  A: 

The C and C++ languages did not include built-in threading libraries. Hence, different platforms had different paradigms wrt threads (PThreads, the WinAPI CreateThread(..) function, MFC threads, etc).

C++0x will include a standard thread library, it seems.

Garrett