Running background process in c++ using pthread - c++

I am developing an API in c++ to be used in iOS and Android development.
Hence, I need to use pthread.
Now I have a function, which sends data to the server after serialization of a queue.
//include headers here
void sendToServer(queue q) {
/*
send the whole queue to server after serialization
pop the queue
*/
}
which is called by
// headers
void log (data)
{
while(1)
{/*
add data to queue q
*/
if(num_nodes>=threshold || duration > interval)
sendToServer(q);
//apply thread wait condition
}
}
If i want to make a separate thread for log which runs in the background, can I implement a singleton class with a method get_instance which starts a thread with log
class C
{
private:
C();
/*disallow copy constructor and assignment operator*/
static C* singleton_inst;
pthread_t t;
void sendToServer(queue q);
public:
void* log(void*);
static C* get_instance()
{
if(singleton_inst==NULL)
{
pthread_create(t, NULL, log, NULL);
singleton_inst= new C();
}
return singleton_inst;
}
}
So, next time in my test function, when i do:
C::get_instance();
//C::get_instance->signal_thread_to_resume;
will the second line resume the same thread started in the first line?

If you really have to use pthread I think this will work, but it is untested:
#include <iostream>
#include <pthread.h>
using namespace std;
//include headers here
/*the called function must be void* (void*) */
/* so you have to cast queue*/
void* sendToServer(void* q) {
/*
send the whole queue to server after serialization
pop the queue
*/
}
pthread_t thread1;
// headers
void log (char* data)
{
// create the thread
int th1 = pthread_create( &thread1, NULL, sendToServer, (void*) data);
}
int main() {
log((char*)"test");
/* Wait till threads are complete before main continues. Unless we */
/* wait we run the risk of executing an exit which will terminate */
/* the process and all threads before the threads have completed. */
pthread_join( thread1, NULL);
return 0;
}
Don't forget to link with the pthread libary. Without it, it will not work.
But try to use std::thread.

Related

How can I tell when my ThreadPool is finished with its tasks?

In c++11, I have a ThreadPool object which manages a number of threads that are enqueued via a single lambda function. I know how many rows of data I have to work on and so I know ahead of time that I will need to queue N jobs. What I am not sure about is how to tell when all of those jobs are finished, so I can move on to the next step.
This is the code to manage the ThreadPool:
#include <cstdlib>
#include <vector>
#include <deque>
#include <iostream>
#include <atomic>
#include <thread>
#include <mutex>
#include <condition_variable>
class ThreadPool;
class Worker {
public:
Worker(ThreadPool &s) : pool(s) { }
void operator()();
private:
ThreadPool &pool;
};
class ThreadPool {
public:
ThreadPool(size_t);
template<class F>
void enqueue(F f);
~ThreadPool();
void joinAll();
int taskSize();
private:
friend class Worker;
// the task queue
std::deque< std::function<void()> > tasks;
// keep track of threads
std::vector< std::thread > workers;
// sync
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
};
void Worker::operator()()
{
std::function<void()> task;
while(true)
{
{ // acquire lock
std::unique_lock<std::mutex>
lock(pool.queue_mutex);
// look for a work item
while ( !pool.stop && pool.tasks.empty() ) {
// if there are none wait for notification
pool.condition.wait(lock);
}
if ( pool.stop ) {// exit if the pool is stopped
return;
}
// get the task from the queue
task = pool.tasks.front();
pool.tasks.pop_front();
} // release lock
// execute the task
task();
}
}
// the constructor just launches some amount of workers
ThreadPool::ThreadPool(size_t threads)
: stop(false)
{
for (size_t i = 0;i<threads;++i) {
workers.push_back(std::thread(Worker(*this)));
}
//workers.
//tasks.
}
// the destructor joins all threads
ThreadPool::~ThreadPool()
{
// stop all threads
stop = true;
condition.notify_all();
// join them
for ( size_t i = 0;i<workers.size();++i) {
workers[i].join();
}
}
void ThreadPool::joinAll() {
// join them
for ( size_t i = 0;i<workers.size();++i) {
workers[i].join();
}
}
int ThreadPool::taskSize() {
return tasks.size();
}
// add new work item to the pool
template<class F>
void ThreadPool::enqueue(F f)
{
{ // acquire lock
std::unique_lock<std::mutex> lock(queue_mutex);
// add the task
tasks.push_back(std::function<void()>(f));
} // release lock
// wake up one thread
condition.notify_one();
}
And then I distribute my job among threads like this:
ThreadPool pool(4);
/* ... */
for (int y=0;y<N;y++) {
pool->enqueue([this,y] {
this->ProcessRow(y);
});
}
// wait until all threads are finished
std::this_thread::sleep_for( std::chrono::milliseconds(100) );
Waiting for 100 milliseconds works just because I know those jobs can complete in less time than 100ms, but obviously its not the best approach. Once it has completed N rows of processing it needs to go through another 1000 or so generations of the same thing. Obviously, I want to begin the next generation as soon as I can.
I know there must be some way to add code into my ThreadPool so that I can do something like this:
while ( pool->isBusy() ) {
std::this_thread::sleep_for( std::chrono::milliseconds(1) );
}
I've been working on this for a couple nights now and I find it hard to find good examples of how to do this. So, what would be the proper way to implementat my isBusy() method?
I got it!
First of all, I introduced a few extra members to the ThreadPool class:
class ThreadPool {
/* ... exisitng code ... */
/* plus the following */
std::atomic<int> njobs_pending;
std::mutex main_mutex;
std::condition_variable main_condition;
}
Now, I can do better than checking some status every X amount of time. Now, I can block the Main loop until no more jobs are pending:
void ThreadPool::waitUntilCompleted(unsigned n) {
std::unique_lock<std::mutex> lock(main_mutex);
main_condition.wait(lock);
}
As long as I manage what's pending with the following bookkeeping code, at the head of the ThreadPool.enqueue() function:
njobs_pending++;
and right after I run the task in the Worker::operator()() function:
if ( --pool.njobs_pending == 0 ) {
pool.main_condition.notify_one();
}
Then the main thread can enqueue whatever tasks are necessary and then sit and wait until all calculations are completed with:
for (int y=0;y<N;y++) {
pool->enqueue([this,y] {
this->ProcessRow(y);
});
}
pool->waitUntilCompleted();
You may need to create an internal structure of threads associated with a bool variable flag.
class ThreadPool {
private:
// This Structure Will Keep Track Of Each Thread's Progress
struct ThreadInfo {
std::thread thread;
bool isDone;
ThreadInfo( std::thread& threadIn ) :
thread( threadIn ), isDone(false)
{}
}; // ThredInfo
// This Vector Should Be Populated In The Constructor Initially And
// Updated Anytime You Would Add A New Task.
// This Should Also Replace // std::vector<std::thread> workers
std::vector<ThreadInfo> workers;
public:
// The rest of your class would appear to be the same, but you need a
// way to test if a particular thread is currently active. When the
// thread is done this bool flag would report as being true;
// This will only return or report if a particular thread is done or not
// You would have to set this variable's flag for a particular thread to
// true when it completes its task, otherwise it will always be false
// from moment of creation. I did not add in any bounds checking to keep
// it simple which should be taken into consideration.
bool isBusy( unsigned idx ) const {
return workers[idx].isDone;
}
};
If you have N jobs and they have to be awaited for by calling thread sleep, then the most efficient way would be to create somewhere a variable, that would be set by an atomic operation to N before scheduling jobs and inside each job when done with computation, there would be atomic decrement of the variable. Then you can use atomic instruction to test if the variable is zero.
Or locked decrement with wait handles, when the variable would decrement to zero.
I just have to say, I do not like this idea you are asking for:
while ( pool->isBusy() ) {
std::this_thread::sleep_for( std::chrono::milliseconds(1) );
}
It just does not fit well, it won't be 1ms almost never, it is using resources needlessly etc...
The best way would be to decrement some variable atomically, and test atomically the variable if all done and the last job will simply based on atomic test set WaitForSingleObject.
And if you must, the waiting will be on WaitForSingleObject, and would woke up after completion, not many times.
WaitForSingleObject

How to return data from OpenCV or C++ Thread function to main thread in case of windows os?

I am not much familiar with openCV or C++ threading.
The main problem is I am passing some data to the thread function, it does some processing and after that IT NEEDS TO RESEND THE PROCESSED DATA.
In Win 32 or VC++ this return data we can post/postthread through a message to the main thread in case of UI Threads, but in case of worker threads there is no facility to return the data.
What about openCV or C++ threading case how to send the return data to the main thread? Can you please give me idea how to do this.
The code I am using is as like below
// .h file
#define MAX_THREADS 3
#include "windows.h"
typedef struct MyData {
unsigned char* colorPixelData;
uint32* punTIFFImageData;
int ii;
int jj;
int hh;
int ww;
int nWidth;
int kk;
} MYDATA, *PMYDATA;
void MyThreadFunction(void *arg);
and
// .cpp file, Main thread
PMYDATA pDataArray[MAX_THREADS];
PMYDATA pReturnedDataArray[MAX_THREADS];
HANDLE hThreadArray[MAX_THREADS];
//some code
hThreadArray[count] = CreateThread(
NULL,
0,
(LPTHREAD_START_ROUTINE)MyThreadFunction,
(void *)&pDataArray[count], // argument to thread function
0,
NULL);
WaitForMultipleObjects(MAX_THREADS, hThreadArray, TRUE, INFINITE);
//called thread function
void MyThreadFunction(void *arg)
{
//NEED TO RETURN DATA FROM HERE TO MAIN THREAD pReturnedDataArray
}
Use global variables instead so other threads can read/write on them.
By doing this you don't need to worry about a thread "returning something".
You can use std::thread that is supported in C++11 standard. Then you can pass your pDataArray[count] and as many other variables as you want so you don't have to return anything to get your output. You can get more informations about this class here:
http://www.cplusplus.com/reference/thread/thread/

Unable to receive a message using message_queue in Boost thread

I have a requirement for creating a Event based Multi-thread application for which i am trying to use boost::thread and boost/interprocess/ipc/message_queue for sending messages between threads.
What i am doing currently is making the thread wait in its workerfunction to wait for a message.
Actually this is just for basic start where the sender and receiver both is a same thread, on later stage i have thought to store a list of message_queue corresponding for each thread and then fetch it accordingly or something like that.
But now, as per the code below i am using
//in a common class
typedef struct s_Request{
int id;
}st_Request;
//in thread(XYZ) class
st_Request dataone;
message_queue *mq;
void XYZ::threadfunc(void *ptr)
{
XYZ*obj = (XYZ*) ptr;
obj->RecieveMsg();
}
void XYZ::RecieveMsg()
{
message_queue mq1(open_only,"message_queue");
if(!(mq1.try_receive(&dataone, sizeof(st_Request), recvd_size, priority)))
printf("msg not received");
printf("id = %d",dataone.id);
}
void XYZ::Create()
{
mq= new message_queue(open_or_create,"message_queue",100,sizeof(st_Request));
boost:thread workerthread(threadfunc,this);
workerthread.join();
}
void XYZ::Send(st_Request *data)
{
if (!(mq->try_send(data, sizeof(st_Request), 0)))
printf("message sending failed");
}
//I am calling it like
class ABC: public XYZ
{
..some functions to do stuff... };
void ABC::createMSGQ()
{
create();
st_Request *data;
data->id =10;
send(data);
}
My thread is waiting in RecieveMsg but i am not getting any msg and the prints are coming till Send function entry and than the code crash.
Please Guide me for what i am doing wrong, if the approach is entirely wrong, i am open to move to new approach.
P.s. this is my first question on stack overflow i tried follow the guidelines still if i strayed away anywhere please do correct.
st_Request *data;
data->id =10;
data is uninitialized, you cannot dereference it. Pointers should point to something before you dereference them.
I don't understand the point of this function:
void XYZ::Create()
{
mq= new message_queue(open_or_create,"message_queue",100,sizeof(st_Request));
boost:thread workerthread(threadfunc,this);
workerthread.join();
}
You create a new thread, then block and wait for it to finish so you can join it. Why not just do the work here, instead of creating a new thread and waiting for it to finish?
What is threadfunc? Do you mean ThreadFunc?
This function is written strangely:
void XYZ::ThreadFunc(void *ptr)
{
XYZ*obj = (XYZ*) ptr;
obj->RecieveMsg();
}
Why not pass the argument as XYZ* instead of void*? Boost.Thread doesn't require everything to be passed as void*. Is that function static? It doesn't need to be:
struct XYZ {
void threadFunc();
void create();
void recv();
};
void XYZ::threadFunc()
{
recv();
}
void XYZ::create()
{
boost::thread thr(&XYZ::threadFunc, this);
thr.join();
}

Thread Safe queue in C++

Is this the correct way to make a Thread Safe Queue in C++ which can handle unsigned char* arrays of binary data?
Notice that in the data is produced from the main thread and not a created pthread, which makes me question if the pthread_mutex_t will actually work correctly on the push and pop.
Thread Safe Queue
#include <queue>
#include <pthread.h>
class ts_queue
{
private:
std::queue<unsigned char*> _queue_;
pthread_mutex_t mutex;
pthread_cond_t cond;
public:
ts_queue()
{
pthread_mutex_init(&mutex, NULL);
pthread_cond_init(&cond, NULL);
}
void push(unsigned char* data)
{
pthread_mutex_lock(&mutex);
_queue_.push(data);
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mutex);
}
void pop(unsigned char** popped_data)
{
pthread_mutex_lock(&mutex);
while (_queue_.empty() == true)
{
pthread_cond_wait(&cond, &mutex);
}
*popped_data = _queue_.front();
_queue_.pop();
pthread_mutex_unlock(&mutex);
}
};
CONSUMER TEST:
void *consumer_thread(void *arguments)
{
ts_queue *tsq = static_cast<ts_queue*>(arguments);
while (true)
{
unsigned char* data = NULL;
tsq->pop(&data);
if (data != NULL)
{
// Eureka! Received from the other thread!!!
// Delete it so memory keeps free.
// NOTE: In the real scenario for which I need
// this class, the data received are bitmap pixels
// and at this point it would be processed
delete[] data;
}
}
return 0;
}
PRODUCER TEST:
void main()
{
ts_queue tsq;
// Create the consumer
pthread_t consumer;
pthread_create(&consumer, NULL, consumer_thread, &tsq));
// Start producing
while(true)
{
// Push data.
// Expected behaviour: memory should never run out, as the
// consumer should receive the data and delete it.
// NOTE: test_data in the real purpose scenario for which I
// need this class would hold bitmap pixels, so it's meant to
// hold binary data and not a string
unsigned char* test_data = new unsigned char [8192];
tsq.push(test_data);
}
return 0;
}
How do you know the consumer never gets the data? When I try your program out, I get a segmentation fault, and GDB tells me the consumer did get a pointer, but it's an invalid one.
I believe your problem is that you have a data race on the _queue_ member. push() calls _queue_.push(data) (a write on _queue_) while holding push_mutex and pop() calls _queue_.front() (a read on _queue_) and _queue_.pop() (another write on _queue_) while holding pop_mutex, but push() and pop() can occur at the same time, causing both threads to be writing (and reading) _queue_ at the same time, a classical data-race.

c++ multithread

I use C++ to implement a thread class. My code shows in the following.
I have a problem about how to access thread data.
In the class Thread, I create a thread use pthread_create() function. then it calls EntryPoint() function to start thread created. In the Run function, I want to access the mask variable, it always shows segment fault.
So, my question is whether the new created thread copy the data in original class? How to access the thread own data?
class Thread {
public:
int mask;
pthread_t thread;
Thread( int );
void start();
static void * EntryPoint (void *);
void Run();
};
Thread::Thread( int a) {
mask =a;
}
void Thread::Run() {
cout<<"thread begin to run" <<endl;
cout << mask <<endl; // it always show segmentfault here
}
void * Thread::EntryPoint(void * pthis) {
cout << "entry" <<endl;
Thread *pt = (Thread *) pthis;
pt->Run();
}
void Thread::start() {
pthread_create(&thread, NULL, EntryPoint, (void *)ThreadId );
pthread_join(thread, NULL);
}
int main() {
int input_array[8]={3,1,2,5,6,8,7,4};
Thread t1(1);
t1.start();
}
I'm not familiar with the libraries you're using, but how does EntryPoint know that pthis is a pointer to Thread? Thread (this) does not appear to be passed to pthread_create.
It's great that you're attempting to write a Thread class for educational purposes. However, if you're not, why reinvent the wheel?
pThis is most likely NULL, you should double check that you're passing the correct arguments to pthread_create.
Basically, the problem is as soon as you start your thread, main exits and your local Thread instance goes out of scope. So, because the lifetime of your thread object is controlled by another thread, you've already introduced a race condition.
Also, I'd consider joining a thread immediately after you've created it in Thread::start to be a little odd.