MCPcopy Create free account
hub / github.com/MultithreadedJSBook/code-samples / get_happycoins

Function get_happycoins

ch1-c-threads/happycoin-threads.c:43–61  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41}
42
43void * get_happycoins(void * arg) {
44 int attempts = *(int *)arg; // <1>
45 int limit = attempts/10000;
46 uint32_t seed = time(NULL);
47 uint64_t * nums = malloc(limit * sizeof(uint64_t));
48 struct happy_result * result = malloc(sizeof(struct happy_result));
49 result->nums = nums;
50 result->count = 0;
51 for (int i = 1; i < attempts; i++) {
52 if (result->count == limit) {
53 break;
54 }
55 uint64_t random_num = random64(&seed);
56 if (is_happycoin(random_num)) {
57 result->nums[result->count++] = random_num;
58 }
59 }
60 return (void *)result;
61}
62
63#define THREAD_COUNT 4
64

Callers

nothing calls this directly

Calls 2

random64Function · 0.70
is_happycoinFunction · 0.70

Tested by

no test coverage detected