我的pthread没有调度。在C中
我有一个程序,它接受参数,然后为多个我的pthread没有调度。在C中,c,multithreading,pthreads,C,Multithreading,Pthreads,我有一个程序,它接受参数,然后为多个pthread\u t(pthread数组)动态分配空间,然后pthread\u create()加载函数。 问题是我的第一个线程没有停止运行。在我的第一个线程完成整个过程之前,我的第二个线程甚至不会创建。我如何解决这个问题? 下面是一个简单的代码来演示我的问题 #include <stdio.h> #include <pthread.h> pthread_t *ptr; int num; void* func(int id) {
pthread\u t
(pthread数组)动态分配空间,然后pthread\u create()
加载函数。
问题是我的第一个线程没有停止运行。在我的第一个线程完成整个过程之前,我的第二个线程甚至不会创建。我如何解决这个问题?
下面是一个简单的代码来演示我的问题
#include <stdio.h>
#include <pthread.h>
pthread_t *ptr;
int num;
void* func(int id)
{
while(1)
printf("%d\n", id);
}
int main(int argc, char *argv[])
{
int i;
num = atoi(argv[1]); //assuming arguments are always valid
ptr = malloc(sizeof(pthread_t)*num);
for(i = 0; i < num; i++)
pthread_create(&ptr[i], NULL, func(i), NULL);
//free allocated space
return 0;
}
但是,我仍然只看到0
。我现在就创建了线程?请查看pthread\u create
的链接
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
pthread_create()函数在调用过程中启动一个新线程。新线程通过调用start_例程()开始执行。arg作为start_例程()的唯一参数传递
为线程函数提供参数的正确方法是使用void*args
[在您的例子中为NULL
]
你可以这样做
void* func(void * id)
{
int c;
if (id)
c = (int)*id;
while(1)
printf("%d\n", c);
}
及
编辑:
如戴维所述,为了确保<代码>主体()/CODE继续运行,只要线程函数没有终止,您可以考虑添加以下
pthread_join(ptr[i], NULL)
线程执行顺序完全取决于调度程序运行单个线程的方式,因此操作顺序不受保证[包括
i
的增量作为main()
]的一部分]。试试像这样的东西
工作代码:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void* func(void* id)
{
int *c;
c = (int*)id;
// while(1) //removing infinite loop for ease of o/p
if(c)
printf("%d\n", *c);
}
int main(int argc, char *argv[])
{
int i = 0;
int *index = NULL;
int num;
pthread_t *ptr;
if (argv[1])
num = atoi(argv[1]);
index = calloc (num, sizeof (int));
for(i = 0; i < num; i++)
{
index[i] = i;
}
ptr = malloc(sizeof(pthread_t)*num);
for(i = 0; i < num; i++)
{
pthread_create(&ptr[i], NULL, func, (void*)&index[i]);
}
for(i = 0; i < num; i++)
pthread_join(ptr[i], NULL);
return 0;
}
#包括
#包括
#包括
void*func(void*id)
{
int*c;
c=(int*)id;
//while(1)//删除无限循环以便于o/p
如果(c)
printf(“%d\n”,*c);
}
int main(int argc,char*argv[])
{
int i=0;
int*index=NULL;
int-num;
pthread_t*ptr;
if(argv[1])
num=atoi(argv[1]);
索引=calloc(num,sizeof(int));
对于(i=0;i
请查看pthread\u create
的
int pthread_create(pthread_t *thread, const pthread_attr_t *attr,void *(*start_routine) (void *), void *arg);
pthread_create()函数在调用过程中启动一个新线程。新线程通过调用start_例程()开始执行。arg作为start_例程()的唯一参数传递
为线程函数提供参数的正确方法是使用void*args
[在您的例子中为NULL
]
你可以这样做
void* func(void * id)
{
int c;
if (id)
c = (int)*id;
while(1)
printf("%d\n", c);
}
及
编辑:
如戴维所述,为了确保<代码>主体()/CODE继续运行,只要线程函数没有终止,您可以考虑添加以下
pthread_join(ptr[i], NULL)
线程执行顺序完全取决于调度程序运行单个线程的方式,因此操作顺序不受保证[包括
i
的增量作为main()
]的一部分]。试试像这样的东西
工作代码:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void* func(void* id)
{
int *c;
c = (int*)id;
// while(1) //removing infinite loop for ease of o/p
if(c)
printf("%d\n", *c);
}
int main(int argc, char *argv[])
{
int i = 0;
int *index = NULL;
int num;
pthread_t *ptr;
if (argv[1])
num = atoi(argv[1]);
index = calloc (num, sizeof (int));
for(i = 0; i < num; i++)
{
index[i] = i;
}
ptr = malloc(sizeof(pthread_t)*num);
for(i = 0; i < num; i++)
{
pthread_create(&ptr[i], NULL, func, (void*)&index[i]);
}
for(i = 0; i < num; i++)
pthread_join(ptr[i], NULL);
return 0;
}
#包括
#包括
#包括
void*func(void*id)
{
int*c;
c=(int*)id;
//while(1)//删除无限循环以便于o/p
如果(c)
printf(“%d\n”,*c);
}
int main(int argc,char*argv[])
{
int i=0;
int*index=NULL;
int-num;
pthread_t*ptr;
if(argv[1])
num=atoi(argv[1]);
索引=calloc(num,sizeof(int));
对于(i=0;i
仅指出代码中实际发生的情况—要按预期工作,需要一个指向应在新线程中运行的函数的指针
您正在做的是:
pthread_create(&ptr[i], NULL, func(i), NULL);
直接调用函数,而不是将func
作为指针传递。由于在调用pthread\u create()
之前需要计算pthread\u create()
的参数,因此运行func()
,但在主线程中,甚至在开始新线程之前。由于func()
由一个无限循环组成,因此除了func(0)
的输出之外,您永远也看不到任何东西
要正确调用pthread\u create()
,请使用指向它的函数指针,只需省略()
部分即可轻松获得该指针(正如其他一些答案所指出的,您可以将i
参数传递到func()
的第四个参数中的pthread\u create()
)。即:
pthread_create(&ptr[i], NULL, func, (void*)i);
另外,还有其他需要注意的事项(正如其他答案所指出的),您应该更改
func()
以获取void*
参数,并确保主线程在生成线程后继续运行-或者通过输入无限循环排序,或者通过调用等待生成的线程终止,只是为了指出代码中实际发生的情况—要按预期工作,需要一个指向应在新线程中运行的函数的指针
for(i = 0; i < num; i++)
pthread_create(&ptr[i], NULL, func, (void*)&i)
您正在做的是:
pthread_create(&ptr[i], NULL, func(i), NULL);
直接调用函数,而不是将func
作为指针传递。由于在调用pthread\u create()
之前需要计算pthread\u create()
的参数,因此运行func()
,但在主线程中,甚至在开始新线程之前。由于func()
由一个无限循环组成,因此除了func(0)
的输出之外,您永远也看不到任何东西
要正确调用pthread\u create()
,请使用函数poin