Ubuntu – Multithreading in codeblocks

code-blocks

I tried to implement multithreading using codeblocks in ubuntu.
I typed the following program.

void *myThreadFun(void *vargp)
{

    sleep(1);
    printf("Printing hi from Thread \n");
    return NULL;
}

int main()
{

    pthread_t tid;
    printf("Before Thread\n");
    pthread_create(&tid, NULL, myThreadFun, NULL);
    pthread_join(tid, NULL);
    printf("After Thread\n");
    exit(0);
}

But I couldn't compile and run the program in codeblocks.
I got the following error

multithread.c  undefined reference to 'pthread_create'
multithread.c  undefined reference to 'pthread_join'

can someone help me out

I had used the following header files in the code:

pthread.h
stdio.h
stdlib.h

Best Answer

  • In Code::Blocks if you are using the GNU gcc compiler, you will probably need to add the -pthread compiler flag to both the compiler and linker options (under `Project --> Build options...)

    Code::Blocks compiler options

    Code::Blocks linker options

  • Related Question