Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace usleep with condition_variable #318

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/torrent/utils/thread_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ thread_base::stop_thread_wait() {

release_global_lock();

while (!is_inactive()) {
usleep(1000);
{
auto lock = std::unique_lock(m_global.mutex);
m_global.cv.wait(lock, [this] { return !is_inactive(); });
Copy link
Owner

@rakshasa rakshasa Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong, you only ever acquire the global lock through acquire_global_lock() as the waiting value must be incremented.

Which means this cv implementation needs to be changed.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong, you only ever acquire the global lock through acquire_global_lock() as the waiting value must be incremented.

Which means this cv implementation is incorrect.

}

acquire_global_lock();
Expand Down
2 changes: 2 additions & 0 deletions src/torrent/utils/thread_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define LIBTORRENT_UTILS_THREAD_BASE_H

#include <atomic>
#include <condition_variable>
#include <functional>
#include <mutex>
#include <pthread.h>
Expand Down Expand Up @@ -85,6 +86,7 @@ class LIBTORRENT_EXPORT thread_base {
protected:
struct global_lock_type {
std::atomic_int waiting{0};
std::condition_variable cv;
std::mutex mutex;
};

Expand Down
Loading