Skip to content

Commit

Permalink
Merge pull request #51 from rmsacks/nanosleep
Browse files Browse the repository at this point in the history
Replace usleep() usage with nanosleep()
  • Loading branch information
pobrn authored Oct 15, 2020
2 parents 2ba2258 + 82b2252 commit f17af54
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion hash_pthreads.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#include <unistd.h> /* read(), close() */
#include <inttypes.h> /* PRId64 etc. */
#include <pthread.h>
#include <time.h> /* nanosleep() */

#ifdef USE_OPENSSL
#include <openssl/sha.h> /* SHA1() */
Expand Down Expand Up @@ -169,6 +170,10 @@ static void *print_progress(void *data)
{
struct queue *q = data;
int err;
struct timespec t;

t.tv_sec = PROGRESS_PERIOD / 1000000;
t.tv_nsec = PROGRESS_PERIOD % 1000000 * 1000;

err = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
FATAL_IF(err, "cannot set thread cancel type: %s\n", strerror(err));
Expand All @@ -178,7 +183,7 @@ static void *print_progress(void *data)
printf("\rHashed %u of %u pieces.", q->pieces_hashed, q->pieces);
fflush(stdout);
/* now sleep for PROGRESS_PERIOD microseconds */
usleep(PROGRESS_PERIOD);
nanosleep(&t, NULL);
}

return NULL;
Expand Down

0 comments on commit f17af54

Please sign in to comment.