设为首页 加入收藏

TOP

动态开辟指定数量的线程来查找动态开辟的数组中的1000000数据中的值(二)
2015-07-20 18:03:02 来源: 作者: 【 】 浏览:6
Tags:动态 开辟 指定 数量 线程 查找 1000000 数据
是否相等

if (*px == pstruct->num)

{

//查找

printf("\n第%d个线程找到%d,数值地址是:%p\n", pstruct->identify, *px, px);

//改变标识,代表找到

*(pstruct->pflag) = 1;

*(pstruct->addr) = px;

return;

}

}

printf("\n没有找到第%d个线程", pstruct->identify);

return;

}

/************************************************************************/

/* 通过二分查找的方式进行查找,这里有待检测 */

/************************************************************************/

void binarySearch(void *p)

{

struct threadStruct *pstruct = (struct threadStruct *)p;

int *low = pstruct->start;

int *high = pstruct->start + pstruct->length;

while (low <= high)

{

//这里说明指针相减得到的是中间的差值

int *mid = low + ((high - low) >> 1);

if (pstruct->num < *mid)

{

high = mid - 1;

}

else if (pstruct->num > *mid)

{

low = mid + 1;

}

else

{

//找到后将标识改成1

*pstruct->pflag = 1;

*pstruct->addr = ∣

return;

}

}

if (low > high)

{

printf("\n%d线程没有找到", pstruct->identify);

return;

}

}

thread.h

#ifndef _THREAD_H_

#define _THREAD_H_

struct threadStruct

{

int *start; //表示要查找的首地址

int length; //限定长度,从首地址开始只能找到后续的length个数值

int num; //要查找的数据

int identify; //线程的编号

int *pflag; //传递flag的地址,通过这个指针可以修改flag的值

int **addr; //存放所找数值所在的地址指针的地址

};

/************************************************************************/

/*为线程开辟空间 */

/************************************************************************/

extern void initThreadArrayStoreSpace(struct threadStruct **threadArr, int threadnum);

/************************************************************************/

/* 初始化线程内容,第二个参数表示的是数组 */

/************************************************************************/

extern void initThreadContent(struct threadStruct *threadArr, int len,

int *arr, int n, int targetNum, int threadnum);

/************************************************************************/

/* 打印结构体内容 */

/************************************************************************/

extern void printStructItemContent(struct threadStruct *threadArr, int n);

/************************************************************************/

/* 释放线程数组的内存空间 */

/************************************************************************/

extern void freeThreadStoreSpace(struct threadStruct *threadArr);

/************************************************************************/

/* 使用初始化好的线程执行查找动作 */

/************************************************************************/

extern void searchNumByMutiThread(struct threadStruct *threadArr, int n);

#endif

threadImpl.c

#include

#include

#include //调用多线程的时候要用到的头文件

#include

#include "find.h"

#include "thread.h"

/*要注意的是这里不能把变量定义在头文件中*/

/*定义是否找到的标识*/

int flag = 0;

/*这里表示数值所在位置*/

int *numAddress = NULL;

/*定义时间值*/

/************************************************************************/

/*为线程开辟空间 */

/************************************************************************/

void initThreadArrayStoreSpace(struct threadStruct **threadArr, int TNUM)

{

if (TNUM < 0)

{

printf("对不起,您的线程数不能小于0.\n");

return;

}

else

{

//因为是结构体数组,所以这里的struct threadStruct类型的

*threadArr = (struct threadStruct *)malloc(sizeof(struct threadStruct) * TNUM);

}

}

/************************************************************************/

/* 初始化线程内容,第二个参数表示的是数组 */

/***************

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDOJ 4883 TIANKENG’s restaurant 下一篇uva 11290 - Gangs(卡特兰数)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: