-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #325 from bmx-ng/task/timed-blockingqueue
Added time Enqueue and Dequeue methods.
- Loading branch information
Showing
5 changed files
with
169 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
' | ||
' Demonstrates how to use a blocking queue to synchronize threads. | ||
' | ||
SuperStrict | ||
|
||
Framework Brl.StandardIO | ||
Import Brl.Threads | ||
Import Brl.Collections | ||
|
||
|
||
Function Producer:Object(data:Object) | ||
Local queue:TBlockingQueue<Int> = TBlockingQueue<Int>(data) | ||
|
||
For Local i:Int = 1 To 10 | ||
Try | ||
Print "Producing " + i | ||
queue.Enqueue(i, 100, ETimeUnit.Milliseconds) ' 100 milliseconds timeout | ||
Delay 100 ' Simulate work | ||
Catch ex:TTimeoutException | ||
Print "Enqueue timed out: " + ex.ToString() | ||
End Try | ||
Next | ||
End Function | ||
|
||
Function Consumer:Object(data:Object) | ||
Local queue:TBlockingQueue<Int> = TBlockingQueue<Int>(data) | ||
|
||
For Local i:Int = 1 To 10 | ||
Try | ||
Local item:Int = queue.Dequeue(1500, ETimeUnit.Milliseconds) ' 1.5 second timeout | ||
Print "Consuming " + item | ||
Delay 1000 ' Simulate work | ||
Catch ex:TTimeoutException | ||
Print "Dequeue timed out: " + ex.ToString() | ||
End Try | ||
Next | ||
End Function | ||
|
||
Local queue:TBlockingQueue<Int> = New TBlockingQueue<Int>(5) | ||
Local producerThread:TThread = CreateThread(Producer, queue) | ||
Local consumerThread:TThread = CreateThread(Consumer, queue) | ||
|
||
WaitThread(producerThread) | ||
WaitThread(consumerThread) | ||
|
||
Print "All tasks are done." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
' Copyright (c)2024 Bruce A Henderson | ||
' | ||
' This software is provided 'as-is', without any express or implied | ||
' warranty. In no event will the authors be held liable for any damages | ||
' arising from the use of this software. | ||
' | ||
' Permission is granted to anyone to use this software for any purpose, | ||
' including commercial applications, and to alter it and redistribute it | ||
' freely, subject to the following restrictions: | ||
' | ||
' 1. The origin of this software must not be misrepresented; you must not | ||
' claim that you wrote the original software. If you use this software | ||
' in a product, an acknowledgment in the product documentation would be | ||
' appreciated but is not required. | ||
' | ||
' 2. Altered source versions must be plainly marked as such, and must not be | ||
' misrepresented as being the original software. | ||
' | ||
' 3. This notice may not be removed or altered from any source | ||
' distribution. | ||
' | ||
SuperStrict | ||
|
||
Module BRL.Time | ||
|
||
ModuleInfo "Version: 1.0" | ||
ModuleInfo "Author: Bruce A Henderson" | ||
ModuleInfo "License: zlib/libpng" | ||
ModuleInfo "Copyright: Bruce A Henderson" | ||
|
||
ModuleInfo "History: 1.00" | ||
ModuleInfo "History: Initial Release" | ||
|
||
|
||
Rem | ||
bbdoc: A unit of date-time, such as Days or Hours. | ||
End Rem | ||
Enum ETimeUnit | ||
Milliseconds | ||
Seconds | ||
Minutes | ||
Hours | ||
Days | ||
End Enum | ||
|
||
Rem | ||
bbdoc: Converts a time value to milliseconds. | ||
End Rem | ||
Function TimeUnitToMillis:ULong( value:ULong, unit:ETimeUnit ) | ||
Select unit | ||
Case ETimeUnit.Milliseconds | ||
Return value | ||
Case ETimeUnit.Seconds | ||
Return value * 1000 | ||
Case ETimeUnit.Minutes | ||
Return value * 60000 | ||
Case ETimeUnit.Hours | ||
Return value * 3600000 | ||
Case ETimeUnit.Days | ||
Return value * 86400000 | ||
End Select | ||
End Function | ||
|
||
Type TTimeoutException Extends TRuntimeException | ||
|
||
Method New(message:String) | ||
Super.New(message) | ||
End Method | ||
|
||
End Type |
2a3dff6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good stuff! I actually have a very similar TimeUnitToMillis function in my own modules using the code from brl.timer -> TChrono.GetTimestamp()
I see you're using Generics, there's still one "game breaking" bug with generics and imports/modules that I found if you feel inclined, I tried fixing it myself some time ago but I couldn't wrap my head around it: bmx-ng/bcc#657
Thanks!