線程的同步控制

typedef struct _Node
{
  struct _Node *next; 
  int data;
} Node;
typedef struct _List
{
  Node *head; 
  CRITICAL_SECTION critical_sec;
} List;
List *CreateList()
{
  List *pList = (List *)malloc(sizeof(pList));
  pList->head = NULL;
  InitializeCriticalSection(&pList->critical_sec);
  return pList;
}
void DeleteList(List *pList)
{
  DeleteCriticalSection(&pList->critical_sec);
  free(pList);
}

void AddHead(List *pList, Node *node)
{
  EnterCriticalSection(&pList->critical_sec);
  node->next = pList->head;
  pList->head = node;
  LeaveCriticalSection(&pList->critical_sec);
}

void Insert(List *pList, Node *afterNode, Node *newNode)
{
  EnterCriticalSection(&pList->critical_sec);
  if(afterNode == NULL)
  {
    AddHead(pList, newNode);
  }
  else
  {
    newNode->next = afterNode->next;
	afterNode->next = newNode;
  }
  LeaveCriticalSection(&pList->critical_sec);
}

Node *next(List *pList, Node *node)
{
  Node* next;
  EnterCriticalSection(&pList->critical_sec);
  next = node->next;
  LeaveCriticalSection(&pList->critical_sec);
  return next;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章