So, if we comment out the line pthread_exit() in main() in the thread01.c of the previous example code, the threads created may not have a chance to execute their work before being terminated. Otherwise, they will be automatically terminated when main() finishes. If main() finishes before the threads it has created, and exits with pthread_exit(), the other threads will continue to execute. Typically, the pthread_exit() routine is called after a thread has completed its work and is no longer required to exist. pthread_exit is used to explicitly exit a thread.The entire process is terminated due to a call to either the exec or exit subroutines.The thread is canceled by another thread via the pthread_cancel routine.The thread makes a call to the pthread_exit subroutine.The thread returns from its starting routine (the main routine for the initial thread).There are several ways in which a Pthread may be terminated:.As the loop iterates, the value of this memory location changes, possibly before the created threads can access it. It passes the address of variable id, which is shared memory space and visible to all threads. If we do (void*) &id, it's a wrong way of passing data to the child thread. Note that, in the code, we pass the parameter (thread id) to the child thread. Int ret = pthread_create(&my _thread, NULL, &worker _thread, (void*) id) Printf("This is worker_thread #%ld\n", (long)arg) The return value from the pthread_create() call will be zero if it's successful, otherwise, it returns an error. The call also passes the address of a my_thread variable for the worker_thread() to store a handle to the thread. The call to create the thread has a NULL value for the attributes, which gives the thread default attributes. In the code, the main thread will create a second thread to execute worker_thread(), which will print out its message while main thread prints another. Printf("Error: pthread_create() failed\n") Ret = pthread_create(&my _thread, NULL, &worker _thread, NULL) Here is a sample of creating a child thread:.There is no implied hierarchy or dependency between threads. Once created, threads are peers, and may create other threads.The maximum number of threads that may be created by a process is implementation dependent.NULL may be used if no argument is to be passed. While using fork() causes execution to continue in the same location with a different return code, using a new thread explicitly provides a pointer to a function where the new thread should start executing.Ī single argument that may be passed to start_routine. So, we can pass any type of single argument and return a pointer to any type. We should pass the address of a function taking a pointer to void as a parameter and the function will return a pointer to void. The routine that the thread will execute once it is created. We can specify a thread attributes object, or NULL for the default values. This identifier enables us to refer to the thread.Īn attribute object that may be used to set thread attributes. When a thread is created, an identifier is written to the memory location to which this variable points. This is a pointer to pthread_t structure. pthread_create (pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *arg) arguments:Īn identifier for the new thread returned by the subroutine.This routine can be called any number of times from anywhere within our code. pthread_create creates a new thread and makes it executable.All other threads must be explicitly created by the programmer. Our main() program is a single, default thread.I just want to compile and run the damn thing, any environment/editor will do ! Thanks for your help. A quick search on Google gives very little insight. Now, VS can open all the other include files alright, but not pthread.h, and upon compilation it will complain that it cannot open include file 'pthread.h'. Next I tried in Visual Studio 2008 under Windows.
How can I configure it so that it compiles properly ? I just created an empty C++ project and copy-pasted the code in main.cpp. Furthermore, I tried the same with stdlib.h and others and it can't open any of them. So I guess Code::Blocks doesn't know where it is. It's one of the tutorial examples at, more precisley this : įirst I tried it with Code::Blocks in my Ubuntu VM, but Code::Blocks says pthread is an undefined reference and when I try to open the include file (by right clicking and clicking "open file" or something like that), it says the file can't be found. Hello, I am trying to compile a simple C program that uses pthreads.