Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reducing CPU Usage of Main GUI Loop #409

Open
balajeerc opened this issue Nov 10, 2019 · 0 comments
Open

Reducing CPU Usage of Main GUI Loop #409

balajeerc opened this issue Nov 10, 2019 · 0 comments

Comments

@balajeerc
Copy link

Using the example found in _example/twitterstream, my GUI loop in the main thread is setup as shown below:

var uiLock sync.Mutex
var alive bool = true

func InitGUI() {
	runtime.LockOSThread()
	gtk.Init(&os.Args)
}

func RunGUILoop() {
	for alive {
		uiLock.Lock()
		alive = gtk.MainIterationDo(false)
		uiLock.Unlock()
	}
}

func safeCallGUI(guiChange func()) {
	uiLock.Lock()
	defer uiLock.Unlock()
	guiChange()
}

This lets me use the uiLock mutex to safely make changes to the GUI in other goroutines, and this works well.

However, doing this basically jacks up the CPU usage of this process due the rather busy loop in which gtk.MainIterationDo(false) is called.

My first shot at reducing the CPU usage was to add a time.Sleep:

	for alive {
		uiLock.Lock()
		alive = gtk.MainIterationDo(false)
		uiLock.Unlock()
		time.Sleep(20000 * time.Microsecond)
	}

The trouble with this is that while it cuts down CPU usage, it adds a perceptible delay to any GUI event handling. I managed to tweak it to a sweet spot of 10000 microseconds to get an acceptable GUI event responsiveness as well as felling CPU to 1%.

However, I reckon that perhaps there is a more elegant way to do this? Any suggestions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant