- 12th Aug 2022
- 03:05 am
- Admin
#include
#include
#include
#include
int shared_data;
#define MAX_THREAD 200
void *thread_code(int * p)
{
int my_id = *(int *)p;
printf("I am thread %d, starting up now\n",my_id);
return NULL;
}
int main(int argc, char * argv[])
{
int ret;
int id = 1;
pthread_t threads[MAX_THREAD];
int thread_count = atoi(argv[1]);
if(thread_count>=MAX_THREAD)
{
printf("You can have at most %d threads",MAX_THREAD);
exit(0);
}
for(int i=0;i {
ret = pthread_create(&threads[i], NULL, thread_code, (int *)&id);
}
// Now join the threads back together
for (int i = 0; i < MAX_THREAD; i++)
pthread_join(threads[i], NULL);
return 0;
}