生产者与消费者C语言版

#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <windows.h>

#include "base.h"

typedef struct
{
	HANDLE sem;//账号信号量
	int count;//账号余额
}Account;

HANDLE threads[2];//第一个是inThread,第二个是outThread

unsigned long inThrId;
unsigned long outThrId;

void *inThreadEntry(void *param);
void *outThreadEntry(void *param);

BOOL startInThread(Account *account);
BOOL startOutThread(Account *account);

BOOL instore(Account *account,int count);
BOOL outstore(Account *account, int count);

void initAccount(Account *account);
void destroyAccount(Account *account);

void main(void)
{
	Account *account = (Account *)malloc(sizeof(Account));
	if (NULL == account)
	{
		printf("ERROR: Malloc for account failed!\n");
		return;
	}

	initAccount(account);
	
	if (BOOL_TRUE == startInThread(account))
	{
		if (BOOL_TRUE != startOutThread(account))
		{
			destroyAccount(account);
			CloseHandle(threads[0]);
		}
	}
	else
	{
		destroyAccount(account);
	}

	WaitForMultipleObjects(2,threads,TRUE,INFINITE);

	CloseHandle(threads[0]);
	CloseHandle(threads[1]);
	destroyAccount(account);
}

void *inThreadEntry(void *param)
{
	while (1)
	{
		instore((Account *)param,1);
		Sleep(1000);
	}

	return NULL;
}

void *outThreadEntry(void *param)
{
	while (1)
	{
		outstore((Account *)param,5);
		Sleep(1000);
	}

	return NULL;
}

BOOL startInThread(Account *account)
{
	threads[0] = (HANDLE)_beginthreadex(NULL,0,(unsigned int(_stdcall *)(void *))inThreadEntry,account,0,&inThrId);
	if (NULL == threads[0])
	{
		printf("ERROR: Create in thread failed!\n");
		return BOOL_FALSE;
	}
	else
	{
		printf("INFO: Create in thread succeed!\n");
		return BOOL_TRUE;
	}
}

BOOL startOutThread(Account *account)
{
	threads[1] = (HANDLE)_beginthreadex(NULL,0,(unsigned int(_stdcall *)(void *))outThreadEntry,account,0,&outThrId);
	if (NULL == threads[1])
	{
		printf("ERROR: Create out thread failed!\n");
		return BOOL_FALSE;
	}
	else
	{
		printf("INFO: Create out thread succeed!\n");
		return BOOL_TRUE;
	}
}

BOOL instore(Account *account,int count)
{
	if (account == NULL)
	{
		printf("ERROR: the account is not initated!\n");
		return BOOL_FALSE;
	}
	
	WaitForSingleObject(account->sem,INFINITE);
	account->count = account->count + count;
	printf("INFO: Store %d into this account!\n",count);
	printf("INFO: After sotre, the count is %d!\n",account->count);
	ReleaseSemaphore(account->sem,1,NULL);

	return BOOL_TRUE;
}

BOOL outstore(Account *account, int count)
{
	int nowCount;

	if (NULL == account)
	{
		printf("ERROR: Please first init the account!\n");
		return BOOL_FALSE;
	}

	WaitForSingleObject(account->sem,INFINITE);
	nowCount = account->count;
	if (nowCount < count)
	{
		ReleaseSemaphore(account->sem,1,NULL);
		printf("ERROR: The account is not enough,Waiting...!\n");
		Sleep(500);
		return outstore(account,count);
	}
	
	account->count = account->count - count;
	printf("INFO: Get %d from account succeed!\n",count);
	printf("INFO: After outstore: %d!\n",account->count);
	ReleaseSemaphore(account->sem,1,NULL);

	return BOOL_TRUE;
}

void initAccount(Account *account)
{
	memset(account,0x00,sizeof(Account));
	account->sem = CreateSemaphore(NULL,1,1,(LPCWSTR)"Semaphore for account!");
	account->count = 0;
}

void destroyAccount(Account *account)
{
	if (NULL == account)
	{
		return;
	}

	CloseHandle(account->sem);

	memset(account,0x00,sizeof(Account));
	free(account);
	account = NULL;
}

 执行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值