vichargrave / wqueue Goto Github PK
View Code? Open in Web Editor NEWExample code from Multithreaded Work Queue in C++ blog.
Home Page: https://vichargrave.github.io/programming/multithreaded-work-queue-in-cpp/
Example code from Multithreaded Work Queue in C++ blog.
Home Page: https://vichargrave.github.io/programming/multithreaded-work-queue-in-cpp/
Hi Vic, first of all thanks for sharing your code! It was just the missing peace to solve issues in my project!
Unfortunatly your code does not safely destroy the mutex. The documentation says:
It shall be safe to destroy an initialized mutex that is unlocked. Attempting to destroy a locked mutex, or a mutex that another thread is attempting to lock, or a mutex that is being used in a pthread_cond_timedwait() or pthread_cond_wait() call by another thread, results in undefined behavior.
Thats exactly what happens sometimes ;(
In my case ~wqueue()
gets called from another thread while your wqueue::remove()
method just executes pthread_cond_wait. However, pthread_mutex_destroy(&m_mutex)
should never get called at this time. pthread_cond_wait
checks at the beginning for destroyed mutex. That's why it doesn't always crash, but sometimes the destroy happens after that check ;(
To reproduce I open and immediately close the dialog that initializes and destroys the thread very often.
To safely destroy the thread probably requires a design change.
In Java I solved that problem with a work item that stops the thread. Maybe that would not require a vast design change.
I try to test it but there is a compile error like:
g++ -c -Wall -I../threads main.cpp -o main.o
g++ -c -Wall -I../threads ../threads/thread.cpp -o ../threads/thread.o
g++ -lpthread main.o ../threads/thread.o -o wqueue
../threads/thread.o: In function Thread::~Thread()': thread.cpp:(.text+0x9b): undefined reference to
pthread_detach'
thread.cpp:(.text+0xb7): undefined reference to pthread_cancel' ../threads/thread.o: In function
Thread::start()':
thread.cpp:(.text+0x11e): undefined reference to pthread_create' ../threads/thread.o: In function
Thread::join()':
thread.cpp:(.text+0x16c): undefined reference to pthread_join' ../threads/thread.o: In function
Thread::detach()':
thread.cpp:(.text+0x1c0): undefined reference to `pthread_detach'
collect2: error: ld returned 1 exit status
Makefile:12: recipe for target 'wqueue' failed
make: *** [wqueue] Error 1
Moreover, I want to get results back to the main().
How can I do it?
Best thanks for you help.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.