我正在尝试使用Detached Threads
in编译和运行以下代码C Linux
。问题是我希望每个线程都向我显示printf
处理程序的相应内容,*idThreadMethod
但它什么也不显示!printf
我在调用函数之前尝试使用 apthread_create
并显示它,但问题应该在*idThreadMethod
(处理函数)内部。编码:
//gcc detachedThreads.c -lpthread -o p
//./p 4
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int i;
void *idThreadMethod(void *args)
{
int pid;
pid = *((int *)args);
printf("\nI'm The Detached Thread %d\n", i);
printf("\nMy PID is: %d\n", pid);
pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
int quantityThreads, returnThread, pid;
pthread_t idThread[15];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
if(argc-1 < 1)
{
printf("\nSome arguments are missing\n");
return EXIT_FAILURE;
}
quantityThreads = atoi(argv[1]);
pid=getpid();
int *it = &pid;
for(i=0;i<quantityThreads;i++)
{
returnThread = pthread_create(&idThread[i],&attr,idThreadMethod,it);
if(returnThread == -1)
{
printf("\nThere is an error trying to create the thread\n");
return EXIT_FAILURE;
}
}
return EXIT_SUCCESS;
}
我该怎么做才能显示printf
来自该*idThreadMethod
函数的消息?