是不同偏移量所需的互斥量到分配的堆内存中 [英] Is mutex needed for different offsets into allocated heap memory

查看:204
本文介绍了是不同偏移量所需的互斥量到分配的堆内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在框架中放置一个将生成二进制数据表的工具。我计划使这个多线程充分利用我的24核心。 (我估计,在单线程中,生成数据的挂墙时间将约为50天)。我以前使用服务器/客户端设计与套接字通信,因为我需要分布在多个机器上这样做。

I am laying in the framework for a tool that will generate a binary data table. I a plan on making this multithreaded to take full advantage of the 24 cores at my disposal. (I am estimating that the wall time for generation of the data will be about 50 days–in a single thread.). I have done this in the past using server/client design with socket communication as I needed to distributed this across multiple machines.

这次,我正在寻找一个单一的机器/多线程方法,我试图找出如何做到这一点正确的方法。

This time around, I am looking at a single machine/multi-threaded approach and am trying to figure out how to do this the right way.

主线程将处理任务到每个子线程的分配,并确定到分配的内存中的偏移量。

The master thread will handle the assignment of tasks to each child thread and determining the offset into the allocated memory.

每个线程将写入分配的内存中的唯一地址范围。因为这些块永远不会在记录之间重叠,所以没有两个线程将尝试写入相同的偏移量。

Each thread will write to a unique address range within the allocated memory. Because these blocks will never overlap between records, no two threads will ever attempt to write the same offset.

p>

void computeRecord(void *taskInput)
{
  struct TaskData *taskData = (TaskData *)(taskInput);

  RecordData data; 
  // A huge long computation block to populate data
  //   (4-5 second run time)

  long record_id = taskData->record_id;
  char *buffer   = taskData->start_buffer;

  // mutex lock needed here ??

  int n_bytes = sizeof(RecordData)
  memcpy( (char *)(buffer+record_id*n_bytes), (char *)(&recordData) n_bytes);

  // mutex unlock here ?
}

长设置。简短的问题。在这种情况下是否需要互斥?

Long setup. Short question. Is the mutex necessary in this case?

推荐答案

为了获得最佳性能,您需要将数据对齐到缓存行 - 这将避免不同的CPU内核之间相互弹跳缓存行。

For best performance, you'll want your data to be aligned to cache-lines - this will avoid the different CPU cores from "bouncing" cache-lines between each other.

但是无论如何,只要我们谈论独立地交互的单独字节,就不需要保护。只有当多个线程访问同一个字节[当然,当访问多个字节时也适用]。

But regardless of that, as long as we are talking separate bytes that are interacted on independently, no protection is needed. Only if more than one thread is accessing the very same byte [also applies when accessing multiple bytes, of course].

编辑:这个语句当然只有在处理器有字节寻址时才是真的。处理器,不是阿尔法,但可能有其他人。
(编辑2:不,在C ++ 11兼容的编译器无关紧要,它取决于编译器以线程安全的方式处理字节寻址)

This statement is of course only true if the processor has byte addressing. The processor that comes to mind that doesn't is Alpha, but there may be others. ( No, doesn't matter in C++11 compliant compiler, it's up to the compiler to deal with byte addressing in a thread-safe manner)

这篇关于是不同偏移量所需的互斥量到分配的堆内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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