Friday, December 28, 2012

Threads Program using c on linux/fedora

This program creates a single extra thread, shows that it is sharing variables with the original thread, and gets the new thread to return a result to the original thread. Multithreaded programs don't get much simpler than this.
Lets look the function which create the thread. pthread_create creates a new thread,much as fork creates a new process.
int pthread_create(pthread_t *thread, pthread_attr_t *attr, void(*start_routine)(void *), void *arg);
The first argument is a pointer to pthread_t . When a thread is created, an identifier is written to the memory location to which this variable points.This identifier enables you to refer to the thread . The next argument sets the thread attributes. You do not usually need any special attributes, and you can simply pass NULL as this argument. Later is the chapter you will see how to use these attributes. The final two arguments tell the thread the function that it is to start executing and the arguments that are to be passed to this function.

Void *(*start_routine)(void *)
The preceding line simply says that you must pass the address of a function taking a pointer to void as a parameter and the function will return a pointer to void.
When a thread terminates, ti calls the pthread_exit function, much as a process calls exit when it terminates. This function terminates the calling thread , returning a pointer to an object. Never use it to return a pointer to a local variable, because the variable will cease to exist when the thread does so, causing a serious bug. pthread_exit is declared as follows.
void pthread_exit(void *retval);
pthread_join is the thread equivalent of wait that processes use to collect child processes. This function is declared as follows:
int pthread_join(pthread_t th , void **thread_return);
The first parameter is the thread for which to wait, the identifier that pthread_create filled in for you. The second argument is a pointer to a pointer that itself points to the return value from the thread. Like pthread_create, this function returns xero for success and an error code on failure.


void *thread_function(void *arg);

char message[] = "Hello World";

int main() {
    int res;
    pthread_t a_thread;
    void *thread_result;
    res = pthread_create(&amp;a_thread, NULL, thread_function, (void *)message);
    if (res != 0) {
        perror("Thread creation failed");
    printf("Waiting for thread to finish...\n");
    res = pthread_join(a_thread, &amp;thread_result);
    if (res != 0) {
        perror("Thread join failed");
    printf("Thread joined, it returned %s\n", (char *)thread_result);
    printf("Message is now %s\n", message);

void *thread_function(void *arg) {
    printf("thread_function is running. Argument was %s\n", (char *)arg);
    strcpy(message, "Bye!");
    pthread_exit("Thank you for the CPU time");
Now steps for compiling the source code.
If you are using codeblocks IDE in fefora then you must have to add library file file in linker setting which you can do form setting/compiler and debugger menu. Library file is in usr/lib or user/lib64 directories.

If you are using the command line shell then use following command to compile.

$ cc D_REENTRANT programThread.c -o programThread -lpthread

No comments:

Post a Comment