user1492588
user1492588

Reputation:

Couldn't pass a value to a thread in C

I am new in thread subject and I want to create some process and threads. However I couldn't pass my value to the thread.

Please let me know what is wrong in my code.

When I compile I got the following error:

warning: passing argument 2 of ‘pthread_create’ makes pointer from integer without a cast [-Wint-conversion]

Here is my code:

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <pthread.h>
///////////////////////////

void *print_fun(int *id)    {

    printf("Thread #%d is using the print function now! \n",a);
}

int main()
{
    int pid1,pid2,pid3;
    pthread_t t1,t2;

    pid1=fork();
    if (pid1==0)
    {//child #1
        pthread_create(&t1,1,print_fun,NULL);
        printf("The child of process #1 has been created!!\n");
    }
    if (pid1>0)
    {//Father #1
        printf("The process #1 has been created!!\n");
        pid2=fork();
        if (pid2>0)//Father #2
        {
            printf("The process #2 has been created!!\n");
            pid3=fork();
            if (pid3>0)//Father #3
            {
                printf("The process #3 has been created!!\n");
            }
            else
            {
                printf("There is error in the third fork!\n");
            }
        }
        else if(pid2==0)//child #2
        {
            pthread_create(&t1,2,print_fun,NULL);
            printf("The child of process #2 has been created!!\n");
        }
        else
        {
            printf("There is error in the second fork!\n");
        }
    }
    else
    {//error
        printf("There is error in the first fork!\n");
    }

    pthread_exit(NULL);
}

Upvotes: 0

Views: 61

Answers (1)

Jabberwocky
Jabberwocky

Reputation: 50882

You probably want this:

pthread_create(&t1, NULL, print_fun, (void*)2);

instead of:

pthread_create(&t1, 2, print_fun, NULL);

The second argument of pthread_create is a pointer to a pthread_attr_t structure or NULL.

Upvotes: 2

Related Questions