20150418多线程、指针与数组、结构体

三.多线程传递参数

<span style="font-size:24px;">#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <Windows.h>
#include <time.h>

void run(void *p) {

	int *px = p;
	char str[100] = { 0 };
	sprintf(str, "锄禾日当午%d", *px);

	MessageBoxA(0, str, "天朝很痛苦", 0);

}

void main1() {


	int a[10] = { 1,2,3,4,5,6,7,8,9,10 };

	for (int i = 0; i < 10; i++) {


		HANDLE	hd=_beginthread(run, 0,&i);
		//WaitForSingleObject(hd, INFINITE);
	}


	system("pause");
}</span>


五.多线程

1.多线程查找(多个查找、单个查找)

<span style="font-size:24px;">int isfind = 0;

struct findinfo {
	int *pstart;//首地址
	int length;
	int findnum;
	int id;//编号
};

void findit(void *p) {
	
	struct findinfo *ps = p;//保存地址
	printf("线程%d开始查找", ps->id);
	//遍历首地址,长度为10个元素
	for (int *pf = ps->pstart; pf < ps->pstart + ps->length; pf++) {
		if (isfind == 1) {
			printf("\n线程%d结束查找,其他线程已经找到", ps->id);
			return;
		}
		if (*pf == ps->findnum) {
			printf("线程%d结束查找,找到数据%d地址%p\n", ps->id, *pf, pf);
			isfind = 1;
			//return;
			return;
		}
	}

	printf("线程%d结束查找,没有找到\n", ps->id);
}

void main() {

	int a[100] = { 0 };
	time_t ts;
	unsigned int data = time(&ts);
	srand(data);

	for (int i = 0; i < 100; i++) {

		a[i] = rand() % 100;
		printf("%4d", a[i]);
		if ((i+1) % 10 == 0) {
			printf("\n");
		}
	}

	int num = 0;
	scanf("%d", &num);
	struct findinfo info[10];//结构体数组,保存每个数组要查找的信息
	for (int i = 0; i < 10; i++) {

		info[i].pstart = a + 10 * i;//首地址
		info[i].length = 10;
		info[i].id = i;
		info[i].findnum = num;
		HANDLE hd=_beginthread(findit, 0, &info[i]);
		//WaitForSingleObject(hd, INFINITE);
	}


	system("pause");
}</span>

2.多线程切割

<span style="font-size:24px;">#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <process.h>
#include <Windows.h>


int isfind = 0;
struct findinfo {
	int *pstart;
	int length;
	int findnum; 
	int id;//编号
};

void findit(void *p) {
	struct findinfo *ps = p;//保存地址


	printf("\n线程%d开始查找", ps->id);
	//遍历首地址,长度10个元素

	for (int *pf = ps->pstart; pf < ps->pstart + ps->length; pf++)
	{
		if (isfind == 1)
		{
			printf("\n线程%d结束查找,其他线程已经找到", ps->id);
			return;
		}

		if (*pf == ps->findnum)//相等
		{
			printf("结束查找找到数据%d地址%p", ps->id, *pf, pf);
			isfind = 1;
			return;
		}
		Sleep(500);

	}



	printf("\n线程%d结束查找,没有找到", ps->id);

}


#define M 100   //数据
#define N 8		//线程数量
void main() {

	int a[M] = { 0 };
	time_t ts;
	unsigned int data = time(&ts);
	srand(data);



	
	for (int i = 0; i < 100; i++) {


		a[i] = rand()%100;
		printf("%4d", a[i]);
		if ((i + 1) % 10 == 0) {
			printf("\n");
		}
	}
	int num;
	scanf("%d", &num);
	struct findinfo info[N];
	if (M%N == 0) {
		for (int i = 0; i < N; i++) {
			info[i].pstart = a + M/N * i;
			info[i].length = M/N;
			info[i].id = i;
			info[i].findnum = num;
			HANDLE hd = _beginthread(findit, 0, &info[i]);
		}
	}
	else {
		for (int i = 0; i < N-1; i++) {
			info[i].pstart = a + M / (N-1) * i;
			info[i].length = M / (N-1);
			info[i].id = i;
			info[i].findnum = num;
			HANDLE hd = _beginthread(findit, 0, &info[i]);
		}
		//info[N-1]
		int i = N - 1;
		info[i].pstart = a + M / (N - 1) * i;
		info[i].length = M % (N - 1);
		info[i].id = i;
		info[i].findnum = num;
		HANDLE hd = _beginthread(findit, 0, &info[i]);
	}

	system("pause");
}</span>

七.指针数组强化

1.数组逆置

void rev(int *p, int n)//长度
{
	for (int i = 0; i < n / 2;i++)
	{
		int temp = p[i];
		p[i] = p[n - 1 - i];
		p[n - 1 - i] = temp;
	}



}

void revp(int *p, int n)//长度
{
	for (int *phead = p, *pback = p + n - 1; phead < pback;phead++,pback--)
	{
		int temp = *phead;
		*phead = *pback;
		*pback = temp;


	}





}

void show(int *p, int n)
{

	for (int i = 0; i < n;i++)
	{
		printf("%4d", p[i]);
	}
}

void main4()
{
	int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };//10/2  《5  //0-4
	int b[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9};//9/2  0-3 4
	revp(a, sizeof(a) / sizeof(a[0]));
	show(a, sizeof(a) / sizeof(a[0]));
	putchar('\n');
	revp(b, sizeof(b) / sizeof(b[0]));
	show(b, sizeof(b) / sizeof(b[0]));


	system("pause");



}

八.队列与多线程

1.struct的两种取值

struct Myinfo info;

方法一:info.length=100;

方法二:(&info)->length=100;

(如果用箭头赋值必须是地址)


2.多线程结构体应用

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <Windows.h>
#include <time.h>
#define N 1024

struct Myinfo {
	int *pstart;//开始地址
	int id;
	int length;
	int sum;
};

void add(void *p) {//void可以保存任何类型的指针,也可以转化为任何类型的指针

	struct Myinfo *pinfo = p;
	for (int i = 0; i < pinfo->length; i++) {
		pinfo->sum += pinfo->pstart[i];
	}
	printf("线程%d的结果%d\n", pinfo->id, pinfo->sum);
}

void main() {

	time_t ts;
	unsigned int data = time(&ts);
	srand(data);
	
	int num[N] = { 0 };
	for (int i = 0; i < N; i++) {

		//num[i] = rand() % 1000;
		printf("%4d", num[i] = rand() % 1000);
		if ((i + 1) % 18 == 0) {
			printf("\n");
		}
	}

	printf("\n");


	struct Myinfo info[8] = { 0 };

	for (int i = 0; i < 8; i++) {
		info[i].id = i;
		info[i].length = N / 8;
		info[i].sum = 0;
		info[i].pstart = num + i*N / 8;
		_beginthread(add, 0, &info[i]);
	}

	system("pause");
	int lastsum = 0;
	for (int i = 0; i < 8; i++) {
		lastsum += info[i].sum;
	}

	printf("\n多线程总和=%d", lastsum);

	system("pause");
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值