简体   繁体   中英

C multi-threading origin

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?

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.

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.

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM