[英]pthread how to create many threads
To create one thread, I did like this: 要创建一个线程,我这样做是这样的:
void *routine(void *i){....}
pthread_t thread1;
pthread_create(&thread1, NULL, routine, NULL);
Now I want to create 100 threads and all of them execute routine
, do I have to do like below? 现在,我想创建100个线程,并且所有这些线程都执行
routine
,是否必须执行以下操作? Is it possible to use a for loop? 是否可以使用for循环?
pthread_t thread1;
pthread_t thread2;
...
pthread_t thread100;
pthread_create(&thread1, NULL, routine, NULL);
pthread_create(&thread2, NULL, routine, NULL);
....
pthread_create(&thread100, NULL, routine, NULL);
You can create an array of threads 您可以创建线程数组
#define NTHREADS 100
pthread_t th[NTHREADS];
int i;
for (i=0;i<NTHREADS;++i)
pthread_create(&th[i],...);
Just take pthread_t thread_arr[100];
只要拿
pthread_t thread_arr[100];
(array of threads). (线程数组)。 Work it out as you work with single thread_t variable.
在使用单个thread_t变量时进行计算。 Use pthread_arr[1], pthread_arr[2]... as individual variables.
使用pthread_arr [1],pthread_arr [2] ...作为单个变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.