Какой вариант лучше? 1 Код (Text): #include "list.h" template <typename T> class TList { public: LIST_ENTRY head; CRITICAL_SECTION lock; struct TListEntry { LIST_ENTRY list; T data; }; TList() { INIT_LIST_HEAD(&head); InitializeCriticalSection(&lock); } ~TList() { list_for_each(pos,&head) { Remove(pos); } DeleteCriticalSection(&lock); } void Remove(LIST_ENTRY *list) { TListEntry *entry = (TListEntry *)list; UNLINK_ENTRY(&entry->list); delete entry; } void Clear() { list_for_each(pos,&head) { T data = Data(pos); delete data; Remove(pos); } } void Add(T data) { TListEntry *entry = new TListEntry; if (entry) { entry->data = data; ADD_ENTRY_TAIL(&head,&entry->list); } } inline T Data(LIST_ENTRY *list) { return ((TListEntry*)list)->data; } inline void Lock() { EnterCriticalSection(&lock); } inline void Unlock() { LeaveCriticalSection(&lock); } }; list.h: Код (Text): #define INIT_LIST_HEAD(head) \ (head)->Flink = (head); \ (head)->Blink = (head) #define UNLINK_ENTRY(entry) \ (entry)->Blink->Flink = (entry)->Flink; \ (entry)->Flink->Blink = (entry)->Blink #define ADD_ENTRY(head,entry) \ (entry)->Flink=(head)->Flink; \ (entry)->Blink=(head); \ (head)->Flink->Blink = (entry); \ (head)->Flink = (entry) #define ADD_ENTRY_TAIL(head,entry) \ (head)->Blink->Flink = (entry); \ (entry)->Blink = (head)->Blink; \ (entry)->Flink = (head); \ (head)->Blink = (entry) #define get_list_entry(type,entry) (type)(entry) #define list_for_each(pos, head) \ for (LIST_ENTRY *pos = (head)->Flink, *__safe_pos__ = pos->Flink; pos != (head);\ pos = __safe_pos__, __safe_pos__ = (pos)->Flink) 2 Код (Text): template <typename T> class ZList { struct ZListEntry { ZListEntry *next; ZListEntry *prev; T data; }; CRITICAL_SECTION ZListLock; public: ZListEntry *first, *last; class iterator { friend class ZList; ZListEntry *pos_next, *pos_prev, *pos; public: iterator(ZListEntry *entry) { pos = entry; if (pos) { pos_next = pos->next; pos_prev = pos->prev; } else { pos_next = pos_prev = 0; } } void operator ++(int) { pos = pos_next; if (pos) { pos_next = pos->next; } else { pos_next = 0; } } void operator --(int) { pos = pos_prev; if (pos) { pos_prev = pos->prev; } else { pos_prev = 0; } } ZListEntry* operator *(void) { return pos; } }; ZList() { first = last = 0; InitializeCriticalSection(&ZListLock); } ~ZList() { //Clear(); DeleteCriticalSection(&ZListLock); } void Clear() { T data; for (iterator i = iterator(first);*i;i++) { data = Data(i.pos); delete data; Unlink(i.pos); } } void Add(T data) { ZListEntry *entry = new ZListEntry; if (entry) { entry->data = data; if (last) { last->next = entry; entry->prev = last; } last = entry; if (!first) { first = entry; } } } void Unlink(ZListEntry *entry) { if (entry->prev) { (entry->prev)->next = entry->next; } else { first = entry->next; } if (entry->next) { (entry->next)->prev = entry->prev; } else { last = entry->prev; } delete entry; } T Data(ZListEntry *entry) { T retval = (T)0; if (entry) { retval = entry->data; } return retval; } void Lock() { EnterCriticalSection(&ZListLock); } void Unlock() { LeaveCriticalSection(&ZListLock); } }; Не тестилось особо, возможны ошибки. Оба варианта гавно? Но тогада предложите свой, STL юзать нельзя
Зачем лок, внешняя по смыслу сущность, инкапсулирован в списки? См scoped_lock в бусте. Тебя обманули.