Skip to content

Commit

Permalink
Replace usleep with C++11's sleep_for
Browse files Browse the repository at this point in the history
usleep is deprecated and replaced by nanosleep in POSIX 2008.

sleep_for is standard C++11 and internally uses nanosleep.
  • Loading branch information
neheb committed Aug 23, 2019
1 parent 9bbbee6 commit 38efa9b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/data/hash_queue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@

#include "config.h"

#include <chrono>
#include <thread>
#include <functional>
#include <rak/functional.h>
#include <unistd.h>

#include "torrent/exceptions.h"
#include "torrent/data/download_data.h"
Expand Down Expand Up @@ -135,7 +136,7 @@ HashQueue::remove(HashQueueNode::id_type id) {

while ((done_itr = m_done_chunks.find(hash_chunk)) == m_done_chunks.end()) {
pthread_mutex_unlock(&m_done_chunks_lock);
usleep(100);
std::this_thread::sleep_for(std::chrono::microseconds(100));
pthread_mutex_lock(&m_done_chunks_lock);
}

Expand Down
7 changes: 4 additions & 3 deletions src/torrent/utils/thread_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
#include "config.h"

#include <cstring>
#include <chrono>
#include <thread>
#include <signal.h>
#include <unistd.h>

#include "exceptions.h"
#include "poll.h"
Expand Down Expand Up @@ -97,7 +98,7 @@ thread_base::stop_thread_wait() {
release_global_lock();

while (!is_inactive()) {
usleep(1000);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}

acquire_global_lock();
Expand Down Expand Up @@ -161,7 +162,7 @@ thread_base::event_loop(thread_base* thread) {
}

// Add the sleep call when testing interrupts, etc.
// usleep(50);
// std::this_thread::sleep_for(std::chrono::microseconds(50));

int poll_flags = 0;

Expand Down

0 comments on commit 38efa9b

Please sign in to comment.