]> git.sesse.net Git - bmusb/blobdiff - bmusb.cpp
Add very rudimentary stopping support.
[bmusb] / bmusb.cpp
index 4b5f75a6fb95f232293a0161d2db52cf4c0ceb85..4f64d6c443c0348dbc2386d94fb8bfbd2f45e824 100644 (file)
--- a/bmusb.cpp
+++ b/bmusb.cpp
@@ -21,6 +21,7 @@
 #include <condition_variable>
 #include <thread>
 #include <stack>
+#include <atomic>
 #include "bmusb.h"
 
 using namespace std;
@@ -58,6 +59,9 @@ condition_variable queues_not_empty;
 deque<QueuedFrame> pending_video_frames;
 deque<QueuedFrame> pending_audio_frames;
 
+thread usb_thread;
+atomic<bool> should_quit;
+
 FrameAllocator::~FrameAllocator() {}
 
 #define NUM_QUEUED_FRAMES 8
@@ -373,7 +377,7 @@ end:
        }
 }
 
-void usb_thread()
+void usb_thread_func()
 {
        printf("usb thread started\n");
 
@@ -383,7 +387,7 @@ void usb_thread()
        if (sched_setscheduler(0, SCHED_RR, &param) == -1) {
                printf("couldn't set realtime priority for USB thread: %s\n", strerror(errno));
        }
-       while (true) {
+       while (!should_quit) {
                int rc = libusb_handle_events(nullptr);
                if (rc != LIBUSB_SUCCESS)
                        break;
@@ -786,7 +790,7 @@ void start_bm_capture()
                }
        }
 
-       thread(usb_thread).detach();
+       usb_thread = thread(usb_thread_func);
 
 
 #if 0
@@ -798,3 +802,9 @@ out:
        return rc;
 #endif
 }
+
+void stop_bm_capture()
+{
+       should_quit = true;
+       usb_thread.join();
+}