以下是我想实现线程数组的一个c程序。 有两个线程函数。我想在每个函数内发送一个int值。但是代码没有给出任何输出。 示例程序:实现线程数组
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
void * threadFunc1(void * arg)
{
int id = *((int *) arg);
printf("Inside threadfunc2 for thread %d",id)
}
void * threadFunc2(void * arg)
{
int i= *((int *)arg);
printf("Inside threadfunc2 for thread %d",i)
}
int main(void)
{
pthread_t thread[10];
for(int i=0;i<10;i++)
{
pthread_create(&thread[i],NULL,threadFunc1,(void*)&i); // want to send the value of i inside each thread
pthread_create(&thread[i],NULL,threadFunc,(void*)&i);
}
while(1);
return 0;
}
代码中有什么问题吗?
如果您使用C++,请使用'std :: thread',这是一回事。然后,你问题中的C标签也是错误的。在任何情况下,它都缺少有关代码在执行时的作用以及您的期望。我的水晶球告诉我,你应该尝试使用'%p'格式说明符输出传递给线程函数的指针,或者只是将其传递给'std :: cout'。 –
请参阅[@UlrichEckhardt](http://stackoverflow.com/questions/33183877/implementing-an-array-of-thread#comment54174239_33183877)的评论。这就是为什么每个人都会告诉你**不要用C++标记**来标记c问题。 –
你能分享获得的输出吗? –