2010-06-03 25 views
0

我已閱讀使用CRITICAL_SECTION時使用多線程增長鏈接列表的以下代碼。什麼是使用兩個線程添加到鏈接列表的main()部分?使用線程的鏈接列表示例

#include <windows.h> 

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 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; 
} 

回答

0

它可能會涉及一個或多個呼叫CreateThread

+0

,但只需要一個參數傳遞給線程函數? – 2010-06-03 13:10:18