ReadWrite lock using Boost.Threads (how to convert this simple class) - c++

I am porting some code from windows to Linux (Ubuntu 9.10). I have a simple class (please see below), which uses windows functions to implement simple mutex locking. I want to use Boost.Threads to reimplement this, but that library is new to me.
Can someone point out the changes I need to make to the class below, in order tomuse Boost.Threads instead of the WIN specific functions?
#ifndef __my_READWRITE_LOCK_Header__
#define __my_READWRITE_LOCK_Header__
#include <windows.h>
//Simple RW lock implementation is shown below.
#define RW_READERS_MAX 10
#define RW_MAX_SEMAPHORE_COUNT 10
#define RW_MUTEX_NAME L"mymutex"
#define RW_SEMAPHORE_NAME L"mysemaphore"
class CThreadRwLock
{
public:
CThreadRwLock()
{
InitializeCriticalSection(&m_cs);
m_hSem = CreateSemaphore(0, RW_READERS_MAX, RW_READERS_MAX, 0);
}
~CThreadRwLock()
{
DeleteCriticalSection(&m_cs);
CloseHandle(m_hSem);
}
void AcquireReaderLock()
{
EnterCriticalSection(&m_cs);
WaitForSingleObject(m_hSem, INFINITE);
LeaveCriticalSection(&m_cs);
}
void AcquireWriterLock()
{
EnterCriticalSection(&m_cs);
for(int i = 0; i < RW_READERS_MAX; i++)
{
WaitForSingleObject(m_hSem, INFINITE);
}
LeaveCriticalSection(&m_cs);
}
void ReleaseReaderLock()
{
ReleaseSemaphore(m_hSem, 1, 0);
}
void ReleaseWriterLock()
{
ReleaseSemaphore(m_hSem, RW_READERS_MAX, 0);
}
private:
CRITICAL_SECTION m_cs;
HANDLE m_hSem;
};
class CProcessRwLock
{
public:
CProcessRwLock()
{
m_h = CreateMutex(NULL, FALSE, RW_MUTEX_NAME);
m_hSem = CreateSemaphore(NULL, RW_MAX_SEMAPHORE_COUNT, RW_MAX_SEMAPHORE_COUNT, RW_SEMAPHORE_NAME);
}
~CProcessRwLock()
{
CloseHandle(m_h);
}
void AcquireReaderLock()
{
WaitForSingleObject(m_h, INFINITE);
ReleaseMutex(m_h);
}
void AcquireWriterLock()
{
WaitForSingleObject(m_h, INFINITE);
for(int i = 0; i < RW_READERS_MAX; i++)
{
WaitForSingleObject(m_hSem, INFINITE);
}
ReleaseMutex(m_h);
}
void ReleaseReaderLock()
{
ReleaseSemaphore(m_hSem, 1, 0);
}
void ReleaseWriterLock()
{
ReleaseSemaphore(m_hSem, RW_READERS_MAX, 0);
}
private:
HANDLE m_h, m_hSem;
};
class AutoThreadRwLock
{
public:
AutoThreadRwLock(const bool readlock = true):m_readlock(readlock)
{
if (readlock)
m_lock.AcquireReaderLock();
else
m_lock.AcquireWriterLock();
}
~AutoThreadRwLock()
{
if (m_readlock)
m_lock.ReleaseReaderLock();
else
m_lock.ReleaseWriterLock();
}
private:
AutoThreadRwLock(const AutoThreadRwLock&);
AutoThreadRwLock& operator= (const AutoThreadRwLock& );
CThreadRwLock m_lock ;
bool m_readlock ;
};
class AutoProcessRwLock
{
public:
AutoProcessRwLock(const bool readlock = true): m_readlock(readlock)
{
if (readlock)
m_lock.AcquireReaderLock();
else
m_lock.AcquireWriterLock();
}
~AutoProcessRwLock()
{
if (m_readlock)
m_lock.ReleaseReaderLock();
else
m_lock.ReleaseWriterLock();
}
private:
AutoProcessRwLock(const AutoProcessRwLock&);
AutoProcessRwLock& operator= (const AutoProcessRwLock&);
CProcessRwLock m_lock ;
bool m_readlock ;
};
#endif //__my_READWRITE_LOCK_Header__

I'm not going to re-write all your code for you. However you should look into boost's shared_mutex class.
Also, this question from StackOverflow shows how to use a boost::shared_mutex

Related

Unknown cause of Segmentation Fault during destruction of a ConcurrentQueue

So I was messing around with a ConcurrentQueue (found here) and I am stumbling upon a Seg Fault during the destruction of the ConcurrentQueue (Seg Faults at line 3397 of concurrentqueue.h). I tried using the queue in multiple other contexts however its is only causing a Seg Fault while being used in this context (Code below.) (Which is making me wonder if my code is causing this).
#ifndef MPSCWORKER_MPSCWORKER_HPP
#define MPSCWORKER_MPSCWORKER_HPP
#include <atomic>
#include <condition_variable>
#include <exception>
#include <memory>
#include <thread>
#include "ConcurrentQueue.h"
template <class Type>
class SinkBase;
class SinkNotRegisteredException;
template <class Type, const unsigned int numOfSinks>
class MPSCWorker{
public:
//Funcs
MPSCWorker();
~MPSCWorker(){
Type msg = {};
//Send an empty msg to the reserved channel to say we're done.
send(msg, numOfSinks + 1);
if(workerThread.joinable())
workerThread.join();
for(int i = 0; i <= numOfSinks + 1; i++){
if(sinks[i].get() != nullptr){
sinks[i].get()->onExit();
}
}
}
void start(){
isThreadRunning.store(true);
workerThread = std::thread(run,
std::ref(workQueue),
std::ref(sinks),
std::ref(isThreadRunning),
std::ref(threadCondVar),
std::ref(threadMutex));
}
int addSink(std::unique_ptr<SinkBase<Type>> &&sink, unsigned int sinkID){
if(!isThreadRunning.load()){
if(sinkID <= numOfSinks + 1){
sinks[sinkID] = std::move(sink);
if(!sinks[sinkID].get()->onInit()){
return -3;
}
} else{
return -1;
}
}else{
return -2;
}
return 0;
}
void send(Type msg, unsigned int sinkID){
workQueue.enqueue(InternalType{msg, sinkID});
threadCondVar.notify_one();
}
private:
//Types
struct InternalType{
Type msg;
unsigned int sinkID;
};
//Vars
std::atomic<bool> isThreadRunning;
std::thread workerThread;
std::condition_variable threadCondVar;
std::mutex threadMutex;
//ID = numOfSinks + 1: reserved
std::unique_ptr<SinkBase<Type>> sinks[numOfSinks + 1];
moodycamel::ConcurrentQueue<InternalType> workQueue;
//Funcs
static void run(moodycamel::ConcurrentQueue<InternalType> &workQueue,
std::unique_ptr<SinkBase<Type>> (&sinks)[numOfSinks + 1],
std::atomic<bool> &isThreadRunning,
std::condition_variable &threadCondVar,
std::mutex &threadMutex){
while(isThreadRunning.load()){
InternalType msg = {};
bool isDequeueSuccess = false;
std::unique_lock<std::mutex> lock(threadMutex);
threadCondVar.wait(lock, [&workQueue, &msg, &isDequeueSuccess](){
isDequeueSuccess = workQueue.try_dequeue(msg);
return isDequeueSuccess;
});
if(msg.sinkID == numOfSinks + 1){
isThreadRunning.store(false);
return;
}
if(sinks[msg.sinkID].get() != nullptr){
sinks[msg.sinkID].get()->onProcess(msg.msg);
}else{
throw SinkNotRegisteredException("Sink ID: " + std::to_string(msg.sinkID)
+ " has not been registered. Use addSink"
"to associate this ID with a Sink before"
"starting the worker.");
}
}
}
};
template <class Type, const unsigned int numOfSinks>
MPSCWorker<Type, numOfSinks>::MPSCWorker(){
isThreadRunning.store(false);
for(unsigned int i = 0; i <= numOfSinks +1; i++){
sinks[i] = nullptr;
}
}
class SinkNotRegisteredException : std::exception{
public:
SinkNotRegisteredException(std::string msg){this->msg = msg;}
const char* what(){return msg.c_str();}
std::string whatStr(){return std::string(msg);}
private:
std::string msg;
};
template <class Type>
class SinkBase{
public:
virtual bool onInit() = 0;
virtual void onExit() = 0;
virtual void onProcess(Type data) = 0;
};
#endif //MPSCWORKER_MPSCWORKER_HPP
Here is an example usage of the code above (I know the code is meant for multi-threaded applications but the example is single threaded for simplicity).
#include <iostream>
#include "MPSCWorker.hpp"
template <class Type>
class PrintSink : public SinkBase<Type>{
public:
virtual bool onInit(){return true;}
virtual void onExit(){}
virtual void onProcess(Type data){std::cout << data << std::endl;}
};
int main(){
MPSCWorker<std::string, 1> worker;
std::unique_ptr<PrintSink<std::string>> sink(new PrintSink<std::string>);
if(sink.get() == nullptr){
return -1;
}
worker.addSink(std::move(sink), 0);
worker.start();
worker.send("test", 0);
worker.send("test", 0);
worker.send("test", 0);
worker.send("test", 0);
worker.send("test", 0);
worker.send("test", 0);
worker.send("test", 0);
worker.send("test", 0);
worker.send("test", 0);
worker.send("test", 0);
return 0;
}
Now the real question is: Is my code causing the Seg Fault? Or is it ConcurrentQueue that is doing something wrong? And in any case, How can this be solved?

C++11 Threads Not Joining

I have experience with threads in Java but want to learn how to use them in C++11. I tried to make a simple threadpool, where threads are created once and can be asked to execute tasks.
#include <thread>
#include <iostream>
#define NUM_THREADS 2
class Worker
{
public:
Worker(): m_running(false), m_hasData(false)
{
};
~Worker() {};
void execute()
{
m_running = true;
while(m_running)
{
if(m_hasData)
{
m_system();
}
m_hasData = false;
}
};
void stop()
{
m_running = false;
};
void setSystem(const std::function<void()>& system)
{
m_system = system;
m_hasData = true;
};
bool isIdle() const
{
return !m_hasData;
};
private:
bool m_running;
std::function<void()> m_system;
bool m_hasData;
};
class ThreadPool
{
public:
ThreadPool()
{
for(int i = 0; i < NUM_THREADS; ++i)
{
m_threads[i] = std::thread(&Worker::execute, &m_workers[i]);
}
};
~ThreadPool()
{
for(int i = 0; i < NUM_THREADS; ++i)
{
std::cout << "Stopping " << i << std::endl;
m_workers[i].stop();
m_threads[i].join();
}
};
void execute(const std::function<void()>& system)
{
// Finds the first non-idle worker - not really great but just for testing
for(int i = 0; i < NUM_THREADS; ++i)
{
if(m_workers[i].isIdle())
{
m_workers[i].setSystem(system);
return;
}
}
};
private:
Worker m_workers[NUM_THREADS];
std::thread m_threads[NUM_THREADS];
};
void print(void* in, void* out)
{
char** in_c = (char**)in;
printf("%s\n", *in_c);
}
int main(int argc, const char * argv[]) {
ThreadPool pool;
const char* test_c = "hello_world";
pool.execute([&]() { print(&test_c, nullptr); });
}
The output of this is:
hello_world
Stopping 0
After that, the main thread halts, because it's waiting for the first thread to join (in the destructor of the ThreadPool). For some reason, the m_running variable of the workers is not set to false, which keeps the application running indefinitely.
In Worker::stop the member m_running is written in the main thread, while it is read in execute in a different thread. This is undefined behavior. You need to protect read/write access from different threads. In this case I would recommend using std::atomic<bool> for m_running.
Edit: the same holds for m_hasData.

Embedding matplotlib in C++

I am reading a message from a socket with C++ code and am trying to plot it interactively with matplotlib, but it seems Python code will block the main thread, no matter I use show() or ion() and draw(). ion() and draw() won't block in Python.
Any idea how to plot interactively with matplotlib in C++ code?
An example would be really good.
Thanks a lot.
You may also try creating a new thread that does the call to the
blocking function, so that it does not block IO in your main program
loop. Use an array of thread objects and loop through to find an unused
one, create a thread to do the blocking calls, and have another thread
that joins them when they are completed.
This code is a quick slap-together I did to demonstrate what I mean about
using threads to get pseudo asynchronous behavior for blocking functions...
I have not compiled it or combed over it very well, it is simply to show
you how to accomplish this.
#include <pthread.h>
#include <sys/types.h>
#include <string>
#include <memory.h>
#include <malloc.h>
#define MAX_THREADS 256 // Make this as low as possible!
using namespace std;
pthread_t PTHREAD_NULL;
typedef string someTypeOrStruct;
class MyClass
{
typedef struct
{
int id;
MyClass *obj;
someTypeOrStruct input;
} thread_data;
void draw(); //Undefined in this example
bool getInput(someTypeOrStruct *); //Undefined in this example
int AsyncDraw(MyClass * obj, someTypeOrStruct &input);
static void * Joiner(MyClass * obj);
static void * DoDraw(thread_data *arg);
pthread_t thread[MAX_THREADS], JoinThread;
bool threadRunning[MAX_THREADS], StopJoinThread;
bool exitRequested;
public:
void Main();
};
bool MyClass::getInput(someTypeOrStruct *input)
{
}
void MyClass::Main()
{
exitRequested = false;
pthread_create( &JoinThread, NULL, (void *(*)(void *))MyClass::Joiner, this);
while(!exitRequested)
{
someTypeOrStruct tmpinput;
if(getInput(&tmpinput))
AsyncDraw(this, tmpinput);
}
if(JoinThread != PTHREAD_NULL)
{
StopJoinThread = true;
pthread_join(JoinThread, NULL);
}
}
void *MyClass::DoDraw(thread_data *arg)
{
if(arg == NULL) return NULL;
thread_data *data = (thread_data *) arg;
data->obj->threadRunning[data->id] = true;
// -> Do your draw here <- //
free(arg);
data->obj->threadRunning[data->id] = false; // Let the joinThread know we are done with this handle...
}
int MyClass::AsyncDraw(MyClass *obj, someTypeOrStruct &input)
{
int timeout = 10; // Adjust higher to make it try harder...
while(timeout)
{
for(int i = 0; i < MAX_THREADS; i++)
{
if(thread[i] == PTHREAD_NULL)
{
thread_data *data = (thread_data *)malloc(sizeof(thread_data));
if(data)
{
data->id = i;
data->obj = this;
data->input = input;
pthread_create( &(thread[i]), NULL,(void* (*)(void*))MyClass::DoDraw, (void *)&data);
return 1;
}
return 0;
}
}
timeout--;
}
}
void *MyClass::Joiner(MyClass * obj)
{
obj->StopJoinThread = false;
while(!obj->StopJoinThread)
{
for(int i = 0; i < MAX_THREADS; i++)
if(!obj->threadRunning[i] && obj->thread[i] != PTHREAD_NULL)
{
pthread_join(obj->thread[i], NULL);
obj->thread[i] = PTHREAD_NULL;
}
}
}
int main(int argc, char **argv)
{
MyClass base;
base.Main();
return 0;
}
This way you can continue accepting input while the draw is occurring.
~~Fixed so the above code actually compiles, make sure to add -lpthread

linux C++ thread in class

Hi i want to do the class with method which will start in separate thread after creating class. That how i do that:
class Devemu {
int VarInc;
void Increm() {
for(;;) {
if (VarInc > 532) VarInc = 0;
else VarInc++;
}
}
public:
static void* IncWrapper(void* thisPtr) {
((Devemu*) thisPtr)->Increm();
return NULL;
}
Devemu() {
VarInc = 0;
}
int Var() {
return VarInc;
}
};
int main(int argc, char** argv) {
Devemu* em = new Devemu();
pthread_t thread_id;
pthread_create(&thread_id, NULL, &Devemu::IncWrapper, NULL);
for(int i = 0 ;i < 50; i++) {
printf("%d\n", em->Var());
}
return (EXIT_SUCCESS);
}
I unlike that pthread_create in main and IncWrapper method can i change that?
Yes, you can put it in the constructor if you like :
class Devemu {
int VarInc;
pthread_t thread_id;
void Increm() {
for(;;) {
if (VarInc > 532) VarInc = 0;
else VarInc++;
}
}
public:
static void* IncWrapper(void* thisPtr) {
((Devemu*) thisPtr)->Increm();
return NULL;
}
Devemu() {
VarInc = 0;
pthread_create(&thread_id, NULL, &Devemu::IncWrapper, NULL);
}
int Var() {
return VarInc;
}
};
I suppose it's better to put the thread creation in the separate member-function like that:
class Devemu {
...
void run()
{
pthread_create(&thread_id, NULL, &Devemu::IncWrapper, NULL);
}
...
};
Not actually in ctor, because sometimes you (or anyone who uses your code) can forget that thread created in the ctor and start coding like:
Devemu m;
...
Devemu m1;
...
creating unnecessary threads just like instances of the class.
If you want to get working source code you need make next changes:
--- so0.cpp 2019-11-04 11:26:11.101984795 +0000
+++ so1.cpp 2019-11-04 11:26:57.108501816 +0000
## -1,3 +1,7 ##
+#include "stdio.h"
+#include <pthread.h>
+#include <cstdlib>
+
class Devemu {
int VarInc;
## -24,7 +28,7 ##
Devemu* em = new Devemu();
pthread_t thread_id;
-pthread_create(&thread_id, NULL, &Devemu::IncWrapper, NULL);
+pthread_create(&thread_id, NULL, &Devemu::IncWrapper, em);
My variant for resolving your problem is :
#include "stdio.h"
#include <pthread.h>
#include <cstdlib>
#include <unistd.h>
class Devemu {
private:
int VarInc;
pthread_attr_t attr; /* отрибуты потока */
pthread_t thread_id;
void Increm();
public:
static void* IncWrapper (void* thisPtr);
Devemu();
~Devemu();
int Var();
};
void Devemu::Increm() {
while (true) {
if (VarInc > 532) { VarInc = 0; } else { VarInc++; }
}
}
void* Devemu::IncWrapper(void* thisPtr) {
((Devemu*) thisPtr)->Increm();
return NULL;
}
Devemu::~Devemu() {
pthread_cancel (thread_id);
}
Devemu::Devemu() {
VarInc = 0;
/// get default value of arrts
pthread_attr_init(&attr);
/// start thread
pthread_create(&thread_id, &attr, &Devemu::IncWrapper, this);
}
int Devemu::Var() {
return VarInc;
}
int main(int argc, char** argv) {
Devemu* em = new Devemu();
for(int i = 0 ; i < 100; i++) {
printf("%d\n", em->Var());
usleep (10);
}
delete em;
usleep (1000);
return (EXIT_SUCCESS);
}

Inner class and initialisation

I have a class defined like this: This is not all complete and probably won't compile.
class Server
{
public:
Server();
~Server();
class Worker
{
public:
Worker(Server& server) : _server(server) { }
~Worker() { }
void Run() { }
void Stop() { }
private:
Server& _server;
}
void Run()
{
while(true) {
// do work
}
}
void Stop()
{
// How do I stop the thread?
}
private:
std::vector<Worker> _workers;
};
My question is, how do I initialize the workers array passing in the outer class named Server.
What I want is a vector of worker threads. Each worker thread has it's own state but can access some other shared data (not shown). Also, how do I create the threads. Should they be created when the class object is first created or externally from a thread_group.
Also, how do I go about shutting down the threads cleanly and safely?
EDIT:
It seems that I can initialize Worker like this:
Server::Server(int thread_count)
: _workers(thread_count), Worker(*this)), _thread_count(thread_count) { }
And I'm currently doing this in Server::Run to create the threads.
boost::thread_group _threads; // a Server member variable
Server::Run(){
for (int i = 0; i < _thread_count; i++)
_threads.create_thread(boost::bind(&Server::Worker::Run, _workers(i)));
// main thread.
while(1) {
// Do stuff
}
_threads.join_all();
}
Does anyone see any problems with this?
And how about safe shutdown?
EDIT:
One problem I have found with it is that the Worker objects don't seem to get constructed!
oops. Yes they do I need a copy constructor on the Worker class.
But oddly, creating the threads results in the copy constructor for Worker being called multiple times.
I have done it with pure WINAPI, look:
#include <stdio.h>
#include <conio.h>
#include <windows.h>
#include <vector>
using namespace std;
class Server
{
public:
class Worker
{
int m_id;
DWORD m_threadId;
HANDLE m_threadHandle;
bool m_active;
friend Server;
public:
Worker (int id)
{
m_id = id;
m_threadId = 0;
m_threadHandle = 0;
m_active = true;
}
static DWORD WINAPI Run (LPVOID lpParam)
{
Worker* p = (Worker*) lpParam; // it's needed because of the static modifier
while (p->m_active)
{
printf ("I'm a thread #%i\n", p->m_id);
Sleep (1000);
}
return 0;
}
void Stop ()
{
m_active = false;
}
};
Server ()
{
m_workers = new vector <Worker*> ();
m_count = 0;
}
~Server ()
{
delete m_workers;
}
void Run ()
{
puts ("Server is run");
}
void Stop ()
{
while (m_count > 0)
RemoveWorker ();
puts ("Server has been stopped");
}
void AddWorker ()
{
HANDLE h;
DWORD threadId;
Worker* n = new Worker (m_count ++);
m_workers->push_back (n);
h = CreateThread (NULL, 0, Worker::Run, (VOID*) n, CREATE_SUSPENDED, &threadId);
n->m_threadHandle = h;
n->m_threadId = threadId;
ResumeThread (h);
}
void RemoveWorker ()
{
HANDLE h;
DWORD threadId;
if (m_count <= 0)
return;
Worker* n = m_workers->at (m_count - 1);
m_workers->pop_back ();
n->Stop ();
TerminateThread (n->m_threadHandle, 0);
m_count --;
delete n;
}
private:
int m_count;
vector <Worker*>* m_workers;
};
int main (void)
{
Server a;
int com = 1;
a.Run ();
while (com)
{
if (kbhit ())
{
switch (getch ())
{
case 27: // escape key code
com = 0;
break;
case 'a': // add worker
a.AddWorker ();
break;
case 'r': // remove worker
a.RemoveWorker ();
break;
}
}
}
a.Stop ();
return 0;
}
There are no synchronization code here, because I haven't enougth time to do it... But I wish it will help you =)
Have you looked at boost asio at all? It looks like it could be a good fit for what you are trying to do. Additionally you can call boost asio's io_service's run (similar to your Run method) from many threads i.e. you can process your IO in many threads. Also of interest could be http://think-async.com/Asio/Recipes for an asio based thread-pool.
Have a look at the asio examples. Perhaps they offer an alternative way of handling what you are trying to do. Esp. have a look at how a clean shutdown is accomplished.