Seite 1 von 1

ConcurrentQueue "Erledigt"

Verfasst: 22.04.2019 12:08
von silbersurfer
Hallo Leute,
ich sehe den Wald vor lauter Bäumen mal wieder nicht.
für mein Projekt wollte ich einen Wateschlangen stack schreiben, und bin dabei auf NicTheQuick sein Tutorial gestoßen
und habe mir das angesehen und finde das auch richtig Super.
jetzt wollte ich das gerne so Anpassen, das keine Zahlen sondern Strings zu der liste hinzugefügt/abgerufen werden,
ich komme einfach nicht drauf wie ich jetzt mit dem *Pointer zur Liste verfahren soll.
Ich hoffe das ihr mir da auf die sprünge helfen könnt
Edit: hat sich erledigt hab mal wieder was Übersehnen :freak: :oops:
ich sehe den Wald vor lauter Bäumen mal wieder nicht.

Code: Alles auswählen

DeclareModule ConcurrentQueue
	Interface type
		destroy.i()
		CountElements.i()
		isEmpty.i()
		push.i(element.s)
		pop.s()
	EndInterface	
	Declare.i new()
EndDeclareModule	
Module ConcurrentQueue
	
	Structure Type_s
		*vTable
		semaphore.i
		lock.i
		List elements.s()
	EndStructure	
	Procedure new ()
		Protected *this.Type_s=AllocateMemory(SizeOf(Type_s))
		If (Not *this)
			ProcedureReturn #False
		EndIf 
		
		InitializeStructure(*this, Type_s)
		With *this
			\vTable			=?vTable
			\semaphore 	=CreateSemaphore(0)
			\lock				=CreateMutex()
		EndWith	
		ProcedureReturn *this
	EndProcedure	
	
	Procedure destroy(*this.Type_s)
		With *this
			FreeMutex(\lock)
			FreeSemaphore(\semaphore)
			ClearStructure(*this, Type_s)
			FreeMemory(*this)
		EndWith	
	EndProcedure	
	
	Procedure.i countElements(*this.Type_S)
		Protected Size.i
		With	*this
			LockMutex(\lock) 
			size= ListSize(\elements())
			UnlockMutex(\lock)
		EndWith	
		ProcedureReturn size
	EndProcedure	
	
	Procedure.i isEmpty(*this.type)
		ProcedureReturn Bool(*this\CountElements() = 0)
	EndProcedure	
	
	Procedure push(*this.Type_s, element.s)
		Protected result.i = #False
		With *this
			LockMutex(\lock)
			LastElement(\elements())
			If AddElement(\elements())
				\elements()=element
				SignalSemaphore(\semaphore)
				result=#True
			EndIf 
			UnlockMutex(\lock)
		EndWith	
		;ProcedureReturn result
	EndProcedure 	
	
	Procedure.s pop(*this.type_s)
		Protected element.s
		With *this
			LockMutex(\lock)
			If Not TrySemaphore(\semaphore)
				UnlockMutex(\lock)
				WaitSemaphore(\semaphore)
				LockMutex(\lock)
			EndIf 	
			FirstElement(\elements())
			element = \elements()
			DeleteElement(\elements())
			UnlockMutex(\lock)
		EndWith	
		ProcedureReturn element
	EndProcedure	
	DataSection
		vTable:
		Data.i @destroy(), @countElements(), @isEmpty(), @push(), @pop()
	EndDataSection	
EndModule	

Define q.ConcurrentQueue::type =ConcurrentQueue::new()
q\push("1")
q\push("2")
Debug q\CountElements()
Debug q\pop()
Debug q\pop()