Rather than sleep-loop, main thread waits for all threads to exit.
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
diff --git a/cpu-miner.c b/cpu-miner.c
index 62777ec..19f3f35 100644
--- a/cpu-miner.c
+++ b/cpu-miner.c
@@ -31,8 +31,6 @@
#define DEF_RPC_USERPASS "rpcuser:rpcpass"
enum {
- STAT_SLEEP_INTERVAL = 100,
- STAT_CTR_INTERVAL = 10000000,
FAILURE_INTERVAL = 30,
};
@@ -62,7 +60,6 @@ bool opt_debug = false;
bool opt_protocol = false;
bool opt_quiet = false;
static int opt_retries = 10;
-static bool program_running = true;
static const bool opt_time = true;
static enum sha256_algos opt_algo = ALGO_C;
static int opt_n_threads = 1;
@@ -446,6 +443,7 @@ static void parse_cmdline(int argc, char *argv[])
int main (int argc, char *argv[])
{
int i;
+ pthread_t *t_all;
/* parse command line */
parse_cmdline(argc, argv);
@@ -454,11 +452,13 @@ int main (int argc, char *argv[])
if (setpriority(PRIO_PROCESS, 0, 19))
perror("setpriority");
+ t_all = calloc(opt_n_threads, sizeof(pthread_t));
+ if (!t_all)
+ return 1;
+
/* start mining threads */
for (i = 0; i < opt_n_threads; i++) {
- pthread_t t;
-
- if (pthread_create(&t, NULL, miner_thread,
+ if (pthread_create(&t_all[i], NULL, miner_thread,
(void *)(unsigned long) i)) {
fprintf(stderr, "thread %d create failed\n", i);
return 1;
@@ -472,11 +472,11 @@ int main (int argc, char *argv[])
opt_n_threads,
algo_names[opt_algo]);
- /* main loop */
- while (program_running) {
- sleep(STAT_SLEEP_INTERVAL);
- /* do nothing */
- }
+ /* main loop - simply wait for all threads to exit */
+ for (i = 0; i < opt_n_threads; i++)
+ pthread_join(t_all[i], NULL);
+
+ fprintf(stderr, "all threads dead, fred. exiting.\n");
return 0;
}