简体   繁体   English

C多线程起源

[英]C multi-threading origin

In my copy of C Programming Language (aka: K&R), there seems no mention of multithreading. 在我的C编程语言 (又名:K&R)中,似乎没有提到多线程。 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? 多线程的概念在哪里适合C世界?


Edit: I think my original question have been: 编辑:我认为我原来的问题是:

  • you can write anything in C 你可以用C写任何东西
  • multithreading exists 多线程存在
  • you cannot write multithreading in C <-- logical contradiction 你不能在C < - 逻辑矛盾中写多线程

What accounts for this contradiction? 这个矛盾是什么原因造成的? Where's the origin of multithreading? 多线程的起源在哪里? If POSIX, then what is POSIX written in if not C? 如果是POSIX,那么如果不是C,那么POSIX写的是什么? A form of assembly that is inaccessible to C? C无法访问的一种装配形式?

C is a pretty low level language. C是一种非常低级的语言。 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. 对典型C程序中的线程的支持来自操作系统,而不是来自C运行时 - 如果您的环境不支持线程,那么您必须自己实现它们,找到一个执行它的库,或者没有线程。 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. 这与Java之类的语言形成对比,在Java语言中,运行时环境提供了许多保证可供Java程序使用的服务,无论底层操作系统是否以Java平台公开的方式支持它们。

Now, having said that, I'm pretty sure that when the first edition of K&R was published, Unix did not support threads. 现在,说了这些,我很确定在第一版K&R发布时,Unix不支持线程。 Since C was first implemented as a systems language for the Unix environment, it's not surprising that it has no native thread support. 由于C最初是作为Unix环境的系统语言实现的,因此它没有本机线程支持也就不足为奇了。

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. 如果您正在为类似Unix的环境编写代码,那么在需要一个支持良好的API以在C中实现多线程程序时,请查找POSIX线程。

The book is complete. 这本书很完整。 C is capable of running threads, but only with support from the run-time it sits on. C能够运行线程,但只能在它所处的运行时支持。 C doesn't support many things natively. C本身不支持很多东西。 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. 这在某种程度上是好的,因为它意味着C可以在小型嵌入式计算机上运行,​​并且不需要大量内存来处理您可能想要或可能不想要的功能。

Multithreading was around way before C. ( That's according to this: http://www.cs.clemson.edu/~mark/multithreading.html ) 多线程在C之前就已经存在了(根据这个: http//www.cs.clemson.edu/~mark/multithreading.html

You need a threading library. 你需要一个线程库。 Eg, on windows you can: 例如,在Windows上,您可以:

#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. 你需要下载windows平台sdk才能做到这一点。 Most platforms have some kind of sdk that'll have a library with some functions for creating threads. 大多数平台都有某种sdk,它有一个带有一些创建线程函数的库。 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. 大多数都有一个CreateThread样式函数,您可以在其中传入一个函数的地址,您希望新启动的线程与您在main函数上开始的当前线程并行运行。

A standardized threading library you might want to look up is posix. 您可能想要查找的标准化线程库是posix。

If I remember correctly, multithreading actually came into common use much later than the C programming language. 如果我没记错的话,多线程实际上比C编程语言要晚得多。 The POSIX Threads library is the typical way to do multithreading in a Unix/Linux program, and is not part of the standard library. POSIX Threads库是在Unix / Linux程序中执行多线程处理的典型方法,不属于标准库。

The C and C++ languages did not include built-in threading libraries. C和C ++语言不包含内置的线程库。 Hence, different platforms had different paradigms wrt threads (PThreads, the WinAPI CreateThread(..) function, MFC threads, etc). 因此,不同的平台具有不同的线程(PThreads,WinAPI CreateThread(..)函数,MFC线程等)。

C++0x will include a standard thread library , it seems. 似乎C ++ 0x 将包含一个标准的线程库

Multithreading (or multiprocessing) surely emerged before that. 多线程(或多处理)肯定在此之前出现。 However, multithreading support in a programming language is scarce even now, particularly C has none. 然而,即使现在,编程语言中的多线程支持也很少,特别是C没有。 So I think you should read a book on eg. 所以我认为你应该读一本关于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). POSIX线程,或者你的环境为你提供的任何线程支持(线程库现在彼此非常相似,至少在它们的同步原语的原则中;像RCU这样的奇怪的事情只在特定的环境中使用)。

It does mention it on my copy (2nd edition), in the Introduction chapter (p. 2): 它确实在我的副本(第2版),引言章节(第2页)中提到它:

Similarly, C offers only straightforward, single-thread control flow: tests, loops, grouping, and subprograms, but not multiprogramming, parallel operations, synchronization, or coroutines. 类似地,C仅提供简单的单线程控制流程:测试,循环,分组和子程序,但不提供多道程序设计,并行操作,同步或协同程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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