我是线程主题的新手,我想创建一些进程和线程。但是我无法将我的价值传递给线程。
请告诉我代码中的错误。
编译时出现以下错误:
警告:传递'pthread_create'的参数2使得整数指针没有强制转换[-Wint-conversion]
这是我的代码:
#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);
}
答案 0 :(得分:2)
你可能想要这个:
pthread_create(&t1, NULL, print_fun, (void*)2);
而不是:
pthread_create(&t1, 2, print_fun, NULL);
pthread_create
的第二个参数是指向pthread_attr_t
结构或NULL
的指针。