#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;
}
执行结果: