C多线程性能问题

标签 c linux multithreading matrix

我正在编写一个多线程程序来遍历一个n x n矩阵,其中主对角线中的元素以并行方式处理,如下面的代码所示:

int main(int argc, char * argv[] )
{   
  /* VARIABLES INITIALIZATION HERE */

  gettimeofday(&start_t, NULL); //start timing
  for (int slice = 0; slice < 2 * n - 1; ++slice)
  {  
    z = slice < n ? 0 : slice - n + 1;
    int L = 0;
    pthread_t threads[slice-z-z+1];
    struct thread_data td[slice-z-z+1];

    for (int j=z; j<=slice-z; ++j)
    {
      td[L].index= L;
      printf("create:%d\n", L );
      pthread_create(&threads[L],NULL,mult_thread,(void *)&td[L]);
      L++;
    }

    for (int j=0; j<L; j++) 
    {
      pthread_join(threads[j],NULL);
    }
  }     

  gettimeofday(&end_t, NULL); 
  printf("Total time taken by CPU: %ld \n", ( (end_t.tv_sec - start_t.tv_sec)*1000000 + end_t.tv_usec - start_t.tv_usec));

  return (0);
}

void *mult_thread(void *t)
{      
  struct thread_data *my_data= (struct thread_data*) t;

  /* SOME ADDITIONAL CODE LINES HERE */ 

  printf("ThreadFunction:%d\n", (*my_data).index );

  return (NULL);
}

问题是,与串行(朴素)实现相比,这种多线程实现给我带来了非常糟糕的性能。

是否可以进行一些调整来提高多线程版本的性能?

最佳答案

线程池可能会让事情变得更好。

定义一个新的结构类型如下。

typedef struct {
    struct thread_data * data;
    int status; // 0: ready 
                // 1: adding data 
                // 2: data handling, 3: done
    int next_free;
} thread_node;

初始化:

size_t thread_size = 8;
thread_node * nodes = (thread_node *)malloc(thread_size * sizeof(thread_node));
for(int i = 0 ; i < thread_size - 1 ; i++ ) {
    nodes[i].next_free = i + 1;
    nodes[i].status = 0 ; 
}
nodes[thread_size - 1].next_free = -1;
int current_free_node = 0 ;
pthread_mutex_t mutex;

获取线程:

int alloc() {
    pthread_mutex_lock(&mutex);
    int rt = current_free_node;
    if(current_free_node != -1) {
        current_free_node = nodes[current_free_node].next_free;
        nodes[rt].status = 1;
    }
    pthread_mutex_unlock(&mutex);
    return rt;
}

返回线程:

void back(int idx) {
    pthread_mutex_lock(&mutex);
    nodes[idx].next_free = current_free_node;
    current_free_node = idx;
    nodes[idx].status = 0;
    pthread_mutex_unlock(&mutex);
}

先创建线程,然后使用alloc()尝试获取空闲线程,更新指针。

  • 不要使用 join 来判断状态。
  • 将您的 mult_thread 修改为循环,并在作业完成后,将您的状态更改为 3
  • 对于线程中的每个循环,您可以给它更多的工作

希望能给你带来一些帮助。

------------ 更新于 2015 年 4 月 23 日 ----------------------------------

here就是一个例子。

使用命令编译并运行 $ g++ thread_pool.cc -o tp -pthread --std=c++

yu:thread_pool yu$ g++ tp.cc -o tp  -pthread --std=c++11 && ./tp
1227135.147 1227176.546 1227217.944 1227259.340...
time cost 1 : 1068.339091 ms
1227135.147 1227176.546 1227217.944 1227259.340...
time cost 2 : 548.221607 ms

您还可以删除计时器,它也可以编译为 std c99 文件。

当前线程大小已限制为2,您也可以调整参数thread_size,然后重新编译运行。更多的线程可能会给你带来更多优势(在我的电脑中,如果我将线程大小更改为 4,任务将在 280 毫秒内完成),而如果你没有足够的 cpu 线程,太多的线程数可能不会对你有太大帮助。

关于C多线程性能问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29736655/

相关文章:

c++ - strncpy() 是 memcpy() 的特例吗?

linux - 使用 tar 将 Linux 主目录的内容归档

linux - TCP 重传计时器覆盖/杀死 TCP 保活计时器,延迟断开连接发现

java - HashTable 中的 ArrayList 是线程安全的

c - 如何使用 Struct 启动函数?

c - 如何影响 C 中结构的相同副本?

c - 注意: previous implicit declaration of ‘point_forward’ was here

linux - Linux 上的 inotifywait 是否允许在超时期限内收集事件?

multithreading - Lwt.async() 未按预期工作

c++ - SignalObjectAndWait 考虑有 SetEvent 和 WaitForSingleObject 的目的是什么?