简体   繁体   English

使用pthread_create()创建1000多个线程

[英]creating more than 1000 threads using pthread_create()

I'm trying to create 1000 threads using the pthread_create() function. 我正在尝试使用pthread_create()函数创建1000个线程。

This is the statement I'm using: 这是我正在使用的语句:

for (int i=0 ; i <1000; i++)
{
   retValue = pthread_create(&threadId, NULL, simplethreadFunction, NULL);
}

Everytime this for-loop runs does it create a new thread? 每次运行此for循环时,都会创建一个新线程吗?

This is a simple thing. 这很简单。 But I'm unable to understand it. 但是我听不懂。

Everytime this for-loop runs does it create a new thread? 每次运行此for循环时,都会创建一个新线程吗?

Yes, it does. 是的,它确实。

This is a simple thing. 这很简单。 But I'm unable to understand it. 但是我听不懂。

I will add a few more points: 我还要补充几点:

  1. First parameter to the function pthread_create is pointer type to pthread_t. 函数pthread_create的第一个参数是指向pthread_t的指针类型。 Basically you are passing an address to this function, which this function uses to assign 'something'. 基本上,您是将地址传递给此函数,该函数用于分配“内容”。

    When this function creates a thread, an 'opaque, unique identifier' for this thread is created and the pointer you passed is made to point to this location, so that you can access it later, if required. 当此函数创建线程时,将为此线程创建一个“不透明的唯一标识符”,并使传递的指针指向该位置,以便以后可以根据需要访问它。

  2. If you will pass the same pointer all the 1000 times, you will have access to the unique identifier for only one (the last one) thread created out of all 1000, because each time the previous value will get over written. 如果将同一指针全部传递1000次,则只能访问全部1000个线程中唯一一个(最后一个)线程的唯一标识符,因为每次以前的值都会被覆盖。

  3. This unique value is required if you would want to perform further operations on a thread (like joining etc). 如果您想对线程执行进一步的操作(例如,连接等),则需要此唯一值。

  4. For details about this function and other thread related functions you can go though this and this . 有关此功能和其他线程相关功能的详细信息,可以通过thisthis进行操作

  5. Don't forget to call pthread_exit in your main context, otherwise complete program (including the created threads) might terminate even before all your threads would have finished. 不要忘记在您的主上下文中调用pthread_exit,否则完整的程序(包括创建的线程)可能甚至在所有线程都将完成之前就终止了。

Also regarding the time, this thing might not have any effect on time of creation as far as I think, will just reduce the usability of threads you have created. 关于时间,就我所知,这件事可能对创建时间没有任何影响,只会降低您创建的线程的可用性。 Also, this time you are calculating is not THE time for creating 1000 threads, will depend on lot of other factors like platform/implementation etc. 同样,这次您要计算的时间还不是创建1000个线程的时间,这取决于许多其他因素,例如平台/实现等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM