-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add some multithreading examples and improve docs
- Loading branch information
1 parent
54b187e
commit 2b1a52b
Showing
7 changed files
with
132 additions
and
17 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
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,44 @@ | ||
# Demonstrates keeping the UI responsive while doing tasks in a thread | ||
|
||
if Threads.nthreads() == 1 && Threads.nthreads(:interactive) < 1 | ||
@warn("This example is intended to be run with multiple threads enabled.") | ||
end | ||
|
||
using Gtk4 | ||
|
||
btn = GtkButton("Start") | ||
sp = GtkSpinner() | ||
ent = GtkEntry(;hexpand=true) | ||
|
||
ltp = Threads.threadpool() | ||
|
||
grid = GtkGrid() | ||
grid[1:2,1] = GtkLabel("The GTK loop is running in thread $(Threads.threadid()) ($ltp threadpool)") | ||
grid[1,2] = btn | ||
grid[2,2] = sp | ||
grid[1:2,3] = ent | ||
|
||
signal_connect(btn, "clicked") do widget | ||
start(sp) | ||
Threads.@spawn begin | ||
# Do work | ||
stop_time = time() + 3 | ||
counter = 0 | ||
while time() < stop_time | ||
counter += 1 | ||
end | ||
|
||
tid = Threads.threadid() | ||
tp = Threads.threadpool() | ||
|
||
# Interacting with GTK from a thread other than the main thread is | ||
# generally not allowed, so we register an idle callback instead. | ||
Gtk4.GLib.g_idle_add() do | ||
stop(sp) | ||
ent.text = "I counted to $counter in thread $tid in the $tp threadpool!" | ||
false | ||
end | ||
end | ||
end | ||
|
||
win = GtkWindow(grid, "Threads", 420, 200) |
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,56 @@ | ||
# Demonstrates keeping the UI responsive while doing tasks in a thread | ||
|
||
if Threads.nthreads() == 1 && Threads.nthreads(:interactive) < 1 | ||
@warn("This example is intended to be run with multiple threads enabled.") | ||
end | ||
|
||
using Gtk4 | ||
|
||
btn = GtkButton("Start") | ||
sp = GtkSpinner() | ||
ent = GtkEntry(;hexpand=true) | ||
label = GtkLabel("") | ||
|
||
ltp = Threads.threadpool() | ||
|
||
grid = GtkGrid() | ||
grid[1:2,1] = GtkLabel("The GTK loop is running in thread $(Threads.threadid()) ($ltp threadpool)") | ||
grid[1,2] = btn | ||
grid[2,2] = sp | ||
grid[1:2,3] = ent | ||
grid[1:2,4] = label | ||
|
||
counter = Ref(0) | ||
|
||
signal_connect(btn, "clicked") do widget | ||
start(sp) | ||
stop_time = time() + 3 | ||
|
||
# g_timeout_add can be used to periodically call a function from the main loop | ||
Gtk4.GLib.g_timeout_add(50) do # create a function that will be called every 50 milliseconds | ||
label.label = "counter: $(counter[])" | ||
return time() < stop_time # return true to keep calling the function, false to stop | ||
end | ||
|
||
Threads.@spawn begin | ||
# Do work | ||
|
||
counter[] = 0 | ||
while time() < stop_time | ||
counter[] += 1 | ||
end | ||
|
||
tid = Threads.threadid() | ||
tp = Threads.threadpool() | ||
|
||
# Interacting with GTK from a thread other than the main thread is | ||
# generally not allowed, so we register an idle callback instead. | ||
Gtk4.GLib.g_idle_add() do | ||
stop(sp) | ||
ent.text = "I counted to $(counter[]) in thread $tid in the $tp threadpool!" | ||
false | ||
end | ||
end | ||
end | ||
|
||
win = GtkWindow(grid, "Threads with updating counter", 420, 200) |
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