如何产卵n个线程? [英] How to spawn n threads?

查看:114
本文介绍了如何产卵n个线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写一个多线程程序,基于命令行输入线程数,所以我不能硬code pre-声明线程。这是做这件事的一个有效的方法是什么?

  INT线程= 5; //(动态的,不是硬codeD)
INT I = 0;
*的pthread_t线程=的malloc(sizeof的(的pthread_t)*线程);对于(i = 0; I<螺纹;我++){
    的pthread_t foobar的;
    线程[I] = foobar的; //这将导致冲突?
}对于(i = 0; I<螺纹;我++){    INT RET =在pthread_create(安培;螺纹[I],NULL,(void *的)及foobar_function,NULL);    如果(保留!= 0){
        的printf(创建pthread的错误。\\ n!);
        出口(1);
    }
}


下面是修改我的结果以下建议。似乎工作就好了。

  INT线程= 5;
INT I;*的pthread_t线程=的malloc(sizeof的(的pthread_t)*线程);对于(i = 0; I<螺纹;我++){    INT RET = pthread_create的(安培;螺纹[Ⅰ],NULL,&放大器; foobar_function,NULL);    如果(保留!= 0){
        的printf(创建pthread的错误。\\ n!);
        出口(1);
    }
    //在pthread_join(线程[I],NULL); //实际上并不希望这样在这里:)
}睡眠(1); //主()你的线程之前做可能会结束,
免费(螺纹); //所以我们睡用于说明目的


解决方案

什么是在第一个周期?它是否设置数组元素为未初始化价值?

所以,我认为这就是你所需要的:

  INT线程= 5,I = 0,RET = -1;*的pthread_t线程=的malloc(sizeof的(的pthread_t)*线程);对于(i = 0; I<螺纹;我++){    RET = pthread_create的(安培;螺纹[Ⅰ],NULL,&放大器; foobar_function,NULL);    如果(保留!= 0){
        的printf(创建pthread的错误。\\ n!);
        出口(1);
    }
}

它生成的主题线程,开始的 foobar_function 在每个。你必须(如果一切顺利的话:))它们的ID在线程阵列。因此,例如,您可以通过调用取消第二个线程 pthread_cancel可以(线程[1])等。

I'm trying to write a multi-threaded program, the number of threads based on command-line input, and so I can't hard-code pre-declared threads. Is this a valid way of doing it?

int threads = 5; // (dynamic, not hard-coded)
int i = 0;
pthread_t * thread = malloc(sizeof(pthread_t)*threads);

for (i = 0; i < threads; i++) {
    pthread_t foobar;
    thread[i] = foobar; // will this cause a conflict?
}

for (i = 0; i < threads; i++) {

    int ret = pthread_create(&thread[i], NULL, (void *)&foobar_function, NULL);

    if(ret != 0) {
        printf ("Create pthread error!\n");
        exit (1);
    }
}


Here's my result from modifications suggested below. Seems to work just fine.

int threads = 5;
int i;

pthread_t * thread = malloc(sizeof(pthread_t)*threads);

for (i = 0; i < threads; i++) {

    int ret = pthread_create(&thread[i], NULL, &foobar_function, NULL);

    if(ret != 0) {
        printf ("Create pthread error!\n");
        exit (1);
    }
    // pthread_join(thread[i], NULL); // don't actually want this here :)
}

sleep(1);     // main() will probably finish before your threads do,
free(thread); // so we'll sleep for illustrative purposes

解决方案

What's in the first cycle? Does it set the array elements to uninitialized value?

So i think that's what you need:

int threads = 5, i = 0, ret = -1;

pthread_t * thread = malloc(sizeof(pthread_t)*threads);

for (i = 0; i < threads; i++) {

    ret = pthread_create(&thread[i], NULL, &foobar_function, NULL);

    if(ret != 0) {
        printf ("Create pthread error!\n");
        exit (1);
    }
}

It spawns threads threads, starting foobar_function in each. And you have (if everything goes well:)) their ids in thread array. So for example you can cancel second thread by calling pthread_cancel(thread[1]) etc.

这篇关于如何产卵n个线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆