C语言中的并行线程 [英] Parallel Threads in C

查看:130
本文介绍了C语言中的并行线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有两个线程.是否可以在不休眠任何线程的情况下同时执行两个线程?

I have two threads in my application. Is it possible to execute both the threads simultaneously without sleeping any thread?

推荐答案

正如其他人所提到的,使用多个内核是可能的,但这取决于操作系统决定如何分配线程的能力.我已经看到,您没有控制每个线程在哪里运行的控制权.

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.

对于一个非常好的教程,您可以在此页面上找到一些不错的解释和图片,以及有关如何使用POSIX库进行多线程处理的代码.

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.

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

很难看到睡眠的时间片,因此最好的选择是进行测试,例如,让您的两个线程开始每毫秒计数,看看两个线程是否相同.如果不是,那么CPU至少要睡觉.

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.

大多数人都可能会在某个时间入睡,测试是看两个线程之间有多大差异.

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语言中的并行线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆