多线程的那点儿事(之多线程数据结构)
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】
要想编写多线程,那就要使用锁。而在软件编写中,数据结构是少不了的。所以,我们在编写多线程的时候,就需要考虑一下如何在数据结构中插入锁。当然,有些数据结构是没有锁的,所以自然这个锁并不一定是必须的。
比如说,我们编写一个多线程堆栈,应该怎么做呢,
typedef struct _STACK
{
void* pData;
int maxLen;
int top;
void* hLock;
STATUS (*push)(struct _STACK* pStack, void* pData);
STATUS (*pop)(struct _STACK* pStack, void* pData);
}STACK;
typedef struct _STACK
{
void* pData;
int maxLen;
int top;
void* hLock;
STATUS (*push)(struct _STACK* pStack, void* pData);
STATUS (*pop)(struct _STACK* pStack, void* pData);
}STACK; (1) 初始化操作
STACK* get_new_stack(void* pData, int len, void* pLock)
{
STACK* pStack;
if(NULL == pData || 0 == len)
return NULL;
pStack = (STACK*)malloc(sizeof(STACK));
assert(NULL != pStack);
memset(pStack, 0, sizeof(STACK));
pStack->pData = pData;
pStack->maxLen = len;
if(NULL != pLock)
pStack->hLock = pLock;
return pStack;
}
STACK* get_new_stack(void* pData, int len, void* pLock)
{
STACK* pStack;
if(NULL == pData || 0 == len)
return NULL;
pStack = (STACK*)malloc(sizeof(STACK));
assert(NULL != pStack);
memset(pStack, 0, sizeof(STACK));
pStack->pData = pData;
pStack->maxLen = len;
if(NULL != pLock)
pStack->hLock = pLock;
return pStack;
} (2) 添加数据
STATUS push(struct _STACK* pStack, void* pData, int size)
{
assert(NULL != pStack && NULL != pData);
if(NULL != pStack->hLock)
WaitForSingleObject((HANDLE)pStack->hLock, INFINITE);
if(pStack->top == pStack->maxLen){
if(NULL != pStack->hLock)
ReleaseMutex((HANDLE)pStack->hLock);
return ERROR;
}
memmove((char*)pStack->pData + size * pStack->top, (char*)pData, size);
pStack->top ++;
if(NULL != pStack->hLock)
ReleaseMutex((HANDLE)pStack->hLock);
return OK;
}
STATUS push(struct _STACK* pStack, void* pData, int size)
{
assert(NULL != pStack && NULL != pData);
if(NULL != pStack->hLock)
WaitForSingleObject((HANDLE)pStack->hLock, INFINITE);
if(pStack->top == pStack->maxLen){
if(NULL != pStack->hLock)
ReleaseMutex((HANDLE)pStack->hLock);
return ERROR;
}
memmove((char*)pStack->pData + size * pStack->top, (char*)pData, size);
pStack->top ++;
if(NULL != pStack->hLock)
ReleaseMutex((HANDLE)pStack->hLock);
return OK;
} (3) 对2进行优化,因为判断的条件比较复杂
#define STACK_CHECK_LOCK(hLock) \
do{\
if(hLock)\
WaitForSingleObject((HANDLE)hLock, INFINITE);\
}while(0)
#define STACK_CHECK_UNLOCK(hLock) \
do{\
if(hLock)\
ReleaseMutex((HANDLE)hLock);\
}while(0)
#define STACK_CHECK_LOCK(hLock) \
do{\
if(hLock)\
WaitForSingleObject((HANDLE)hLock, INFINITE);\
}while(0)
#define STACK_CHECK_UNLOCK(hLock) \
do{\
if(hLock)\
ReleaseMutex((HANDLE)hLock);\
}while(0) 所以,2的代码可以修改为,
STATUS push(struct _STACK* pStack, void* pData, int size)
{
assert(NULL != pStack && NULL != pData);
STACK_CHECK_LOCK(pStack->hLock);
if(pStack->top == pStack->maxLen){
STACK_CHECK_UNLOCK(pStack->hLock);
return ERROR;
}
memmove((char*)pStack->pData + size * pStack->top, (char*)pData, size);
pStack->top ++;
STACK_CHECK_UNLOCK(pStack->hLock);
return OK;
}
STATUS push(struct _STACK* pStack, void* pData, int size)
{
assert(NULL !
补充:软件开发 , 其他 ,