多线程处理数据

1.多线程多文件处理

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#include <memory.h>
#include <process.h>
#include <Windows.h>
//多线程多文件,实现线程的调度,设计模式,
//释放内存
//内存不够的情况,排队完成任务


struct infos
{
    char path[256];//原始
    int id;

    char **g_pp;//吧文件的内容,载入这个地址
    int length;

    char findstr[100];//查询

}myinfo[22] = {0};//22个结构体保存了22个文件的信息
HANDLE inithd[22] = {0};//22个初始化线程地址
HANDLE findhd[22] = { 0 };//22个查找线程地址


void runthreadinit(void *p)
{
    struct infos *pinfo = p;
    FILE *pf = fopen(pinfo->path, "r");
    if (pf!=NULL)
    {
        int i = 0;//测试多少行
        while (!feof(pf))
        {
            char str[256] = { 0 };
            fgets(str, 256, pf);//读取
            i++;
        }
        //i记录行数
        rewind(pf);//回到开头 fseek(pf,0,SEEK_SET);
        pinfo->g_pp = calloc(i, sizeof(char*));//分配内存初始化
        pinfo->length = i;//记录长度
        for (int j = 0; j < i;j++)
        {
            char str[256] = { 0 };
            fgets(str, 256, pf);//读取
            int length = strlen(str);
            pinfo->g_pp[j] = calloc(length + 1, sizeof(char));//分配内存
            if (pinfo->g_pp[j]!=NULL)
            {
                strcpy(pinfo->g_pp[j], str);//拷贝内存
            }

        }



    } 
    fclose(pf);
    printf("线程%d init  over
", pinfo->id);
}
void runthreadsearch(void *p)
{
    struct infos *pinfo = p;
    for (int i = 0; i < pinfo->length;i++)
    {
        if (pinfo->g_pp[i]!=NULL)
        {
            char *px = strstr(pinfo->g_pp[i], pinfo->findstr);
            if (px!=NULL)
            {
                printf("
%s", pinfo->g_pp[i]);
            }
        }
    }

    printf("线程%d find over
", pinfo->id);


}

void freeall(struct infos *pinfo)
{
    printf("freeall   start");
    for (int i = 0; i < pinfo->length;i++)
    {
        free(pinfo->g_pp[i]);//释放指针数组每一个指针对于的内存
    }
    free(pinfo->g_pp);//释放

    printf("freeall   end;");

}

void main()
{
    for (int i = 0; i < 22;i++)
    {
        myinfo[i].id = i+1;
        sprintf(myinfo[i].path, "Z:\I\尹成清华终极版C语言视频源码文档20150131\大数据相关数据\dangdangwang%d.txt", i + 1);
        strcpy(myinfo[i].findstr, "吴伟");
    }

    for (int i = 0; i < 15;i++)
    {
         inithd[i] = _beginthread(runthreadinit, 0, &myinfo[i]);
    }
    WaitForMultipleObjects(15, inithd, TRUE, INFINITE);//等待
    system("pause");
    for (int i = 0; i < 15;i++)
    {
        findhd[i] = _beginthread(runthreadsearch, 0, &myinfo[i]);
    }    
    WaitForMultipleObjects(15, findhd, TRUE, INFINITE);//等待
    system("pause");
    printf("开始释放");
    for (int i = 0; i < 15;i++)
    {
        freeall(&myinfo[i]);
    }
    printf("结束释放");
    system("pause");


    for (int i = 15; i < 22; i++)
    {
        inithd[i] = _beginthread(runthreadinit, 0, &myinfo[i]);
    }
    WaitForMultipleObjects(7, inithd+15, TRUE, INFINITE);//等待
    system("pause");


    system("pause");
    for (int i = 15; i < 22; i++)
    {
        findhd[i] = _beginthread(runthreadsearch, 0, &myinfo[i]);
    }
    WaitForMultipleObjects(7, findhd+15, TRUE, INFINITE);//等待
    system("pause");

    for (int i = 7; i < 22; i++)
    {
        freeall(&myinfo[i]);
    }
    system("pause");
}


void main1x()
{

    //myinfo[0].id = 1;
    //strcpy(myinfo[0].path, "Z:\I\尹成清华终极版C语言视频源码文档20150131\大数据相关数据\dangdangwang1.txt");
    //HANDLE pd1 = _beginthread(ru
//#define _CRT_SECURE_NO_WARNINGS
//#include<stdio.h>
//#include<stdlib.h>
//#include <string.h>
//#include <memory.h>
//#include <process.h>
//#include <Windows.h>
//
//char *path = "Z:\I\尹成清华终极版C语言视频源码文档20150131\大数据相关数据\dangdangwang.txt";
//#define N 13180820
//char ** g_pp;
//struct threads *pthread;
//
//void  init(char *path)
//{
//
//    printf("init start");
//    g_pp = malloc(sizeof(char*)*N);
//    memset(g_pp, '', sizeof(char*)*N);//清空内容
//    FILE *pf = fopen(path, "r");
//
//    if (pf==NULL)
//    {
//        printf("init  fail");
//    } 
//    else
//    {
//        for (int i = 0; i < N;i++)
//        {
//            char str[1024] = { 0 };
//            fgets(str, 1024, pf);//读取字符串
//            int length = strlen(str);//获取长度
//            if (length >= 1)
//            {
//                g_pp[i] = malloc(sizeof(char)*(length + 1));//分配内存
//                memset(g_pp[i], '', length + 1);//清空内容
//                if (g_pp[i]!=NULL)
//                {
//                    strcpy(g_pp[i], str);//拷贝
//                }
//            
//
//            } 
//            
//
//
//        }
//
//
//
//
//
//        fclose(pf);
//    }
//
//    printf("init end");
//
//}
//
//void  search(char*str)
//{
//
//    for (int i = 0; i < N;i++)
//    {
//        if (g_pp[i]!=NULL)
//        {
//            char *p = strstr(g_pp[i], str);
//            if (p != NULL)
//            {
//                printf("%s
", g_pp[i]);//找到打印
//            }
//        }
//        
//    }
//
//
//}
//
//
//struct threads
//{
//    char **ppstart;//指针数组的起始地址
//    int length;
//    int id;
//    char *pstr;//查找的字符串
//
//};
//void searchthread(void *p)
//{
//    struct threads *pinfo = p;
//    for (int i = 0; i < pinfo->length;i++)
//    {
//        if (pinfo->ppstart[i]!=NULL)
//        {
//            char *p = strstr(pinfo->ppstart[i], pinfo->pstr);//查找
//            if (p!=NULL)
//            {
//                printf("线程%d找到%s
", pinfo->id,pinfo->ppstart[i]);//找到
//            }
//        }
//    }
//
//
//
//
//
//}
//
//void  searchwiththread(char*str)
//{
//    int num = 23;//线程是CPU核心的倍数
//    pthread = malloc(sizeof(struct threads) * 23);//堆上开辟
//    memset(pthread, '', sizeof(struct threads )* 23);
//    HANDLE *phd = malloc(sizeof(HANDLE) * 23);
//
//    //
//    if (N%num==0)
//    {
//
//        for (int i = 0; i < num;i++)
//        {
//            pthread[i].id = i;
//            pthread[i].pstr = str;
//            pthread[i].length = N/num;//100  5  
//            pthread[i].ppstart = g_pp+i*(N/num);//起始地址
//            phd[i] = _beginthread(searchthread, 0, &pthread[i]);//创建线程
//
//        }
//
//    } 
//    else
//    {
//        //100 9  8×12+4
//        for (int i = 0; i < num-1; i++)
//        {
//            pthread[i].id = i;
//            pthread[i].pstr = str;
//            pthread[i].length = N / (num-1);//100  5  
//            pthread[i].ppstart = g_pp + i*(N / (num-1));//起始地址
//            phd[i] = _beginthread(searchthread, 0, &pthread[i]);//创建线程
//
//        }
//        {
//            int i = num - 1;
//            pthread[i].id = i;
//            pthread[i].pstr = str;
//            pthread[i].length = N%(num-1);//100  5  
//            pthread[i].ppstart = g_pp + i*(N / (num - 1));//起始地址
//            phd[i] = _beginthread(searchthread, 0, &pthread[i]);//创建线程
//            //0 8*12
//        }
//
//
//    }
//
//    WaitForMultipleObjects(num, phd, TRUE, INFINITE);//等待所有线程退出
//
//
//}
//
//
//
//
//
//int getN(char *path)
//{
//    FILE *pf = fopen(path, "r");
//    if (pf == NULL)
//    {
//        return -1;
//    }
//    else
//    {
//
//
//        int i = 0;
//        while (!feof(pf))
//        {
//            char str[256] = { 0 };
//            fgets(str, 256, pf);
//            i++;
//        }
//
//
//        fclose(pf);
//        return i;
//    }
//
//
//
//
//
//
//}
//
//
//
//
//
//
//
//
//
//void main()
//{
//
//
//    init(path);
//    while (1)
//    {
//        char str[128] = { 0 };
//        scanf("%s", str);
//        //search(str);
//        searchwiththread(str);
//    }
//
//
//
//
//
//    system("pause");
//}


nthreadinit, 0, &myinfo[0]);
    //WaitForSingleObject(pd1, INFINITE);//等待
    //strcpy(myinfo[0].findstr, "吴伟");
    //HANDLE pd2 = _beginthread(runthreadsearch, 0, &myinfo[0]);
    //WaitForSingleObject(pd2, INFINITE);//等待

    system("pause");
}

2.多线程检索内存‘

原文地址:https://www.cnblogs.com/sjxbg/p/5875837.html