Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 将参数传递给多个线程_C_Memory Management_Pthreads - Fatal编程技术网

C 将参数传递给多个线程

C 将参数传递给多个线程,c,memory-management,pthreads,C,Memory Management,Pthreads,对于C语言来说,我刚刚读过关于如何正确地将参数传递给线程的文章。如果这个参数需要传递给多个线程呢?我应该在哪里/如何使用free()?说: void *foo(void *i) { int a = *((int *) i); while(1){ printf("foo running \n"); sleep(1); } } void *bar(void *i) { int a = *((int *) i); wh

对于C语言来说,我刚刚读过关于如何正确地将参数传递给线程的文章。如果这个参数需要传递给多个线程呢?我应该在哪里/如何使用
free()
?说:

void *foo(void *i) {
    int a = *((int *) i);  
    while(1){
        printf("foo running \n");
        sleep(1);
    }
}

void *bar(void *i) {
    int a = *((int *) i);   
    while(1){
        printf("bar running \n");
        sleep(1);
    }
}

int main() {
    pthread_t threads[2];
    int i;
    for (i = 0; i < 2; i++ ) {
        int *arg = malloc(sizeof(*arg));
        if ( arg == NULL ) {
            fprintf(stderr, "Couldn't allocate memory for thread arg.\n");
            exit(1);
        }
        *arg = i;
        pthread_create(&threads[0], NULL, foo, arg);
        pthread_create(&threads[1], NULL, bar, arg);
    }
    for (i = 0; i < 2; i++){
        pthread_join(threads[i],NULL);
    }
    return 0;
}
void*foo(void*i){
int a=*((int*)i);
而(1){
printf(“foo-running\n”);
睡眠(1);
}
}
void*bar(void*i){
int a=*((int*)i);
而(1){
printf(“运行的酒吧”);
睡眠(1);
}
}
int main(){
pthread_t线程[2];
int i;
对于(i=0;i<2;i++){
int*arg=malloc(sizeof(*arg));
如果(arg==NULL){
fprintf(stderr,“无法为线程arg分配内存。\n”);
出口(1);
}
*arg=i;
pthread_create(&threads[0],NULL,foo,arg);
pthread_create(线程[1],NULL,bar,arg)(&threads[1]);
}
对于(i=0;i<2;i++){
pthread_join(线程[i],NULL);
}
返回0;
}

正在调用
免费(arg)
main
中生成线程后,相同的事情/安全?

在调用
free(arg)
之前,需要确保两个线程都已完成

这意味着您可以在两个线程上调用
pthread\u join
后执行此操作

for (i = 0; i < 2; i++){
    pthread_join(threads[i],NULL);
}
free(args);
(i=0;i<2;i++)的
{
pthread_join(线程[i],NULL);
}
免费(args);

如果所有线程都需要完全相同的参数,并且它们没有修改参数,那么根本不需要动态分配参数,只需在
main
函数的函数范围中将其声明为变量即可。如果没有动态分配,就没有必要释放它

另一方面,如果在循环中需要单独的参数,则需要跟踪所有参数,例如使用数组:

// Rest of program...

#define NUMBER_OF_ITERATIONS 2

int main(void)
{
    int args[NUMBER_OF_ITERATIONS];
    pthread_t threads[NUMBER_OF_ITERATIONS][2];

    // Create threads
    for (unsigned i = 0; i < NUMBER_OF_ITERATIONS; ++i)
    {
        args[i] = i;
        pthread_create(&threads[i][0], NULL, foo, &args[i]);
        pthread_create(&threads[i][1], NULL, bar, &args[i]);
    }

    // Wait for threads to finish
    for (unsigned i = 0; i < NUMBER_OF_ITERATIONS; ++i)
    {
        pthread_join(threads[i][0]);
        pthread_join(threads[i][1]);
    }

    return 0;
}
//程序的其余部分。。。
#定义迭代次数2
内部主(空)
{
int args[迭代次数];
pthread_t threads[迭代次数][2];
//创建线程
for(无符号i=0;i<迭代次数;++i)
{
args[i]=i;
pthread_创建(&threads[i][0],NULL,foo,&args[i]);
pthread_创建(&threads[i][1]、NULL、bar和args[i]);
}
//等待线程完成
for(无符号i=0;i<迭代次数;++i)
{
pthread_join(线程[i][0]);
pthread_join(threads[i][1]);
}
返回0;
}

上面的程序还解决了另一个问题,当您创建总共四个线程但只连接两个线程时。

顺便说一句。您想在
for()
循环中调用2
pthread\u create()
吗?我将接受Jaochim的回答,因为它还指出在这种情况下,没有必要动态分配它。但是+1使我们超级有用!谢谢