How to have a dynamic number of threads in C
<p>I would like to calculate a mathematic summation in c using threads. 
(∑x^i ,From 0 to N) Each threads should calculate each terms of the summation and finally in the main the program should sum all of them and print them. 
How should I make the number of threads dynamically?
Here is my code:</p>

<pre><code>#include &lt;unistd.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;pthread.h&gt;
#include &lt;semaphore.h&gt;
#include &lt;math.h&gt;

pthread_t sadaf[10];
int i,a[10];
long int x,N;
int sum=0;
pthread_mutex_t mtx;

void *Myfun(void *tid)
{
    int *ThreadID=(int *)tid;
    pthread_mutex_lock(&amp;mtx);
    printf("The thread with id of %d calculated x^i\n",*ThreadID);
    a[*ThreadID]=pow(x,*ThreadID);
    sum=sum+a[*ThreadID];
    pthread_mutex_unlock(&amp;mtx);

}

int main()
{
     int d[10] = {0};
     printf("-&gt;**************************************************************************&lt;-\n");
     printf("This program will calculate the following function:\n-&gt; ∑x^i ,From 0 to N \n");
     printf("-&gt;**************************************************************************&lt;-\n");
     printf("Please enter x:\n");
     scanf("%ld",&amp;x);
     printf("Please enter N:\n");
     scanf("%ld",&amp;N);

    for (i=0; i&lt;N; i++)
    {
        d[i] = i;
        pthread_create(&amp;sadaf[i],NULL,Myfun,(void *)&amp;d[i]);
    }
    for (i=0; i&lt;N; i++)
    {
        pthread_join(sadaf[i],NULL);
    }


    printf("The sum is: %d\n",sum);

}
</code></pre>