6
6
#include < thread>
7
7
#include < vector>
8
8
#include < atomic>
9
- #include " llama.h"
9
+ #include < functional>
10
+ #include < mutex>
11
+ #include < vector>
10
12
#include " arg.h"
11
13
#include " common.h"
12
14
#include " log.h"
13
15
#include " sampling.h"
14
16
17
+ // Define a scope guard for RAII lifetime management
18
+ class ScopeGuard {
19
+ public:
20
+ template <class Callable >
21
+ ScopeGuard (Callable&& func) : m_func(std::forward<Callable>(func)) {}
22
+ ~ScopeGuard () { if (m_func) m_func (); }
23
+ ScopeGuard (const ScopeGuard&) = delete ;
24
+ ScopeGuard& operator =(const ScopeGuard&) = delete ;
25
+ ScopeGuard (ScopeGuard&& other) noexcept : m_func(std::move(other.m_func)) {
26
+ other.m_func = nullptr ;
27
+ }
28
+ private:
29
+ std::function<void ()> m_func;
30
+ };
31
+
32
+
15
33
int main (int argc, char ** argv) {
16
34
common_params params;
17
35
@@ -72,6 +90,10 @@ int main(int argc, char ** argv) {
72
90
models.emplace_back (model);
73
91
}
74
92
93
+
94
+ std::vector<llama_context_ptr> kept_contexts; // Stores contexts after thread exit
95
+ std::mutex kept_contexts_mutex; // Protects kept_contexts
96
+
75
97
for (int m = 0 ; m < num_models; ++m) {
76
98
auto * model = models[m].get ();
77
99
for (int c = 0 ; c < num_contexts; ++c) {
@@ -85,6 +107,12 @@ int main(int argc, char ** argv) {
85
107
return ;
86
108
}
87
109
110
+ // Scope guard moves ctx to kept_contexts when thread exits
111
+ ScopeGuard guard ([&] {
112
+ std::lock_guard<std::mutex> lock (kept_contexts_mutex);
113
+ kept_contexts.push_back (std::move (ctx));
114
+ });
115
+
88
116
std::unique_ptr<common_sampler, decltype (&common_sampler_free)> sampler { common_sampler_init (model, params.sampling ), common_sampler_free };
89
117
if (sampler == NULL ) {
90
118
LOG_ERR (" failed to create sampler\n " );
@@ -142,6 +170,8 @@ int main(int argc, char ** argv) {
142
170
thread.join ();
143
171
}
144
172
173
+ kept_contexts.clear ();
174
+
145
175
if (failed) {
146
176
LOG_ERR (" One or more threads failed.\n " );
147
177
return 1 ;
0 commit comments