I am learning multi-threading in Linux platform. I wrote this small program to get comfort with the concepts. On running the executable, I could not see any error nor does it print Hi. Hence I made to sleep the thread after I saw the output. But still could not see the prints on the console.
I also want to know which thread prints at run time. Can anyone help me?
#include <iostream>
#include <unistd.h>
#include <pthread.h>
using std::cout;
using std::endl;
void* print (void* data)
{
cout << "Hi" << endl;
sleep(10000000);
}
int main (int argc, char* argv[])
{
int t1 = 1, t2 =2, t3 = 3;
pthread_t thread1, thread2, thread3;
int thread_id_1, thread_id_2, thread_id_3;
thread_id_1 = pthread_create(&thread1, NULL, print, 0);
thread_id_2 = pthread_create(&thread2, NULL, print, 0);
thread_id_3 = pthread_create(&thread3, NULL, print, 0);
return 0;
}
Your main thread probably exits and thus the entire process dies. So, the threads don't get a chance to run. It's also possible (quite unlikely but still possible) that you'd see the output from the threads even with your code as-is if the threads complete execution before main thread exits. But you can't rely on that.
Call pthread_join(), which suspends the calling thread until the thread (specified by the thread ID) returns, on the threads after the pthread_create() calls in main():
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_join(thread3, NULL);
You can also use an array of pthread_t which would allow you to use a for loop over the pthread_create() and pthread_join() calls.
Or exit only the main thread using pthread_exit(0), which would exit only the calling thread and the remaining threads (the ones you created) will continue execution.
Note that your thread function should return a pointer or NULL:
void* print (void* data)
{
cout << "Hi" << endl;
return NULL;
}
Not sure about the high sleeps either right the threads exit, which is unnecessary and would hold the threads from exiting. Probably not something you wanted.
Related
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.
I'm fairly new to threads in C. For this program I need to declare a thread which I pass in a for loop thats meant to print out the printfs from the thread.
I can't seem to get it to print in correct order. Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 16
void *thread(void *thread_id) {
int id = *((int *) thread_id);
printf("Hello from thread %d\n", id);
return NULL;
}
int main() {
pthread_t threads[NUM_THREADS];
for (int i = 0; i < NUM_THREADS; i++) {
int code = pthread_create(&threads[i], NULL, thread, &i);
if (code != 0) {
fprintf(stderr, "pthread_create failed!\n");
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
//gcc -o main main.c -lpthread
That's the classic example of understanding multi-threading.
The threads are running concurrently, scheduled by OS scheduler.
There is no such thing as "correct order" when we are talking about running in parallel.
Also, there is such thing as buffers flushing for stdout output. Means, when you "printf" something, it is not promised it will happen immediately, but after reaching some buffer limit/timeout.
Also, if you want to do the work in the "correct order", means wait until the first thread finishes it's work before staring next one, consider using "join":
http://man7.org/linux/man-pages/man3/pthread_join.3.html
UPD:
passing pointer to thread_id is also incorrect in this case, as a thread may print id that doesn't belong to him (thanks Kevin)
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?
edit:
I made the wrong assumption that threads started running on pthread_join when they actually start running on pthread_create.
I'm learning to use Posix threads, and I've read that:
pthread_join() - wait for thread termination
So, in the code sample, main's exit(0) is not reached until both started threads end.
But after the first call to pthread_join(), main continues executing, because the second call to pthread_join() actually runs, and the message in between is printed.
So how's this? does main continue executing while both threads aren't finished yet? or doesn't it?
I know this isn't a reliable way of testing, but the second test message always gets printed after both threads are finished, no matter how long the loop is. (at least on my machine when I tried it)
void *print_message_function( void *ptr )
{
char *message = (char *) ptr;
for( int a = 0; a < 1000; ++a )
printf( "%s - %i\n", message, a );
return NULL;
}
//
int main( int argc, char *argv[] )
{
pthread_t thread1, thread2;
char message1[] = "Thread 1";
char message2[] = "Thread 2";
int iret1, iret2;
//
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
//
pthread_join( thread1, NULL);
printf( "Let's see when is this printed...\n" );
pthread_join( thread2, NULL);
printf( "And this one?...\n" );
//
printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}
The function pthread_join waits for the thread to finish or returns immediately if the thread is already done.
So in your case
pthread_join( thread1, NULL); /* Start waiting for thread1. */
printf( "Let's see when is this printed...\n" ); /* Done waiting for thread1. */
pthread_join( thread2, NULL); /* Start waiting for thread2. */
printf( "And this one?...\n" ); /* Done waiting for thread2. */
But after the first call to pthread_join(), main continues executing,
because the second call to pthread_join() actually runs, and the
message in between is printed.
False. pthread_join waits unless thread1 is already done.
pthread_join() does not return (blocking the calling thread) until the thread being joined has terminated. If the thread has already terminated, then it returns straight away.
In your test, both threads do exit, and so of course you'll see all the messages printed from the main thread. When the first message is printed, you know that thread1 is complete; when the second is printed you know that thread2 is also complete. This will probably happen quite quickly after the first, since both threads were doing the same amount of work at roughly the same time.
pthread_join( thread1, NULL);
The main thread waits here on this join call till thread1 completes its job. Once thread1 completes execution main thread will proceed ahead and execute the next statement printf.
printf( "Let's see when is this printed...\n" );
Again, Main thread will wait here till thread2 completes its job.
pthread_join( thread2, NULL);
Once thread2 completes its job the main thread moves ahead and the next statement which is the printf is executed.
printf( "And this one?...\n" );
The sequence will work in the above mentioned way.Probably, this happens all too soon that the traces you see makes it confusing.
Also, Do not using printf to see behavior of multithreaded programs can be quite misleading, the order of the printf may not always indicate the correct control flow Since it is timing based and flushing of the buffers to stdout may not happen in sasme order as the prints were executed accross threads.
If the first pthread_join returns immediately, that would suggest that the first thread has already finished executing. What does the output look like? Do you see any "Thread 1 - n" output after "Let's see when this is printed"?
In the following code I create some number of threads, and each threads sleeps for some seconds.
However my main program doesn't wait for the threads to finish, I was under the assumption that threads would continue to run until they finished by themselves.
Is there someway of making threads continue to run even though the calling thread finishes.
#include <pthread.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
int sample(int min,int max){
int r=rand();
return (r %max+min );
}
void *worker(void *p){
long i = (long) p;
int s = sample(1,10);
fprintf(stdout,"\tid:%ld will sleep: %d \n",i,s);
sleep(s);
fprintf(stdout,"\tid:%ld done sleeping \n",i,s);
}
pthread_t thread1;
int main(){
int nThreads = sample(1,10);
for(int i=0;i<nThreads;i++){
fprintf(stderr,"\t-> Creating: %d of %d\n",i,nThreads);
int iret1 = pthread_create( &thread1, NULL, worker, (void*) i);
pthread_detach(thread1);
}
// sleep(10);//work if this is not commented out.
return 0;
}
Thanks
Edit:
Sorry for not clarifying, is it possible without explicitly keeping track of my current running threads and by using join.
Each program has a main thread. It is the thread in which your main() function executes. When the execution of that thread finishes, the program finishes along with all its threads. If you want your main thread to wait for other threads, use must use pthread_join function
You need to keep track of the threads. You are not doing that because you are using the same thread1 variable to every thread you are creating.
You track threads by creating a list (or array) of pthread_t types that you pass to the pthread_create() function. Then you pthread_join() those threads in the list.
edit:
Well, it's really lazy of you to not keep track of running threads. But, you can accomplish what you want by having a global var (protected by a mutex) that gets incremented just before a thread finishes. Then in you main thread you can check if that var gets to the value you want. Say nThreads in your sample code.
You need to join each thread you create:
int main()
{
int nThreads = sample(1,10);
std::vector<pthread_t> threads(nThreads);
for(i=0; i<nThreads; i++)
{
pthread_create( &threads[i], NULL, worker, (void*) i)
}
/* Wait on the other threads */
for(i=0; i<nThreads; i++)
{
status* status;
pthread_join(threads[i], &status);
}
}
You learned your assumption was wrong. Main is special. Exiting main will kill your threads. So there are two options:
Use pthread_exit to exit main. This function will allow you to exit main but keep other threads running.
Do something to keep main alive. This can be anything from a loop (stupid and inefficient) to any blocking call. pthread_join is common since it will block but also give you the return status of the threads, if you are interested, and clean up the dead thread resources. But for the purposes of keeping main from terminating any blocking call will do e.g. select, read a pipe, block on a semaphore, etc.
Since Martin showed join(), here's pthread_exit():
int main(){
int nThreads = sample(1,10);
for(int i=0;i<nThreads;i++){
fprintf(stderr,"\t-> Creating: %d of %d\n",i,nThreads);
int iret1 = pthread_create( &thread1, NULL, worker, (void*) i);
pthread_detach(thread1);
}
pthread_exit(NULL);
}