簡體   English   中英

產生任意數量的線程

[英]Spawning Arbitrary Number of Threads

我正在嘗試編寫一個程序,它將產生任意數量的線程,類似於我在將基於進程的程序轉換為基於線程的版本中的代碼? ,它使用進程來完成我想要完成的事情,到目前為止,我有以下代碼,目前我收到很多警告,但我真的想知道我是否正在接近我想要做的事情有點正確。

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

void *runner(void *param); //the thread

int main(int argc, char *argv[]) {
  pthread_t tid = gettid();
  pthread_attr_t attr;

  if (argc != 2){
    fprintf(stderr, "Usage: a.out <integer value>\n");
    return -1;
  }

  if (atoi(argv[1]) < 0) {
    fprintf(stderr, "Argument %d must be non negative\n", atoi(argv[1]));
    return -1;
  }

  printf("My thread identifier is: %d\n", tid);

  // default attributes
  pthread_attr_init(&attr);

  // create the thread
  pthread_create(&tid, &attr, runner, argv[1]);

  // wait for the thread to exit
  pthread_join(tid, NULL);

}

void *runner(void *param){
  //int i, upper = atoi(param);
  int i;
  srand48(gettid());
  int max = nrand()%100;

  if (max > 0){
    for (i=1; i<=max; i++){
      printf("Child %d executes iteration\n", param, i);
    }
  }
  pthread_exit(0);
}

感謝我能得到的任何指導!

如果我理解您的目標,您希望創建命令行參數指示的線程數。

(請記住,任何特定的操作系統只能支持固定數量的線程,這取決於操作系統,所以我不會在這里驗證這個數字的大小。)

以下建議的代碼:

  1. 干凈地編譯
  2. 執行所需的功能
  3. 記錄為什么包含每個 header 文件
  4. 檢查從 C 庫函數返回的錯誤指示,例如pthread_create()

現在建議的代碼:

#include <stdio.h>   // printf(), perror(), NULL
#include <pthread.h> // pthread_create(), pthread_join(), pthread_t
#include <stdlib.h>  // exit(), EXIT_FAILURE, atof()

void *runner(void *param); //the thread

int main(int argc, char *argv[]) 
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s <integer value>\n", argv[0]);
        exit( EXIT_FAILURE );
    }

    // might want to use: `strtol()` rather than `atoi()` 
    // so can check for errors
    size_t maxThreads = (size_t)atoi(argv[1]);

    pthread_t tid[ maxThreads ];   
    for( size_t i=0; i<maxThreads; i++ )
    {
        tid[i] = 0;
    }

    // create the threads
    for( size_t i=0; i<maxThreads; i++ )
    {
        if( pthread_create( &tid[i], NULL, runner, (void *)i ) )
        { 
            perror( "pthread_create failed" );
        }
    }

    // wait for each thread to exit
    for( size_t i = 0; i<maxThreads; i++ )
    {
        // if thread was created, then wait for it to exit
        if( tid[i] != 0 )
        {
            pthread_join( tid[i], NULL );
        }
    }
}


void *runner(void *arg)
{
    size_t threadNum = (size_t)arg;
    printf( "in thread: %zu\n", threadNum );

    pthread_exit( NULL );
}

沒有命令行參數的運行會導致:(其中可執行文件的名稱為:untitled

Usage: ./untitled <integer value>

命令行參數為 10 的運行會導致:

in thread: 0
in thread: 4
in thread: 2
in thread: 6
in thread: 1
in thread: 5
in thread: 7
in thread: 8
in thread: 9
in thread: 3

這清楚地表明線程沒有按特定順序運行

1:我沒有看到 function 調用gettid()

pthread_t tid = gettid();
srand48(gettid());

2:您不能將 pthread_t 打印為 integer,它是一個結構

printf("My thread identifier is: %d\n", tid);

3:是rand(),之前沒見過nrand()。

int max = nrand()%100;

修復這些問題並根據需要編輯問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM