How can I Resume Thread once join() is called? - c++

This is my sender thread once after it is called for first time its finish its execution. I Couldn't be able to resume this sender thread. Is There any mechanism in C++ to resume threads.
void ClientSocket::sender()
{
char buf[1024];
//readBuffer = m_ptrsendStream->Read_Adt(filePath);
//readStream();
//cout << readBuffer.str()<<endl;
cout << "write stream to send through socket\n" << endl;
cin >> buf;
if (isConnected == 0)
{
//send(clientSock, readBuffer.str().c_str(), strlen((char *)readBuffer.str().c_str()), 0);
send(clientSock, buf, strlen(buf), 0);
cout << "sending stream :\n"<<endl << buf << endl;
}
}
//this is where my thread creation happens and join() happens.
int main(int argc, char *argv[])
{
ClientSocket objSocket(argv[1]);
sender_thread = make_shared<thread>([&objSocket]() {
objSocket.sender();
});
try
{
if (sender_thread->joinable())
sender_thread->join();
}

No, once your thread has joined it's done and you need to create a new one.
If you have this pattern where you are constantly creating new threads it might be worthwhile to think about using a threadpool to avoid the overhead of constantly spawning new threads.
In addition, if this is related to networking it's probably best to avoid using threads and instead use something asynchronous like boost::asio.

Terminated threads cannot be resumed (this is not a C++ limitation, but a general limitation; when speaking about resuming thread, it is usually about resuming after previously suspending it).
After join() has returned, corresponding thread is already terminated; it has no state (except maybe for zobmie stuff and return code, but this is of no use for your purposes), and there is nothing to resume
However, it is possible to run your sender() function in another thread, just create another instance of your thread.
EDIT: I concur with #inf on using asio instead of threads whenever possible.

You want resume thread which is completed , normally thread resume used continue from suspended threads . Instead of resuming the thread ,stop come of thread un till it finish all actions , make use of while or wait in thread .

Related

C++ cancelling a pthread using a secondary thread stuck in function call

I'm having trouble instituting a timeout in one of my pthreads. I've simplified my code here and I've isolated the issue to be the CNF algorithm I'm running in the thread.
int main(){
pthread_t t1;
pthread_t t2;
pthread_t t3; //Running multiple threads, the others work fine and do not require a timeout.
pthread_create(&t1, nullptr, thread1, &args);
pthread_join(t1, nullptr);
std::cout << "Thread should exit and print this\n"; //This line never prints since from what I've figured to be a lack of cancellation points in the actual function running in the thread.
return 0;
}
void* to(void* args) {
int timeout{120};
int count{0};
while(count < timeout){
sleep(1);
count++;
}
std::cout << "Killing main thread" << std::endl;
pthread_cancel(*(pthread_t *)args);
}
void *thread1 (void *arguments){
//Create the timeout thread within the CNF thread to wait 2 minutes and then exit this whole thread
pthread_t time;
pthread_t cnf = pthread_self();
pthread_create(&time, nullptr, &timeout, &cnf);
//This part runs and prints that the thread has started
std::cout << "CNF running\n";
auto *args = (struct thread_args *) arguments;
int start = args->vertices;
int end = 1;
while (start >= end) {
//This is where the issue lies
cover = find_vertex_cover(args->vertices, start, args->edges_a, args->edges_b);
start--;
}
pthread_cancel(time); //If the algorithm executes in the required time then the timeout is not needed and that thread is cancelled.
std::cout << "CNF END\n";
return nullptr;
}
I tried commenting out the find_vertex_cover function and add an infinite loop and I was able to create a timeout and end the thread that way. The function is actually working the exact way it should. It should take forever to run under the conditions I'm running it at and therefore I need a timeout.
//This was a test thread function that I used to validate that implementing the timeout using `pthread_cancel()` this way works. The thread will exit once the timeout is reached.
void *thread1 (void *args) {
pthread_t x1;
pthread_t x2 = pthread_self();
pthread_create(&x1, nullptr, to, &x2);
/*
for (int i = 0;i<100; i++){
sleep(1);
std::cout << i << std::endl;
}
*/
}
Using this function I was able to validate that my timeout thread approach worked. The issue is when I actually run the CNF algorithm (using Minisat under the hood) once find_vertex_cover runs, there is no way to end the thread. The algorithm is expected to fail in the situation I'm implementing which is why a timeout is being implemented.
I've read up on using pthread_cancel() and while it isn't a great way it's the only way I could implement a timeout.
Any help on this issue would be appreciated.
I've read up on using pthread_cancel() and while it isn't a great way [..]
That's right. pthread_cancel should be avoided. It's especially bad for use in C++ as it's incompatible with exception handling. You should use std::thread and for thread termination, you can possibly use conditional variable or a atomic variable that terminates the "infinite loop" when set.
That aside, cancellation via pthread_cancel depends on two things: 1) cancellation state 2) cancellation type.
Default cancellation state is enabled. But the default cancellation type is deferred - meaning the cancellation request will be delivered only at the next cancellation point. I suspect there's any cancellation points in find_vertex_cover. So you could set the cancellation type to asynchronous via the call:
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
from the thread(s) you want to be able to cancel immediately.
But again, I suggest to not go for pthread_cancel approach at all and instead rewrite the "cancel" logic so that it doesn't involve pthread_cancel.

Synchronising main thread and worker thread

In QT, from main(GUI) thread I am creating a worker thread to perform a certain operation which accesses a resource shared by both threads. On certain action in GUI, main thread has to manipulate the resource. I tried using QMutex to lock that particular resource. This resource is continuously used by the worker thread, How to notify main thread on this?
Tried using QWaitCondition but it was crashing the application.
Is there any other option to notify and achieve synchronisation between threads?
Attached the code snippet.
void WorkerThread::IncrementCounter()
{
qDebug() << "In Worker Thread IncrementCounter function" << endl;
while(stop == false)
{
mutex.lock();
for(int i = 0; i < 100; i++)
{
for(int j = 0; j < 100; j++)
{
counter++;
}
}
qDebug() << counter;
mutex.unlock();
}
qDebug() << "In Worker Thread Aborting " << endl;
}
//Manipulating the counter value by main thread.
void WorkerThread::setCounter(int value)
{
waitCondition.wait(&mutex);
counter = value;
waitCondition.notify_one();
}
You are using the wait condition completely wrong.
I urge you to read up on mutexes and conditions, and maybe look at some examples.
wait() will block execution until either notify_one() or notify_all() is called somewhere. Which of course cannot happen in your code.
You cannot wait() a condition on one line and then expect the next two lines to ever be called if they contain the only wake up calls.
What you want is to wait() in one thread and notify_XXX() in another.
You could use shared memory from within the same process. Each thread could lock it before writing it, like this:
QSharedMemory *shared=new QSharedMemory("Test Shared Memory");
if(shared->create(1,QSharedMemory::ReadWrite))
{
shared->lock();
// Copy some data to it
char *to = (char*)shared->data();
const char *from = &dataBuffer;
memcpy(to, from, dataSize);
shared->unlock();
}
You should also lock it for reading. If strings are wanted, reading strings can be easier that writing them, if they are zero terminated. You'll want to convert .toLatin1() to get a zero-terminated string which you can get the size of a string. You might get a lock that multiple threads can read from, with shared->attach(); but that's more for reading the shared memory of a different process..
You might just use this instead of muteces. I think if you try to lock it, and something else already has it locked, it will just block until the other process unlocks it.

pthread leaks memory after it finishes

I know by calling pthread_join or pthread_detach will release resources used by the thread after it finishes but my situation is not that easy.
First of all I want to be able to terminate the child thread from the parent thread, so I wrote something like this: (the dynamically allocated buf is there to show I can't seem to find a way to use pthread_detach because I don't know how to wait for the child thread to finish (so I can free buf) if I detach it)
bool running = true;
void *foo(void *buf)
{
while (running)
{
// Do something with buf
}
return NULL;
}
int main(int argc, char **argv)
{
char *buf = new char[1024];
pthread_t tid;
pthread_create(&tid, NULL, foo, buf);
string cmd;
while (true)
{
cin >> cmd;
if (!cmd.compare("stop"))
{
running = false;
pthread_join(tid, NULL);
delete[] buf;
}
else
{
// Do something
}
}
return 0;
}
This seems to work. But the child thread sometimes finishes before the parent thread ever wants to terminate it. In this case, the parent thread is blocked so
How do I inform the parent thread that the child thread has terminated so the parent thread can call pthread_join?
Is there a way to use pthread_detach and still be able to wait for the child thread to finish so I can free buf afterwards (though it seems I can free buf in the child thread in this demo, it's impossible for my real application)?
How do I inform the parent thread that the child thread has terminated so the parent thread can call pthread_join?
There are all sorts of options. Here are three reasonably promising ones:
You could have the child thread set a global flag variable.
You could have the child thread signal the parent thread with pthread_kill().
You could set up a pipe that the child writes to when it's done, and have the parent perform a non-blocking read from it to test whether the child is finished.
Is there a way to use pthread_detach and still be able to wait for the child thread to finish so I can free buf afterwards (though it
seems I can free buf in the child thread in this demo, it's impossible
for my real application)?
No. Once a thread is detached it cannot be joined, ever.
You could consider instead having the thread manage its own resources. Or if the main thread must allocate resources then at least consider handing off responsibility for managing them to the child thread.
I'm going to head off in a different direction. The question should not be "How do I signal end of thread so I can release buf?" it should be "How should I assign ownership for buf, preferably in such a way that the end of the thread does not matter?"
Either foo or main should release buf. So which?
Case 1: buf is allocated but unused by main
This case includes buf allocated and initialized in main so long as main does not consume buf after thread has finished processing. This case is easy. Thread assumes ownership of buf and deletes it when thread is finished.
void *foo(void *buf)
{
while (running)
{
// Do something with buf
}
delete buf;
return NULL;
}
Case 2: buf is allocated by main, operated on by foo, then consumed by main.
Obviously buf must be alive for main to consume it, so main should retain ownership and delete buf after consuming. But when? This means it must wait for foo to finish processing buf, and this means either join or a done message. Now we get to John Bollinger's answer.
Case 3: buf is allocated in foo, operated on by foo, and deleted by foo.
This has the advantage of no confusion what-so-ever, and if main never touches
buf, main should be completely hands-off, including allocation. In a perfect world this looks like:
void *foo(void * )
{
char buf[1024];
while (running)
{
// Do something with buf
}
return NULL;
}
The size of the buffer, if known to main, but not to foo, can be provided to the thread. For example,
pthread_create(&tid, NULL, foo, &bufsize);
and
void *foo(void * size)
{
char * buf = new char[*((int*)size)];
while (running)
{
// Do something with buf
}
delete buf;
return NULL;
}
but you should prefer
void *foo(void * size)
{
std::unique_ptr<char[]> buf(new char[*((int*)size)]);
while (running)
{
// Do something with buf
}
return NULL;
}
to avoid the raw pointer.
Just make sure that bufsize does not change before foo starts.
How do I inform the parent thread that the child thread has terminated so the parent thread can call pthread_join?
Have you considered using std::thread?
I think the following code demonstrates that you need do nothing using std::thread. The sleep_for() are big enough for humans to watch.
void pause_thread(int n, std::string lbl)
{
std::this_thread::sleep_for (std::chrono::seconds(n));
std::cout << lbl << " pause of " << n << " seconds ended" << std::endl;
}
int t403(void)
{
std::cout << "Spawning 3 threads...\n" << std::flush;
std::thread t1 (pause_thread, 6, "t1");
std::thread t2 (pause_thread, 3, "t2");
std::thread t3 (pause_thread, 1, "t3");
std::cout << "Done spawning threads, "
"Note that they finish out-of-order. \n"
"Now 'main' thread waits for spawned threads to join:\n" << std::flush;
t1.join(); std::cout << "join t1 " << std::flush;
t2.join(); std::cout << "join t2 " << std::flush;
t3.join(); std::cout << "join t3 " << std::flush;
std::cout << "completed join \n"
"note: \n - join sequence is in-order, but finish sequence is out-of-order\n"
" - inference: the threads waited for main to join! "<< std::endl;
return(0);
}
Update: 6/1/2016
I have reproduced the above scheme using posix thread and join. It would appear that the main thread needs no notification that a spawned thread has terminated.
Also, from "man pthread_join":
The pthread_join() function waits for the thread specified by 'thread'
(parameter 1) to terminate. If that thread has already terminated,
then pthread_join() returns immediately. The thread specified by
thread must be joinable.
Perhaps I am missing something about your question.
Perhaps the thread that is issuing the join should not be trying to do anything else during this time.
Consider moving the "anything else" into its own cooperative thread?

Concurrent server using pthread API

I am writing a simple client-server application using pthread-s API, which in pseudo code
looks something like this:
static volatile sig_atomic_t g_running = 1;
static volatile sig_atomic_t g_threads = 0;
static pthread_mutex_t g_threads_mutex;
static void signalHandler(int signal)
{
g_running = 0;
}
static void *threadServe(void *params)
{
/* Increment the number of currently running threads. */
pthread_mutex_lock(&g_threads_mutex);
g_threads++;
pthread_mutex_unlock(&g_threads_mutex);
/* handle client's request */
/* decrement the number of running threads */
pthread_mutex_lock(&g_threads_mutex);
g_threads--;
pthread_mutex_unlock(&g_threads_mutex);
}
int main(int argc, char *argv[])
{
/* do all the initialisation
(set up signal handlers, listening socket, ... ) */
/* run the server loop */
while (g_running)
{
int comm_sock = accept(listen_socket, NULL, 0);
pthread_create(&thread_id, NULL, &threadServe, comm_sock) ;
pthread_detach(thread_id);
}
/* wait for all threads that are yet busy processing client requests */
while (1)
{
std::cerr << "Waiting for all threads to finish" << std::endl;;
pthread_mutex_lock(&g_threads_mutex);
if (g_threads <= 0)
{
pthread_mutex_unlock(&g_threads_mutex);
break;
}
pthread_mutex_unlock(&g_threads_mutex);
}
/* clean up */
}
So the server is running in an infinite loop until a signal (SIGINT or SIGTERM) is received. The purpose of the second while loop is to let all the threads (that were processing client requests at the time a signal was received) to have a chance to finish the work they already started.
However I don't like this design very much, because that second while loop is basically a busy loop wasting cpu resources.
I tried to search on Google for some good examples on threaded concurrent server, but I had no luck. An idea that came to my mind was to use pthread_cond_wait() istead of that loop, but I am not sure if this does not bring further problems.
So the question is, how to improve my design, or point me to a nice simple example that deals with similar problem as mine.
EDIT:
I was considering pthread_join(), but I din't know how to join with worker thread,
while the main server loop (with accept() call in it) would be still running.
If I called pthread_join() somewhere after pthread_create()
(instead of pthread_detach()), then the while loop would be blocked until the worker
thread is done and the whole threading would not make sense.
I could use pthread_join() if I spawned all the threads at program start,
but then I would have them around for the entire life of my server,
which I thought might be a little inefficient.
Also after reading man page I understood, that pthread_detach() is exactly
suitable for this purpose.
The busy loop slurping CPU can easily be altered by having a usleep(10000); or something like that outside your mutex lock.
It would be more light-weight if you use a std::atomic<int> g_threads; - that way, you could get rid of the mutex altogether.
If you have an array of (active) thread_id's, you could just use a loop of
for(i = 0; i < num_active_threads; i++)
pthread_join(arr[i]);

Thread is blocking whole program

I'm trying to make a multiclient server. I have this thread:
void client_thread(int new_socket)
{
int size;
char inbuffer[BUF];
do
{
cout << "Waiting for messages: " << endl;
size = recv(new_socket, inbuffer, BUF, 0);
} while (true);
}
and this main procedure:
int main()
{
while (true)
{
//waiting for clients
cout << "Waiting for connections..." << endl;
new_socket = accept ( create_socket, (struct sockaddr *) &cliaddress, &addrlen );
//new client connected
if (new_socket > 0)
{
//start thread
thread(client_thread, new_socket).join();
}
}
return 0;
}
When the first client connects, the thread starts and the server is waiting for messages from him. But the server doesn't wait for new clients anymore. I don't know why. Is it because of the infinite do-while loop inside the thread-function? What's the point of threads if they block your whole program if they contain infinite loops?
The main routine blocks, because it waits for the thread to finish: join().
If you don't want to block, then don't join() your client_thread.
This exception might come from the destruction of your anonymous thread object. When you leave the scope of if() all objects in this scope are destroyed. From http://en.cppreference.com/w/cpp/thread/thread/~thread you can see, the destructor calls terminate(). To avoid it, you can call detach(). So instead of thread(client_thread, new_socket).join();, you must say thread(client_thread, new_socket).detach();.
You are supposed to create a thread and keep a reference to it until you joined it. In your code, the thread object is deallocated right after being created, hence your error if don't call join immediately.
To achieve this properly, the best way is to allocate the object on the heap, using the operator new and store the pointer in a list somewhere. When the thread is done it may remove itself from the list (don't forget to "mutex" it), or you could have another dedicated thread do that: perhaps you could simply have your main thread join all the other threads before exiting.