godotengine / godotengine/godot-docs

"Using multiple threads" incorrectly describes how to use Mutex post- godot-cpp#2032

Open Beginner friendly
#12,346 1 comment 1 reaction 0 assignees View on GitHub
Dominant language
reStructuredText
Stars
5.7k
Forks
3.8k
Avg merge
1d 20h
Merged PRs (30d)
25

Description

**Your Godot version:**
N/A
**Issue description:**
As I understand it, using Mutex from `godot_cpp/classes/mutex.hpp` is obsolete after godotengine/godot-cpp#2032, which removes the old core-based Mutex in favor of inlining the core Mutex into `godot_cpp/templates/mutex.hpp`. As such, the example should look something like
```c++
// header
#pragma once

#include
#include
#include

namespace godot {
class MutexDemo : public Node {
GDCLASS(MutexDemo, Node);

private:
int counter = 0;
Mutex mutex;
Ref thread;

protected:
static void _bind_methods();
void _notification(int p_what);

public:
MutexDemo();
~MutexDemo();

void thread_function();
};
} // namespace godot

// source
#include "mutex_demo.h"

#include
#include
#include
#include

using namespace godot;

void MutexDemo::_bind_methods() {
ClassDB::bind_method(D_METHOD("thread_function"), &MutexDemo::thread_function);
}

void MutexDemo::_notification(int p_what) {
// Prevents this from running in the editor, only during game mode.
if (Engine::get_singleton()->is_editor_hint()) {
return;
}

switch (p_what) {
case NOTIFICATION_READY: {
UtilityFunctions::print("Mutex Demo Counter is starting at: ", counter);
mutex.instantiate();
thread.instantiate();
thread->start(callable_mp(this, &MutexDemo::thread_function), Thread::PRIORITY_NORMAL);

// Increase value, protect it with Mutex.
mutex.lock();
counter += 1;
UtilityFunctions::print("Mutex Demo Counter is ", counter, " after adding with Mutex protection.");
mutex.unlock();
} break;
case NOTIFICATION_EXIT_TREE: { // Thread must be disposed (or "joined"), for portability.
// Wait until it exits.
if (thread.is_valid()) {
thread->wait_to_finish();
}
thread.unref();

UtilityFunctions::print("Mutex Demo Counter is ", counter, " at EXIT_TREE."); // Should be 2.
} break;
}
}

MutexDemo::MutexDemo() {
// Initialize any variables here.
}

MutexDemo::~MutexDemo() {
// Add your cleanup here.
}

// Increment the value from the thread, too.
void MutexDemo::thread_function() {
mutex.lock();
counter += 1;
mutex.unlock();
}
```
**URL to the documentation page:**
https://docs.godotengine.org/en/latest/tutorials/performance/using_multiple_threads.html#mutexes

Contributor guide

No contributing guide indexed for this repository

Research direction

Open the “Mutexes” section of tutorials/performance/using_multiple_threads.html and compare its example with the current godot-cpp Mutex headers described in the issue. Check that the example uses godot_cpp/templates/mutex.hpp and matches the provided thread and mutex usage. Done means the documentation accurately reflects the post-godot-cpp#2032 API.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
88/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.