c++ - C : performance of pthread, 低于单线程

标签 c++ vector pthreads

我对我的代码的性能感到困惑,在处理单线程时它只使用 13 秒,但它会消耗 80 秒。我不知道 vector 一次是否只能由一个线程访问,如果是这样,我可能必须使用结构数组而不是 vector 来存储数据,有人可以帮忙吗?

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <iterator>
#include <string>
#include <ctime>
#include <bangdb/database.h>
#include "SEQ.h"

#define NUM_THREADS 16

using namespace std;


typedef struct _thread_data_t {
    std::vector<FDT> *Query;
    unsigned long start;
    unsigned long end;
    connection* conn;
    int thread;
} thread_data_t;



void *thr_func(void *arg) {

    thread_data_t *data = (thread_data_t *)arg;
    std::vector<FDT> *Query = data->Query;
    unsigned long start = data->start;
    unsigned long end = data->end;
    connection* conn = data->conn;

    printf("thread %d started %lu -> %lu\n", data->thread, start, end);

    for (unsigned long i=start;i<=end ;i++ )
    {
        FDT *fout = conn->get(&((*Query).at(i)));
        if (fout == NULL)
        {
            //printf("%s\tNULL\n", s);

        }
        else
        {
            printf("Thread:%d\t%s\n", data->thread, fout->data);
        }
    }

    pthread_exit(NULL);
}


int main(int argc, char *argv[])
{

    if (argc<2)
    {
        printf("USAGE: ./seq <.txt>\n");
        printf("/home/rd/SCRIPTs/12X18610_L5_I052.R1.clean.code.seq\n");

        exit(-1);
    }
    printf("%s\n", argv[1]);

    vector<FDT> Query;

    FILE* fpin;
    if((fpin=fopen(argv[1],"r"))==NULL)  {
        printf("Can't open Input file %s\n", argv[1]);
        return -1; 
    }

    char *key = (char *)malloc(36);

    while (fscanf(fpin, "%s", key) != EOF)
    {
        SEQ * sequence = new SEQ(key);

        FDT *fk = new FDT( (void*)sequence, sizeof(*sequence) );

        Query.push_back(*fk);
    }

    unsigned long Querysize = (unsigned long)(Query.size());
    std::cout << "myvector stores " << Querysize << " numbers.\n";



    //create database, table and connection
    database* db = new database((char*)"berrydb");

    //get a table, a new one or existing one, walog tells if log is on or off
    table* tbl = db->gettable((char*)"hg19", JUSTOPEN);

    if(tbl == NULL)
    {
        printf("ERROR:table NULL error");
        exit(-1);
    }

    //get a new connection
    connection* conn = tbl->getconnection();
    if(conn == NULL)
    {
        printf("ERROR:connection NULL error");
        exit(-1);
    }

    cerr<<"begin querying...\n";


    time_t begin, end;
    double duration;
    begin = clock();




    unsigned long ThreadDealSize = Querysize/NUM_THREADS;
    cerr<<"Querysize:"<<ThreadDealSize<<endl;



    pthread_t thr[NUM_THREADS];
    int rc;

    thread_data_t thr_data[NUM_THREADS];

    for (int i=0;i<NUM_THREADS ;i++ )
    {
        unsigned long ThreadDealStart = ThreadDealSize*i;
        unsigned long ThreadDealEnd   = ThreadDealSize*(i+1) - 1;

        if (i == (NUM_THREADS-1) )
        {
            ThreadDealEnd = Querysize-1;
        }

        thr_data[i].conn = conn;
        thr_data[i].Query = &Query;
        thr_data[i].start = ThreadDealStart;
        thr_data[i].end = ThreadDealEnd;
        thr_data[i].thread = i;
    }


    for (int i=0;i<NUM_THREADS ;i++ )
    {
        if (rc = pthread_create(&thr[i], NULL, thr_func, &thr_data[i]))
        {
          fprintf(stderr, "error: pthread_create, rc: %d\n", rc);
          return EXIT_FAILURE;
        }
    }


    for (int i = 0; i < NUM_THREADS; ++i) {
        pthread_join(thr[i], NULL);
    }


    cerr<<"done\n"<<endl;
    end = clock();
    duration = double(end - begin) / CLOCKS_PER_SEC;
    cerr << "runtime:   " << duration << "\n" << endl;

    db->closedatabase(OPTIMISTIC);
    delete db;
    printf("Done\n");


  return EXIT_SUCCESS;
}

最佳答案

与标准库中的所有数据结构一样,vector 的方法是可重入的,但不是线程安全的。这意味着不同的实例可以被多个线程独立访问,但是每个实例一次只能被一个线程访问,你必须确保这一点。但是因为每个线程都有单独的 vector ,所以这不是你的问题。

您的问题可能是 printfprintf 是线程安全的,这意味着您可以同时从任意数量的线程调用它,但代价是在内部被互斥包裹。

程序线程部分的大部分工作都在 printf 中完成。所以可能发生的情况是所有线程都已启动并快速到达 printf,除第一个线程外的所有线程都将停止。当 printf 完成并释放互斥量时,系统会考虑调度等待它的线程。它可能确实如此,因此发生相当缓慢的上下文切换。并在每个 printf 之后重复。

具体如何发生取决于实际使用的锁定原语,这取决于您的操作系统和标准库版本。系统应该每次只唤醒下一个 sleep 者,但许多实现实际上唤醒了所有 sleep 者。因此,除了 printf 主要以循环方式执行,为每个执行一个上下文切换之外,可能还有很多额外的虚假唤醒,其中线程刚刚发现锁是举行并重新休眠。

因此,从中得出的教训是,线程不会自动使事情变快。他们只在以下情况下提供帮助:

  • 线程将大部分时间花在阻塞系统调用上。在网络服务器之类的东西中,线程等待来自套接字的数据,而不是等待来自磁盘的响应数据,最后等待网络接受响应。在这种情况下,拥有多个线程会有所帮助,只要它们大多是独立的。
  • 线程的数量与 CPU 线程的数量一样多。目前通常的数字是 4(四核或带超线程的双核)。更多线程无法在物理上并行运行,因此它们不会带来任何 yield 并会产生一些开销。因此,16 个线程有点矫枉过正。

而且当他们都操作相同的对象时,他们永远不会提供帮助,所以他们最终大部分时间都在等待锁。除了您自己锁定的任何对象之外,请记住输入和输出文件句柄也必须在内部锁定。

内存分配还需要在线程之间进行内部同步,但是现代分配器为线程提供了单独的池以避免其中的大部分;如果默认分配器被证明对许多线程来说太慢,您可以使用一些专门的分配器。

关于c++ - C : performance of pthread, 低于单线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13874117/

相关文章:

c++ - 这个 RAII 风格的 Objective-C 类可以工作吗?

r - 获取具有非唯一名称的命名向量值

pthreads - 程序中止或退出时 pthread_mutex_t* 是否释放?

c++ - 如何在干净的代码中向后遍历双向迭代器?

不同 block 作用域中的C++变量具有相同的地址

c++ - 如何从 Windows XP 中的管理员进程中获取有关已登录用户的语言环境信息?

c++ - 从另一个 vector 搜索和查找一个 vector 中的元素的有效方法?

C++ STL vector 迭代器...但出现运行时错误

c++ - CMake 链接错误 pthread:启用多线程以使用 std::thread:不允许操作

c - 使用 pthread_create 停止我的方法工作。为什么?使用 C