Multithreading in C #1

Let's first finish how to create a process, so click here.

Here, I created a process, and two threads in it.
#include <stdio.h>
#include <stdlib.h>  // EXIT_SUCCESS, EXIT_FAILURE
#include <unistd.h>  //getpid, getppid, fork
#include <pthread.h>

int g = 0;

void *ProcessA_Thread2(void *vargp)
{
    // Store the value argument passed to this thread
    int *myid = (int *)vargp;

    // Let us create a static variable to observe its changes
    static int s = 0;

    // Change static and global variables
    ++s; ++g;

    // Print the argument, static and global variables
    printf("%s Thread ID: %d, Static: %d, Global: %d\n", __func__, *myid, s, g);
}

void *ProcessA_Thread1(void *vargp)
{
    // Store the value argument passed to this thread
    int *myid = (int *)vargp;

    // Let us create a static variable to observe its changes
    static int v = 0;

    // Change static and global variables
    ++v; ++g;

    // Print the argument, static and global variables
    printf("%s Thread ID: %d, Static: %d, Global: %d\n", __func__, *myid, v, g);
}

void create_thread()
{
    pthread_t tid_1, tid_2;
    pthread_create(&tid_1, NULL, ProcessA_Thread1, (void *)&tid_1);
    printf("%s %d Thread ID: %d, Global: %d\n", __func__, __LINE__, (int )tid_1, g);
    pthread_create(&tid_2, NULL, ProcessA_Thread2, (void *)&tid_2);
    printf("%s %d Thread ID: %d, Global: %d\n", __func__, __LINE__, (int )tid_2, g);
    pthread_exit(NULL);
}


void process_A ()
{
    printf("%s(), my pid is %d, and my parent's pid is %d\n",
            __func__, getpid(), getppid());
    create_thread();
    exit(EXIT_SUCCESS);
}

void parent(pid_t pid)
{
    printf("%s(), my pid is %d, and my parent's pid is %d\n",
            __func__, getpid(), getppid());
    exit(EXIT_SUCCESS);
}

int main()
{
    pid_t pid = 0;

    printf("In %s\n",__func__);
    switch(pid = fork()) {
        case -1:
            perror("fork failed");
            exit(EXIT_FAILURE);
            break;
        case 0:
            process_A();
            break;
        default:
            parent(pid);
            break;
    }

    return 0;
}

You might get the following error while compiling, so please add the option "-pthread".

terminal$ cc -o ser server1.c
/tmp/cc2poBg2.o: In function `create_thread':
server1.c:(.text+0x11f): undefined reference to `pthread_create'
server1.c:(.text+0x164): undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status
terminal$ cc -pthread -o ser server1.c
terminal$

Output:
1
2
3
4
5
Process_A(), my pid is 5551, and my parent's pid is 1
create_thread 42 Thread ID: 1626085120, Global: 0
create_thread 44 Thread ID: 1617692416, Global: 0
 ProcessA_Thread2 Thread ID: 1617692416, Static: 1, Global: 1
 ProcessA_Thread1 Thread ID: 1626085120, Static: 1, Global: 2 

Here, calling thread doesn't wait until the new thread completes its execution. So, we need to add pthread_join() to wait until each thread completes. Click here to see how to resolve this.

Note: It's a bad idea using global variables in the thread, but I just used to understand which thread gets executed first.

Comments