Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.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_Multithreading_Pthreads - Fatal编程技术网

C 创建动态数量的线程

C 创建动态数量的线程,c,multithreading,pthreads,C,Multithreading,Pthreads,我想创建一些由用户指定的线程。我为此编写的代码是: int nhijos = atoi(argv[1]); thread = malloc(sizeof(pthread_t)*nhijos); for (i = 0; i < nhijos; i++){ if (pthread_create ( &thread[i], NULL, &hilos_hijos, (void*) &info ) != 0){ perror("Error al crear el

我想创建一些由用户指定的线程。我为此编写的代码是:

int nhijos = atoi(argv[1]);

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

for (i = 0; i < nhijos; i++){
  if (pthread_create ( &thread[i], NULL, &hilos_hijos, (void*) &info ) != 0){
  perror("Error al crear el hilo. \n");
  exit(EXIT_FAILURE);
}   
int-nhijos=atoi(argv[1]);
线程=malloc(sizeof(pthread_t)*nhijos);
对于(i=0;i
这是正确的吗

#include<stdio.h>
#include<pthread.h>

void* thread_function(void)
{
    printf("hello");
}
int main(int argc,char *argv[])
{
    int noOfThread= atoi(argv[1]);
    pthread_t thread_id[noOfThread];
    int i;
    int status;
    for(i=0;i<noOfThread;i++)
    {
        pthread_create (&thread_id[i], NULL , &thread_function, NULL);
    }  

    for(i=0;i<noOfThread;i++)
        pthread_join(thread_id[i],NULL);   
}
因此将创建3个线程


在代码中

1> 你为什么要去马洛克


2> 如果是malloc,那你为什么不释放它呢?

是的,但我会做以下事情:

  • 在调用atoi之前验证argc>1(argv[1])

  • validate numberOfThreads是一个正数,并且小于一个合理的范围。(如果用户键入1000000)

  • 验证malloc的返回值是否不为null

  • pthread_create不会在失败时设置errno。因此perror可能不是在失败时调用的正确函数

  • if(argc>1)
    {
    int numberOfThreads=atoi(argv[1]);
    如果((线程数\u线程数\u最大值))
    {
    printf(“线程计数的参数无效\n”);
    退出(退出失败);
    }
    线程=malloc(sizeof(pthread\u t)*线程数);
    如果(线程==NULL)
    {
    printf(“内存不足\n”);
    退出(退出失败);
    }
    对于(i=0;i
    为什么?为什么要创建用户定义的多个线程?只需获取完成任务所需的线程数。因为用户应该指定并发线程数…因为这就是我们应该如何编程此项目…编译时抛出错误…我以前尝试过此方法,但它抛出警告gs,这就是我使用mallocpadre的原因。c:12:5:警告:ISO C90禁止可变长度数组“thread\u id”padre。c:70:2:警告:从不兼容的指针类型/usr/include/pthread传递“pthread\u create”的参数1。h:225:12:注意:应为“pthread\u t*restrict”,但参数类型为“pthread\u t**”padre。c:80:2:警告:传递从不兼容的指针类型/usr/include/pthread.h:225:12:注意:应为“pthread\u t*restrict”,但参数类型为“pthread\u t**”padre.c:89:7:警告:传递“pthread\u join”的参数1会使指针中的整数在c90中不带castoh,我认为c99中不允许使用VLA。这些是允许的。我明白了,这就是为什么你会收到警告。在这种情况下,这是有效的。我试试看。忘了检查malloc。还有检查argc的不错的补充。谢谢
    ./a.exe 3
    
    if (argc > 1)
    {
        int numberOfThreads = atoi(argv[1]); 
        if ((numberOfThreads <= 0) || (numberOfThreads > REASONABLE_THREAD_MAX))
        {
            printf("invalid argument for thread count\n");
            exit(EXIT_FAILURE);
        }
     
        thread = malloc(sizeof(pthread_t)*numberOfThreads); 
        if (thread == NULL)
        {
           printf("out of memory\n");
           exit(EXIT_FAILURE);
        }
    
        for (i = 0; i < numberOfThreads; i++)
        { 
            if (pthread_create ( &thread[i], NULL, &hilos_hijos, (void*) &info ) != 0)
            { 
                printf("Error al crear el hilo. \n"); 
                exit(EXIT_FAILURE); 
            }    
        }