2010-06-03 5 views
0

J'ai lu le code suivant d'utilisation de CRITICAL_SECTION lorsque vous travaillez avec plusieurs threads pour développer une liste chaînée. quelle serait la partie main() qui utilise deux threads à ajouter à la liste chaînée?Exemple de liste liée utilisant des threads

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

Répondre

0

Il sera probablement impliquer un ou plusieurs appels à CreateThread

+0

mais il ne prend qu'un seul argument à fil fonction? –

Questions connexes