简体   繁体   English

C中的并行线程

[英]Parallel Threads in C

I have two threads in my application. 我的应用程序中有两个线程。 Is it possible to execute both the threads simultaneously without sleeping any thread? 是否可以在不休眠任何线程的情况下同时执行两个线程?

You can run the threads parallel in your application especially if they are not waiting on each other for some inputs or conditions. 您可以在应用程序中并行运行线程,尤其是在线程彼此不等待某些输入或条件的情况下。 For example: One thread may be parsing a file and other maybe playing a song in your application. 例如:一个线程可能正在解析文件,而另一个线程可能正在应用程序中播放歌曲。

Generally OS takes care of the thread time slicing. 通常,操作系统负责线程时间划分。 So at the application level it would look like these threads are running parallel but the OS does the time slicing giving each thread certain execution time. 因此,在应用程序级别上,这些线程看起来像是并行运行的,但是OS会进行时间分割,从而为每个线程提供一定的执行时间。

With multi-core processors/cores it is possible to run the threads parallel in realtime, however the OS decides which threads to run unless you specifically code at lower level to ensure which threads you want to run in parallel. 使用多核处理器/内核,可以实时并行运行线程,但是,操作系统会决定运行哪些线程,除非您专门在较低级别进行编码以确保要并行运行的线程。

As others have mentioned, with multiple cores it is possible, but, it depends on how the OS decides to distribute the threads. 正如其他人所提到的,使用多个内核是可能的,但是这取决于操作系统如何决定分配线程。 You don't have any control, that I have seen, on dictating where each thread is ran. 我已经看到,您没有控制每个线程在哪里运行的控制权。

For a really good tutorial, with some nice explanation and pictures you can look at this page, with code as to how to do multi-threading using the POSIX library. 对于一个真正好的教程,有一些很好的解释和图片,你可以看看这个页面,其代码为如何使用POSIX库做多线程。

http://www.pathcom.com/~vadco/parallel.html http://www.pathcom.com/~vadco/parallel.html

The time slice for sleep is hard to see, so your best bet is to test it out, for example, have your two threads begin to count every millisecond, and see if the two are identical. 睡眠的时间片很难看清,所以最好的选择就是测试一下,例如,让您的两个线程开始每毫秒计数,看看两个线程是否相同。 If they are not, then at least one is going to sleep by the cpu. 如果不是,那么至少有一个是要由CPU睡觉。

Most likely both will go to sleep at some time, the test is to see how much of a difference there is between the two threads. 最有可能两者都将在去一些时间睡觉,测试,看看有多少的差别有两个线程之间。

Once one thread blocks, either waiting to send data, or waiting to receive, it will be put to sleep so that other threads can run, so that the OS can continue to make certain everything is working properly. 一旦一个线程阻塞(无论是等待发送数据还是等待接收),它就会进入睡眠状态,以便其他线程可以运行,以便操作系统可以继续确保一切正常。

C does not, itself, have any means to do multi-threaded code. C本身不具有执行多线程代码的任何手段。

However, POSIX has libraries that allow you to work with threads in C. 但是,POSIX具有允许您使用C中的线程的库。

One good article about this topic is How to write multi-threaded software in C and C++ . 关于此主题的一篇不错的文章是如何用C和C ++编写多线程软件

Yes, if you have multiple processors or multi-core processors. 是的,如果您有多个处理器或多核处理器。 One thread will run in one core. 一个线程将在一个内核中运行。

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

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