I'm trying to write a program which will spawn an arbitrary number of threads, similar to the code I have in Convert a process based program into a thread based version? , which uses processes to do what I'm trying to accomplish, so far I have the following code, I'm getting a lot of warnings currently, but I'm really wondering if I'm approaching what I'm trying to do somewhat correctly.
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void *runner(void *param); //the thread
int main(int argc, char *argv[]) {
pthread_t tid = gettid();
pthread_attr_t attr;
if (argc != 2){
fprintf(stderr, "Usage: a.out <integer value>\n");
return -1;
}
if (atoi(argv[1]) < 0) {
fprintf(stderr, "Argument %d must be non negative\n", atoi(argv[1]));
return -1;
}
printf("My thread identifier is: %d\n", tid);
// default attributes
pthread_attr_init(&attr);
// create the thread
pthread_create(&tid, &attr, runner, argv[1]);
// wait for the thread to exit
pthread_join(tid, NULL);
}
void *runner(void *param){
//int i, upper = atoi(param);
int i;
srand48(gettid());
int max = nrand()%100;
if (max > 0){
for (i=1; i<=max; i++){
printf("Child %d executes iteration\n", param, i);
}
}
pthread_exit(0);
}
Appreciate any guidance I can get with this!
If I understand your objective, you want to create the number of threads as the command line parameter indicates.
(remembering that any specific OS can only support a fixed number of threads, which varies depending on the OS, so I will not validate the magnitude that number here.)
the following proposed code:
pthread_create()
and now the proposed code:
#include <stdio.h> // printf(), perror(), NULL
#include <pthread.h> // pthread_create(), pthread_join(), pthread_t
#include <stdlib.h> // exit(), EXIT_FAILURE, atof()
void *runner(void *param); //the thread
int main(int argc, char *argv[])
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s <integer value>\n", argv[0]);
exit( EXIT_FAILURE );
}
// might want to use: `strtol()` rather than `atoi()`
// so can check for errors
size_t maxThreads = (size_t)atoi(argv[1]);
pthread_t tid[ maxThreads ];
for( size_t i=0; i<maxThreads; i++ )
{
tid[i] = 0;
}
// create the threads
for( size_t i=0; i<maxThreads; i++ )
{
if( pthread_create( &tid[i], NULL, runner, (void *)i ) )
{
perror( "pthread_create failed" );
}
}
// wait for each thread to exit
for( size_t i = 0; i<maxThreads; i++ )
{
// if thread was created, then wait for it to exit
if( tid[i] != 0 )
{
pthread_join( tid[i], NULL );
}
}
}
void *runner(void *arg)
{
size_t threadNum = (size_t)arg;
printf( "in thread: %zu\n", threadNum );
pthread_exit( NULL );
}
a run with no command line parameter results in: (where the executable is named: untitled
Usage: ./untitled <integer value>
a run with a command line parameter of 10 results in:
in thread: 0
in thread: 4
in thread: 2
in thread: 6
in thread: 1
in thread: 5
in thread: 7
in thread: 8
in thread: 9
in thread: 3
which makes it clear that threads are run in no particular order
1: I see no function called gettid()
pthread_t tid = gettid();
srand48(gettid());
2: You cannot print pthread_t as an integer, it's a structure
printf("My thread identifier is: %d\n", tid);
3: it's rand(), I have not seen nrand() before.
int max = nrand()%100;
Fix these and edit the question if required.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.