I wrote this program to practice pthread system calls so I used some printing lines to check the results but they are escaped the output is:
Thread 1 created
Thread 2 created
test3
while I think it should be
thread 1 created
test2
thread 2 created
test3
test1
The order may change but I should have this lines so why it escape this print statements?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
void *function();
void *function2();
int main(int argc, char *argv[])
{
pthread_t tid;
int rc;
rc = pthread_create(&tid, NULL, function(), NULL);
if(rc > 0){
fprintf(stderr, "Error\n");
exit(1);
}
pthread_join(tid, NULL);
sleep(1);
printf("test1\n");
pthread_exit(NULL);
}
void *function(){
int rc;
pthread_t tid;
printf("Thread 1 created\n");
rc = pthread_create(&tid, NULL, function2(), NULL);
if(rc > 0){
fprintf(stderr, "Error\n");
exit(1);
}
printf("test2\n");
pthread_exit(NULL);
}
void *function2(){
pthread_detach(pthread_self());
printf("Thread 2 created\n");
printf("test3\n");
pthread_exit(NULL);
}
rc = pthread_create(&tid, NULL, function(), NULL);
You're trying to call pthread_create() with the pointer returned by calling function() as the function to run in the new thread (Remember, function arguments get evaluated before the function itself is called). Now, function() doesn't actually return any value, but it calls function2() in its body (while evaluating the arguments for another call to pthread_create()), which also doesn't return any value, but does call pthread_exit() instead. Since there's only one thread at that point because only the main process thread is running (pthread_create() hasn't actually been called yet; the call stack looks like main() -> function() -> function2()), the whole program then exits.
You need to call pthread_create() with pointers to function and function2, not the results of calling them:
rc = pthread_create(&tid, NULL, function, NULL);
etc.
Related
I understand pthread_detach(pid) that: "storage for the thread thread can be reclaimed when that thread terminate" (as per http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_detach.html)
However, I understand that means that once the pid thread finishes executing, its memory will be freed, and we will not be able to run it again, or call join on it.
However, I tried the following code:
#include <stdio.h>
#include <pthread.h>
void* myFunction (void* arg)
{
printf("Hello World from thread!\n");
}
int main()
{
pthread_t tid;
pthread_create(&tid, NULL, myFunction, NULL);
//pthread_join(tid, NULL);
int isDetached = -10;
isDetached = pthread_detach(tid);
printf("Is my thread detached: %d\n", isDetached);
int i;
for (i = 0; i<15; i++)
printf("%d\n", i);
pthread_create(&tid, NULL, myFunction, NULL);
pthread_join(tid, NULL);
for (i = 0; i<15; i++)
printf("%d\n", i);
return 0;
}
And I get the following:
If I understand correctly, since I did pthread_detach(tid), I should not be able to be able to do
pthread_create(&tid, NULL, myFunction, NULL);
pthread_join(tid, NULL);
after it, yet I did and it works perfectly. So, what is really the purpose of doing othread_detach(pid) if we can still run the thread and join it?
Thanks a lot!
pthread_detach simply tells your program that the current instance of tid will not join, (pthread_join) again, and frees up any pthread handles & objects on the tid instance.
Since you called pthread_detach if you try to pthread_join that thread it will not since it has been released and disposed of.
I've added the pthread_join to your code right after the detach call and you can see nothing happens as expected.
#include <stdio.h>
#include <pthread.h>
void * myFunction (void *arg)
{
printf ("Hello World from thread!\n");
}
int main ()
{
pthread_t tid;
pthread_create (&tid, NULL, myFunction, NULL);
//pthread_join(tid, NULL);
int isDetached = -10;
isDetached = pthread_detach (tid);
printf ("Is my thread detached: %d\n", isDetached);
/* ADDED FOR STACK OVERFLOW */
pthread_join (tid, NULL);
int i;
for (i = 0; i < 15; i++)
printf ("%d\n", i);
pthread_create (&tid, NULL, myFunction, NULL);
pthread_join (tid, NULL);
for (i = 0; i < 15; i++)
printf ("%d\n", i);
return 0;
}
I'm not sure what the confusion is but if you were expecting a different behavior when you called pthread_create a second time; note, this is really creating a fresh instance of tid for you. Therefore your second join call runs the thread.
pthread_t tid;
pthread_create(&tid, NULL, myFunction, NULL);
This creates a new thread and subsequent call topthread_detach(pid) detaches the thread created. Now
pthread_create(&tid, NULL, myFunction, NULL);
This creates a new thread and subsequently you have called a join which basically waits for the completion of the newly created thread, not for the one which has been detached.
You can check the code attached below. When you run this code the thread which has been detached is printing even after join is called i.e myfunc1 is still printing after the pthread_join.
#include <stdio.h>
#include <pthread.h>
void *myFunction2 (void *arg)
{
printf("Hello World from func2!\n");
}
void *myFunction1 (void *arg)
{
while(1) {
printf("Hello World from func1\n");
}
}
int main()
{
pthread_t tid;
pthread_create(&tid, NULL, myFunction1, NULL);
//pthread_join(tid, NULL);
int isDetached = -10;
isDetached = pthread_detach(tid);
printf("Is my thread detached: %d\n", isDetached);
int i;
pthread_create(&tid, NULL, myFunction2, NULL);
pthread_join(tid, NULL);
for (i = 0; i<15; i++)
printf("%d\n", i);
return 0;
}
Your variable, tid, is not a thread. Your program does not start one thread two times, it creates two completely different threads.
The actual threads are objects that exist in the operating system. Your tid variable is just a "handle" that you can use to interact with a thread. Your second call to pthread_create(&tid,...) is re-using the handle variable, pointing it to a new, different thread.
Working from this example:
https://computing.llnl.gov/tutorials/pthreads/samples/hello.c
I've worked backwards and tried to edit in what I'm hoping to accomplish.
I'd like to pass data to the thread being spawned.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
long NUM_THREADS=0;
void *Entropy(void *depth)
{
long tid;
tid = (long)depth;
printf("This is where things get done.\n", tid);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
NUM_THREADS = sysconf(_SC_NPROCESSORS_ONLN);
printf("Cores: %i\n", NUM_THREADS);
pthread_t threads[NUM_THREADS];
int rc;
long t;
int depth;
depth = atoi(argv[1]);
for(t=0;t<NUM_THREADS;t++){
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, Entropy(depth), (void *)t);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
pthread_exit(NULL);
}
I see on line:
rc = pthread_create(&threads[t], NULL, Entropy(depth), (void *)t);
My function Entropy gets called here, so I thought I'd try to tack on some brackets and pass a variable to that function the way I'd seen it done before. This seems to be a little different though, since this whole line returns something to rc, I wonder if that changes how I pass data to my thread, but I'm not sure how else I'd do it.
Right now this code compiles and runs, int main() goes fine without a hitch but it seg faults the moment it tries to create new threads.
In order to pass data to a thread you need to prepare the data in some place in memory, and pass a pointer to that place into pthread_create. It is pthread_create's job to pass that pointer to the runner function of your thread:
typedef struct {
long tid;
int depth;
}thread_data;
...
void *Entropy(void *dataPtr) {
thread_data *data= (thread_data*)dataPtr;
printf("This is where things get done for %li.\n", data->tid);
pthread_exit(NULL);
}
...
pthread_t threads[NUM_THREADS];
thread_data data[NUM_THREADS];
...
for(t=0;t<NUM_THREADS;t++) {
data[t].tid = t;
data[t].depth = depth;
rc = pthread_create(&threads[t], NULL, Entropy, (void *)&data[t]);
}
Your code crashes since you pass incorrect parameters:
rc = pthread_create(&threads[t], NULL, Entropy(depth), (void *)t);
// ^^^^^^^^^^^^^^
Here you should pass function pointer void *(*)(void *) but you are passing void *, and moreover value is unspecified since Entropy() has no return statement (did you turn warnings on at all?). I guess it should be like this:
rc = pthread_create(&threads[t], NULL, Entropy, (void *)t);
Next, how to pass parameter to thread routine? Technically you can use any pointer, but you should think twice about what you are passing. First of all pointed data must be valid when new thread runs. I.e. you shouldn't pass addresses of any locals except if you are sure that thread is finished when you are leaving scope of passed data - use pthread_join(new_thread) at the scope end to achieve that. Another approach is to pass pointer to data at global scope, that is surely valid at any moment. But there is one flaw - such data are visible to all threads, so you may accidentally make a mess. To avoid it - use dynamic memory - allocate data block with malloc() pass pointer to thread and free it in that thread. Latter option reduces chances to corrupt someone's else data.
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
long NUM_THREADS=0;
void *Entropy(void *depth)
{
long tid = *((long *)depth);
free(depth);
printf("This is where things get done.\n", tid);
return NULL;
}
int main(int argc, char *argv[])
{
NUM_THREADS = sysconf(_SC_NPROCESSORS_ONLN);
printf("Cores: %i\n", NUM_THREADS);
pthread_t threads[NUM_THREADS];
int rc;
long t;
int depth;
depth = atoi(argv[1]);
for(t=0;t<NUM_THREADS;t++){
long *arg = malloc(sizeof(*arg));
*arg = t;
printf("In main: creating thread %ld\n", t);
rc = pthread_create(&threads[t], NULL, Entropy, arg);
if (rc){
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
}
for(t=0;t<NUM_THREADS;t++){
pthread_join(threads[t], NULL);
}
}
The code is sample, what I need to do is to modify.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *my_function (void*); // Function for the thread
int main ()
{
pthread_t my_thread ; // Declare a thread identifier
int rc1, x = 7;
// Create my_thread
if ( (rc1 = pthread_create (& my_thread, NULL, & my_function, (void*) &x)))
{
printf ("Error in creating thread %d\n", rc1);
}
pthread_join ( my_thread, NULL); // wait for thread to exit
return (0); // exit the main function
}
// The my_thread is created with my_function() which accepts an argument
void *my_function(void* arg)
{
int i = *(int*)arg;
printf ("The argument which this thread received is %d \n", i ) ;
pthread_exit (NULL) ; // thread exits
}
The question is: Pass a simple integer to a thread’s start function at thread creation time. But I don't know how to pass a simple integer to a thread's start function, and I also don't know what is thread creation time.
If your imlementation has intptr_t (most of them do), you can modify the code as following:
int rc1;
intptr_t x = 7
...
pthread_create (&my_thread, NULL, &my_function, (void*)x));
...
void* my_function(void* arg)
{
intptr_t i = (intptr_t)arg;
...
Hello for above code in thread it is displaying 0 (tid = 0) instead of 8... what may be the reason ? In PrintHello function I am printing threadid but I am sending value 8 but it is printing 0 as output
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void *PrintHello(void *threadid)
{
int *tid;
tid = threadid;
printf("Hello World! It's me, thread #%d!\n", *tid);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
pthread_t thread1,thread2;
int rc;
int value = 8;
int *t;
t = &value;
printf("In main: creating thread 1");
rc = pthread_create(&thread1, NULL, PrintHello, (void *)t);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
printf("In main: creating thread 2\n");
rc = pthread_create(&thread1, NULL, PrintHello, (void *)t);
if (rc)
{
printf("ERROR; return code from pthread_create() is %d\n", rc);
exit(-1);
}
/* Last thing that main() should do */
pthread_exit(NULL);
}
The actual object that holds 8 is value which is local to your main function so accessing after main has exited is not valid.
You don't wait for your child threads to finish before they attempt to access this local variable so the behaviour is undefined.
One fix would be to make your main wait for it's child threads before exiting using pthread_join.
(I've assumed that you've made a typo in your second call to pthread_create and meant to pass thread2 instead of thread1.)
E.g.
/* in main, before exiting */
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
I tried to compile this simple pthreads program with this command
$ gcc -pthread -o pthreads pthreads.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
void *myThread(void *arg);
int main()
{
pthread_t mythread;
int ret;
ret = pthread_create( &mythread, NULL, myThread, NULL );
if (ret != 0){
printf( "Can't create pthread: %s", strerror(errno));
exit(-1);
}
return 0;
}
void *myThread(void *arg){
// Thread code goes here..
printf("OK! NOW ON THE THREAD\n");
pthread_exit(NULL);
}
but when trying ./pthreads there is no output presented!!
You need to wait for the thread to finish. Otherwise you risk exiting before the thread starts executing.
...
pthread_create( &mythread, NULL, myThread, NULL );
...
// Wait for the thread to finish.
pthread_join( mythread, NULL);
You didn't wait for your thread to finish. You need to use pthread_join().
You problem comes from the fact that you're main thread is returning from main, and thus calling exit (or _exit). All running thread are killed when the program exit. In this case, the worker thread didn't have the time to execute before it is killed.
You can use pthread_join to wait for the completion of the thread before returning from main.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
void *myThread(void *arg);
int main()
{
void* thread_return;
pthread_t mythread;
int ret;
ret = pthread_create(&mythread, NULL, myThread, NULL);
if (ret != 0)
{
printf("Can't create pthread: %s\n", strerror(errno));
exit(-1);
}
ret = pthread_join(mythread, &thread_return);
if (ret != 0)
{
printf("Can't join pthread: %s\n", strerror(errno));
exit(-1);
}
return 0;
}
void *myThread(void *arg)
{
printf("OK! NOW ON THE THREAD\n");
pthread_exit(NULL);
}
Sanjit's answer is certainly correct, but for the sake of enlarging your threads toolbox, you might also look at pthread_barrier_wait. When you have a simple program with a lot of threads and main looks like "start all worker threads and wait for them to finish", having main and all the workers simply wait on a barrier can be a nice way to avoid having to store all the worker thread ids and join them in a for loop. Barriers also have a lot of other neat uses that sometimes let you avoid unnecessary complexity from doing the same things with mutexes and condition variables.