Monday, April 18, 2016

Linux POSIX thread an example source.

This example uses POSIX Pthread control calls.
   1) Three new threads create from main thread by pthread_create().
       Main thread give argument, when create a new thread.
   2) Each thread runs own working.
       This example has two thread function.
        1. Timer wait
        2. Character change upper-lower.
   3) Main thread wait other threads complete by pthread_join().
       Created thread make return value and send main thread by pthread_exit().
       Main thread wait and receive child thread return by pthread_join().

This example's return value code memory is using malloc() memory, so must free one in main thread.

When compiling source designate link library “libpthread” specify argument “-lpthread”.

$ gcc pthread.c -o pthread -lpthread

pthread.c

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

void* thread1(void* arg); // Thread Entry function 1
void* thread2(void* arg); // Thread Entry function 2


//
// Main thread.
//
int main(int argc, char **argv) {

    int   ret;

    const char *arg1 = "Tokyo";
    const char *arg2 = "Supercalifragilisticexpialidocious";
    const char *arg3 = "Hokkaido";

    pthread_t thrd1, thrd2, thrd3;
    void* th_ret;

    // Make three threads
    ret = pthread_create(&thrd1, NULL, thread1, (void*)arg1);
    if (ret) {
        perror("pthread_create[Thread 1]");
        exit(1);
    }

    ret = pthread_create(&thrd2, NULL, thread2, (void*)arg2);
    if (ret) {
        perror("pthread_create[Thread 2]");
        exit(1);
    }

    ret = pthread_create(&thrd3, NULL, thread1, (void*)arg3);
    if (ret) {
        perror("pthread_create[Thread 3]");
        exit(1);
    }


    // Waiting threads quit.
    pthread_join(thrd3, &th_ret);
    printf("Thread 3 [%d]\n", *(int*)th_ret);
    free(th_ret);                          // Free return value memory.

    pthread_join(thrd2, &th_ret);
    printf("Thread 2 [%d]\n", *(int*)th_ret);
    free(th_ret);                          // Free return value memory.

    pthread_join(thrd1, &th_ret);
    printf("Thread 1 [%d]\n", *(int*)th_ret);
    free(th_ret);                          // Free return value memory.


    return 0;
}

// Thread entry function 1
//   The function waits from a giving string size.
void* thread1(void* arg) {

    int   t = 0;
    int*  ret = malloc(sizeof(int));       // Allocate a return value area.

    t = strlen((char*)arg);
    sleep( t );
    printf("[%s] %ds\n", (char*)arg, t);

    *ret = t;
    pthread_exit(ret);                     // Return value for main thread.
}

// Thread entry function 2
//   The function converts characters from a giving argument string.
void* thread2(void* arg) {

    int   i;
    char* c = (char*)arg;
    int*  ret = malloc(sizeof(int));       // Allocate a return value area.

    printf("%s --> ", c);
    for(i=0; i<strlen(c); i++) {
        if (isupper(*(c+i))) {
            printf("%c", (char)tolower(*(c+i)));
        }
        else {
            printf("%c", (char)toupper(*(c+i)));
        }
    }
    printf("\n");

    *ret = i;
    pthread_exit(ret);                     // Return value for main thread.
}

No comments:

Post a Comment