在线程

时间:2017-06-28 19:35:16

标签: c++ multithreading

我试图在安全的线程中将tempFile的内容复制到CacheFile 有效的方式。

什么是myscript需要1-2分钟才能写入文件(即tempFile)和sucessfull。我需要复制到cacheFile所以谁调用该函数(getStudentDetails)将从cachefile而不是tempFile获取服务避免延迟(虽然第一次调用总是会延迟1-2分钟,因为cacheFIle会清空)。

注意:我需要使用文件而不是主要存储

以下是我的方法,它工作正常,但它可以更有效和安全吗?

bool unlock=true;

pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  condition_var   = PTHREAD_COND_INITIALIZER;


    void * runScriptAndUpdateCacheFile (  void * arg  )
{

    std::cout<<"Inside thread lock"<<endl;

    unlock=false;  // to avoid multiple call to this thread  till it get complete 
    char tempFile[32]={0};
    strncpy(tempFile,"/tmp/smbXXXXXX",14); // tempFile is the main file where my script will dump the output


    if( mkstemp(tempFile) < 1)
    {
        cout<<"fail to create temp file"<<endl;
        return (NULL);

    }

    if (tempFile != NULL)
    {
        char command[256] = {0};
        sprintf(command, " myscript > %s", tempFile); 
        int status=system(command);  // write ouput of script to tempFile
        if (status < 0)
        {

            std::cout << "Error: " << strerror(errno) << '\n';
            return(NULL);
        }
        else
        {
            if (WIFEXITED(status))  // fetch exit code
               {
                std::cout << "Program returned normally, exit code " << WEXITSTATUS(status) << '\n';
                std::ifstream  src(tempFile, std::ios::binary);
                    std::ofstream  CacheFile((char*)arg, std::ios::binary); 
                                 Cache << src.rdbuf();  // Copy to cacheFile
               }
            else
            {
                std::cout << "Program exited abnormaly\n";
                return(NULL);
            }
        }
    }
cout<<" thread is unlock"<<endl;
unlock=true;

}

可以多次调用getStudentDetails

void getStudentDetails()
{
    pthread_t threads;

        char CacheFile[32]={0};

        strncpy(CacheFile,"/tmp/CacheFile",19);
       //Serve from CacheFile rather than orignal file coz this file
       //will take 1-2 min to dump in it
    std::ifstream fin(CacheFile);

     if(unlock)  // to avoid multiple call to thread when multile call made to getStudentDetails
                  // this unlock is made false when it enter to thread and made true when exit
      {
    pthread_mutex_lock( &count_mutex );
        int rc = pthread_create(&threads, NULL, runSmbtree, CacheFile); // pass cache file to thread  this file will get updated with 
                                    // orignal file create by script.
       pthread_mutex_unlock( &count_mutex );
      }
        std::string line;

        while (getline(fin, line))
        {

            // reading Cachefile and serveing when getStudentDetails called by client
        }
}

Valgrind抱怨以下错误

  1. = 3535 == HEAP SUMMARY:

    == 3535 ==在退出时使用:3个块中的864个字节

    == 3535 ==总堆使用量:169个分配,166个释放,78,323个字节分配

    == 3535 ==

    == 3535 ==搜索指向3个未释放块的指针

    == 3535 ==检查了25,373,616字节

    == 3535 ==

    == 3535 ==主题1:

    == 3535 == 3个块中的864个字节可能在丢失记录1中丢失1个

    == 3535 ==在0x4C2CC70:calloc(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so中)

    == 3535 == by 0x4012E54:allocate_dtv(dl-tls.c:296)

    == 3535 == by 0x4012E54:_dl_allocate_tls(dl-tls.c:460)

    == 3535 == by 0x5359DA0:allocate_stack(allocatestack.c:589)

    == 3535 == by 0x5359DA0:pthread_create @@ GLIBC_2.2.5(pthread_create.c:500)

    == 3535 ==

    == 3535 ==排除摘要:

    == 3535 ==绝对丢失:0个块中的0个字节

    == 3535 ==间接丢失:0个块中的0个字节

    == 3535 ==可能丢失:3个块中的864个字节

    == 3535 ==仍然可以访问:0个块中的0个字节

    == 3535 ==抑制:0个块中的0个字节

    == 3535 ==

    == 3535 ==错误摘要:来自2个上下文的3个错误(被抑制:0从0开始)

    == 3535 ==

    == 3535 ==上下文1中的2个错误:

    == 3535 ==主题2:

    == 3535 == Syscall param open(filename)指向不可追踪的字节

    == 3535 ==在0x565B4CD:??? (系统调用-template.S:81)

    == 3535 == by 0x55E9E07:_IO_file_open(fileops.c:228)

    == 3535 == by 0x55E9E07:_IO_file_fopen @@ GLIBC_2.2.5(fileops.c:333)

    == 3535 == by 0x55DE2E3:__ fopen_internal(iofopen.c:90)

    == 3535 == by 0x4EB29BF:std :: __ basic_file :: open(char const *,std :: _ Ios_Openmode,int)(在

    / usr / lib中/ x86_64的-Linux的GNU /的libstdc ++。so.6.0.19)

    == 3535 == by 0x4EEAEA9:std :: basic_filebuf&gt; :: open(char const *,std :: _ Ios_Openmode)(在

    / usr / lib中/ x86_64的-Linux的GNU /的libstdc ++。so.6.0.19)

    == 3535 == by 0x4EEC747:std :: basic_ofstream&gt; :: basic_ofstream(char const *,

    std :: _ Ios_Openmode)(在

    / usr / lib中/ x86_64的-Linux的GNU /的libstdc ++。so.6.0.19)

    == 3535 == by 0x40200B:runScriptAndUpdateCacheFile(void *)(in ***)

    == 3535 == by 0x5359181:start_thread(pthread_create.c:312)

    == 3535 == by 0x566A30C:clone(clone.S:111)

    == 3535 ==地址0xffefffba0在线程1的堆栈上

    == 3535 ==堆栈指针下方384个字节

    == 3535 ==

    == 3535 ==错误摘要:来自2个上下文的3个错误(被抑制:0从0开始)

0 个答案:

没有答案