Hi,
The following program will launch 3 threads and wait for all the 3 to complete. If the thread 2 complets first, it has to wait till the other two complets.
Can anyone suggest me a way if the 2nd thread complets first then I want to launch a new thread of same type without waiting for the other two threads to complete(these 2 threads will be still running). The maximum number of threads can be active at any point of time will be 3.
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 3
void *BusyWork(void *null)
{
int i;
double result=0.0;
for (i=0; i<1000000; i++)
{
result = result + (double)random();
}
printf("result = %e\n",result);
pthread_exit((void *) 0);
}
int main (int argc, char *argv[])
{
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;
int rc, t, status;
/* Initialize and set thread detached attribute */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(t=0; t<NUM_THREADS; t++)
{
printf("Creating thread %d\n", t);
rc = pthread_create(&thread[t], &attr, BusyWork, NULL);
if (rc)
{
printf("ERROR; return code from pthread_create()
is %d\n", rc);
exit(-1);
}
}
/* Free attribute and wait for the other threads */
pthread_attr_destroy(&attr);
for(t=0; t<NUM_THREADS; t++)
{
rc = pthread_join(thread[t], (void **)&status);
if (rc)
{
printf("ERROR; return code from pthread_join()
is %d\n", rc);
exit(-1);
}
printf("Completed join with thread %d status= %d\n",t, status);
}
pthread_exit(NULL);
}
Thanks,
Sujin
The following program will launch 3 threads and wait for all the 3 to complete. If the thread 2 complets first, it has to wait till the other two complets.
Can anyone suggest me a way if the 2nd thread complets first then I want to launch a new thread of same type without waiting for the other two threads to complete(these 2 threads will be still running). The maximum number of threads can be active at any point of time will be 3.
#include <pthread.h>
#include <stdio.h>
#define NUM_THREADS 3
void *BusyWork(void *null)
{
int i;
double result=0.0;
for (i=0; i<1000000; i++)
{
result = result + (double)random();
}
printf("result = %e\n",result);
pthread_exit((void *) 0);
}
int main (int argc, char *argv[])
{
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;
int rc, t, status;
/* Initialize and set thread detached attribute */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for(t=0; t<NUM_THREADS; t++)
{
printf("Creating thread %d\n", t);
rc = pthread_create(&thread[t], &attr, BusyWork, NULL);
if (rc)
{
printf("ERROR; return code from pthread_create()
is %d\n", rc);
exit(-1);
}
}
/* Free attribute and wait for the other threads */
pthread_attr_destroy(&attr);
for(t=0; t<NUM_THREADS; t++)
{
rc = pthread_join(thread[t], (void **)&status);
if (rc)
{
printf("ERROR; return code from pthread_join()
is %d\n", rc);
exit(-1);
}
printf("Completed join with thread %d status= %d\n",t, status);
}
pthread_exit(NULL);
}
Thanks,
Sujin