62. Multi-threaded multiple file retrieval, each thread retrieves a file that is used in case of insufficient memory

Source: Internet
Author: User

1 #define_crt_secure_no_warnings2#include <stdio.h>3#include <stdlib.h>4#include <string.h>5#include <memory.h>6#include <process.h>7#include <Windows.h>8 //multi-threaded multiple files for thread scheduling9 //Freeing MemoryTen //in case of insufficient memory, queue up to complete the task One  A //To create a multithreaded fabric body - structinfos - { the     //Path -     Charpath[ the]; -     //Thread ID -     intID; +     //load the contents of the file into -     Char**g_pp; +     //Length of File A     intlength; at     //the data to query -     Charfindstr[ -]; -}myinfo[ A] = {0};//22 Structures Save 22 files of information -HANDLE inithd[ A] = {0};//22 Initialize thread address -HANDLE findhd[ A] = {0};//22 Find thread addresses -  in //initializes the thread address according to the path and writes the data to the infos struct - voidRunthreadinit (void*p) to { +     structInfos *pinfo =p; -FILE *PF = fopen (Pinfo->path,"R"); the     if(pf!=NULL) *     { $         //test how many rowsPanax Notoginseng         inti =0; -          while(!feof (PF)) the         { +             Charstr[ the] = {0 }; AFgets (str, the, PF);//Read thei++; +         } -         //I record the number of rows $Rewind (PF);//back to the beginning fseek (pf,0,seek_set); $PINFO-&GT;G_PP = Calloc (i,sizeof(Char*));//Allocating memory Initialization -Pinfo->length = i;//record length -  the         //Read and copy data to G_pp[i] -          for(intj =0; J < i;j++)Wuyi         { the             Charstr[ the] = {0 }; -             //Read WuFgets (str, the, pf); -             //Get length About             intLength =strlen (str); $             //Allocating Memory -PINFO-&GT;G_PP[J] = calloc (length +1,sizeof(Char)); -             //Copy to **g_pp -             if(pinfo->g_pp[j]!=NULL) A             { +strcpy (pinfo->g_pp[j], str); the             } -         } $     }  the     //Close File the fclose (PF); theprintf"thread%d init over\n", pinfo->ID); the } -  in //Calling thread functions the voidRunthreadsearch (void*p) the { About     //pointer type conversions the     structInfos *pinfo =p; the     //each thread is retrieved accordingly. the      for(inti =0; I < pinfo->length;i++) +     { -         if(pinfo->g_pp[i]!=NULL) the         {Bayi             Char*PX = Strstr (Pinfo->g_pp[i], pinfo->findstr); the             if(px!=NULL) the             { -printf"\n%s", pinfo->g_pp[i]); -             } the         } the     } the  theprintf"thread%d find over\n", pinfo->ID); - } the  the //Freeing Memory the voidFreeall (structInfos *pinfo)94 { theprintf"Freeall Start"); the     //releases the pointer array for each pointer to the memory the      for(inti =0; I < pinfo->length;i++)98     { AboutFree (pinfo->g_pp[i]); -     }101Free (PINFO-&GT;G_PP);//Release G_pp102 103printf"Freeall end;");104  the }106 107 voidMain ()108 {109     //initializes the address and string to be queried for the thread struct the      for(inti =0; I < the; i++)111     { theMyinfo[i].id = i+1;113sprintf (Myinfo[i].path,"Dangdangwang%d.txt", i +1); thestrcpy (MYINFO[I].FINDSTR,"Xiao Wang"); the     } the 117     //initialize struct based on address118      for(inti =0; I < the; i++)119     { -Inithd[i] = _beginthread (Runthreadinit,0, &myinfo[i]);121     }122     //wait123WaitForMultipleObjects ( the, INITHD, TRUE, INFINITE);124System"Pause"); the 126     //executing a lookup thread127      for(inti =0; I < the; i++) -     {129Findhd[i] = _beginthread (Runthreadsearch,0, &myinfo[i]); the     }    131     //wait theWaitForMultipleObjects ( the, FINDHD, TRUE, INFINITE);133System"Pause");134 135     //Free All Memory136printf"Start Releasing");137      for(inti =0; I < the; i++)138     {139Freeall (&myinfo[i]); $     }141printf"End Release");142 143System"Pause");144}

62. Multi-threaded multiple file retrieval, each thread retrieves a file that is used in case of insufficient memory

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.