1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright © 2026 Apple Inc.
//
// Regression test for https://github.com/ml-explore/mlx/issues/3126
// Verifies that the process exits cleanly when a background thread is
// performing GPU work and the main thread exits.
#include <chrono>
#include <iostream>
#include <thread>
#include "mlx/mlx.h"
namespace mx = mlx::core;
int main() {
using namespace std::chrono_literals;
std::thread t([] {
auto a = mx::random::normal({2048, 2048});
std::cout << "START" << std::endl;
for (int i = 0; i < 1000; ++i) {
a = mx::matmul(a, a);
// Eval periodically to avoid building a huge graph
if (i % 10 == 0) {
mx::eval(a);
std::cout << "Step " << i << std::endl;
}
}
mx::eval(a);
std::cout << "Done: " << a.shape(0) << "x" << a.shape(1) << std::endl;
});
std::this_thread::sleep_for(1s);
t.detach();
std::cout << "Main thread exiting." << std::endl;
return 0;
}