summaryrefslogtreecommitdiff
path: root/test/threads.cxx
diff options
context:
space:
mode:
authormaxim nikonov <maxim.nikonov@hqo.co>2026-02-06 04:21:03 +0500
committermaxim nikonov <maxim.nikonov@hqo.co>2026-02-06 04:21:03 +0500
commit4810610dafb8ed93dd3672e32256997e22eca950 (patch)
treee0db960825627acbe94a71f773ca3ac36617869f /test/threads.cxx
parentf93978aba486bf0fea8d9ee857d014a02f3f7d96 (diff)
wip
Diffstat (limited to 'test/threads.cxx')
-rw-r--r--test/threads.cxx37
1 files changed, 30 insertions, 7 deletions
diff --git a/test/threads.cxx b/test/threads.cxx
index 4c8a94d11..466953b37 100644
--- a/test/threads.cxx
+++ b/test/threads.cxx
@@ -25,7 +25,7 @@
# include "threads.h"
# include <stdio.h>
# include <math.h>
-# include <vector>
+# include <stdlib.h>
// min. time in seconds before calling Fl::awake(...)
#define DELTA 0.25
@@ -39,9 +39,30 @@ struct prime {
Fl_Terminal *terminal; // widget to write output to
unsigned long max_prime; // highest prime found so far
Fl_Output *value; // highest prime output widget
- std::vector<unsigned long> primes; // collected primes within time frame
+ unsigned long *primes; // collected primes within time frame
+ size_t primes_count; // number of collected primes
+ size_t primes_alloc; // allocated size
};
+static void primes_init(struct prime *pr) {
+ pr->primes = 0;
+ pr->primes_count = 0;
+ pr->primes_alloc = 0;
+}
+
+static void primes_push(struct prime *pr, unsigned long n) {
+ if (pr->primes_count >= pr->primes_alloc) {
+ size_t new_alloc = pr->primes_alloc ? pr->primes_alloc * 2 : 64;
+ pr->primes = (unsigned long *)realloc(pr->primes, new_alloc * sizeof(unsigned long));
+ pr->primes_alloc = new_alloc;
+ }
+ pr->primes[pr->primes_count++] = n;
+}
+
+static void primes_clear(struct prime *pr) {
+ pr->primes_count = 0;
+}
+
Fl_Thread prime_thread;
Fl_Terminal *tty1, *tty2;
@@ -61,7 +82,9 @@ void magic_number_cb(void *p) {
void update_handler(void *v) {
char max_buf[32];
struct prime *pr = (struct prime *)v;
- for (auto n : pr->primes) {
+ size_t i;
+ for (i = 0; i < pr->primes_count; i++) {
+ unsigned long n = pr->primes[i];
pr->terminal->printf("prime: %10lu\n", n);
if (n > pr->max_prime)
pr->max_prime = n;
@@ -103,8 +126,8 @@ extern "C" void* prime_func(void* p) {
pr[0].terminal = pr[1].terminal = terminal;
pr[0].max_prime = pr[1].max_prime = 0;
pr[0].value = pr[1].value = value;
- pr[0].primes.clear();
- pr[1].primes.clear();
+ primes_init(&pr[0]);
+ primes_init(&pr[1]);
int pi = 0; // prime buffer index
Fl_Timestamp last = Fl::now();
@@ -129,7 +152,7 @@ extern "C" void* prime_func(void* p) {
if (pp > hn) { // n is a prime
- pr[pi].primes.push_back(n);
+ primes_push(&pr[pi], n);
// Send a message to the main thread, at which point it will
// process any pending updates.
@@ -140,7 +163,7 @@ extern "C" void* prime_func(void* p) {
last = Fl::now();
Fl::awake(update_handler, (void *)(&pr[pi]));
pi = 1 - pi; // switch to alternate buffer
- pr[pi].primes.clear(); // clear primes
+ primes_clear(&pr[pi]); // clear primes
}
if (n > max_value) {