zoukankan      html  css  js  c++  java
  • 多线程切割查找

     1 #define _CRT_SECURE_NO_WARNINGS
     2 #include <stdio.h>
     3 #include <stdlib.h>
     4 #include <time.h>
     5 #include <process.h>
     6 #include <Windows.h>
     7 
     8 struct findInfo
     9 {
    10     int *pstart;//首地址
    11     int length;
    12     int findNum;//要查找的数据
    13     int id;//编号
    14 };
    15 
    16 int flag = 1;
    17 
    18 void findIt(void *p)
    19 {
    20     if (flag == 1)
    21     {
    22 
    23         findInfo *ps = (findInfo *)p;
    24         printf("
    线程:%d开始查找", ps->id);
    25         //遍历首地址,长度10个元素
    26         for (int *pf = ps->pstart; pf < ps->pstart + ps->length; pf++)
    27         {
    28             if (*pf == ps->findNum)//相等
    29             {
    30                 printf("
    线程%d结束查找,找到数据%d地址%p", ps->id, *pf, pf);
    31                 flag = 0;
    32                 return;
    33             }
    34         }
    35 
    36         printf("
    线程:%d结束查找,没有找到", ps->id);
    37     }
    38     else
    39     {
    40         printf("
    其他线程已找到,结束查找");
    41     }
    42 }
    43 
    44 
    45 void main()
    46 {
    47     int a[100] = { 0 };
    48     time_t ts;
    49     unsigned int data = time(&ts);
    50     srand(data);
    51 
    52     for (int i = 0; i < 100; i++)
    53     {
    54         a[i] = rand() % 100;
    55         printf("%4d", a[i]);
    56         if ((i + 1) % 10 == 0)
    57         {
    58             printf("
    ");
    59         }
    60     }
    61 
    62     int num;
    63     scanf("%4d", &num);
    64     struct findInfo info[10];//结构体数组
    65 
    66     for (int i = 0; i < 10; i++)
    67     {
    68         info[i].pstart = a + 10 * i;
    69         info[i].length = 10;
    70         info[i].id = i;
    71         info[i].findNum = num;
    72         HANDLE hd = (HANDLE)_beginthread(findIt, 0, &info[i]);
    73         WaitForSingleObject(hd, INFINITE);//线程等待,一个一个执行
    74 
    75     }
    76 
    77     system("pause");
    78 }
  • 相关阅读:
    HDOJ 1207 汉诺塔II
    [转]写代码的小女孩
    POJ Subway tree systems
    HDOJ 3555 Bomb (数位DP)
    POJ 1636 Prison rearrangement (DP)
    POJ 1015 Jury Compromise (DP)
    UVA 10003
    UVA 103 Stacking Boxes
    HDOJ 3530 Subsequence
    第三百六十二、三天 how can I 坚持
  • 原文地址:https://www.cnblogs.com/xiaochi/p/5137753.html
Copyright © 2011-2022 走看看