简体   繁体   English

在C ++中使用std :: thread进行线程化

[英]Threading in C++ using std::thread

I'd like to know if I initialize four std::thread 's, will this crash a dual thread cpu or are std::thread 'sa virtual thread kind of thing. 我想知道是否初始化四个std :: thread,这会使双线程cpu崩溃还是std :: thread是一种虚拟线程?

I mainly want to know because a program I am writing for class would go much faster with four threads, but I have no idea how many threads my grader has on their CPU. 我主要想知道,因为我为类编写的程序在四个线程的情况下运行得更快,但是我不知道我的评分器在其CPU上有多少个线程。 Mine has 8 but hers could easily have 2. I don't want to crash her system, so any help would be 我的有8个,但她的很容易有2个。我不想让她的系统崩溃,所以任何帮助都可以

I'm fairly new to Computer Science so I welcome any help I can get. 我是计算机科学的新手,所以我欢迎能获得的任何帮助。

The number of threads doesn't have to be limited to the number of cores. 线程数不必限于核心数。 The OS will just schedule the threads as cores become available. 当内核可用时,操作系统只会调度线程。

That said, you can use hardware_concurrency() to find you how many cores/processors are available, and base the number of threads you use on that. 也就是说,您可以使用hardware_concurrency()查找可用的内核/处理器数量,并以此为基础使用线程数。

... if I initialize four std::thread 's, will this crash a dual thread cpu ...如果我初始化四个std :: thread,这将导致双线程CPU崩溃

I suspect you are asking about threads vs cores ... threads are independent of core's. 我怀疑您是在问线程与内核的关系……线程与内核无关。 A single core cpu can run many threads. 单个核心cpu可以运行多个线程。

  • N threads, where N > core-count, will not crash a Linux system. N个线程,其中N> core-count,不会使Linux系统崩溃。

Linux provides a special file. Linux提供了一个特殊文件。 I use a home grown version of grep in my C++ programs to read and count how many lines of this special file have the string "processor" in them. 我在C ++程序中使用grep的本地版本,以读取并计算该特殊文件的多少行中包含字符串“ processor”。

//       The special file of Ubuntu -- vvvvvvvvvvvvv
size_t retVal = DTB::grep(std::string("/proc/cpuinfo"),
                          std::string("processor"),  // line count of "processor" is core count
                         nullDev); // dev/null -- no output

This invocation returns the count of processors. 该调用返回处理器的数量。 My old Dell has 2. 我的老戴尔有2。


The first few lines of the file look like: 文件的前几行如下所示:

$ cat /proc/cpuinfo
processor   : 0
vendor_id   : AuthenticAMD
cpu family  : 15
model       : 75
model name  : AMD Athlon(tm) 64 X2 Dual Core Processor 5000+
stepping    : 2
cpu MHz     : 1000.000
cache size  : 512 KB

The command top shows I have > 178 tasks in state running or sleeping, a lot more than 2. 命令顶部显示我有> 178个处于运行或睡眠状态的任务,远远超过2个。


would go much faster with four threads, 四个线程会更快

I suspect it would, but this is not guaranteed. 我怀疑会,但是不能保证。 This is yet another test the developer must perform to confirm your guess. 这是开发人员必须执行的另一项测试,以确认您的猜测。 When the threads interact with each other (semaphores? messaging?) it becomes increasingly unlikely that you (or I) can predict the results. 当线程彼此交互(信号量?消息传递?)时,您(或我)就无法预测结果了。


So its not worth it to make it quad threaded? 因此,使它成为四线程是不值得的吗?

I think you should measure the performance of both forms of your code on your machine. 我认为您应该衡量计算机上两种形式的代码的性能。 If 4 threads are quicker, it is not your fault that the other machine is slower. 如果4个线程速度较快,则另一台计算机速度较慢不是您的错。

I recommend planning on recording duration times ... perhaps make it part of the output. 我建议计划记录持续时间...也许将其作为输出的一部分。

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

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