Skip to content

Commit 3744724

Browse files
committed
feat(scripting/cpp): CreateThread
1 parent 0208efe commit 3744724

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# CreateThread
2+
3+
Calls the specified function on a separate thread.
4+
5+
### Syntax
6+
7+
```cpp
8+
CreateThread(std::function<void()> cb)
9+
```
10+
11+
:::note
12+
The function argument also supports lambda function ( \[]\{ ... } ).
13+
:::
14+
15+
### Example
16+
17+
```cpp
18+
void OnPluginStart()
19+
{
20+
CreateThread([]() {
21+
print("Called on the first server tick!\n");
22+
});
23+
}
24+
```

plugin_files/scripting/includes/swiftly/swiftly_scripting_utils.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ int GetPlayerId(const char *str, bool matchbots)
2121
return i;
2222
}
2323
return -1;
24+
}
25+
26+
void CreateThread(std::function<void()> fn)
27+
{
28+
std::thread(fn).detach();
2429
}

plugin_files/scripting/includes/swiftly/swiftly_scripting_utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
#include "swiftly_utils.h"
55
#include "playermanager.h"
66

7+
#include <functional>
8+
#include <thread>
9+
710
int GetPlayerId(const char *str, bool matchbots = false);
11+
void CreateThread(std::function<void()> fn);
812

913
#endif

0 commit comments

Comments
 (0)