Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <pthread.h>
- #define MAX_THREADS 5
- void *print_message(void *message)
- {
- const char *msg = (char *) message;
- printf("%s!\n", msg);
- free(message);
- pthread_exit(NULL);
- }
- int main(int argc, char *argv[])
- {
- const char *fmt = "Here is %d";
- pthread_t threads[MAX_THREADS];
- char *message;
- int i;
- for(i = 0; i < MAX_THREADS; ++i) {
- message = malloc(sizeof(char) * strlen(fmt));
- sprintf(message, fmt, i);
- if (pthread_create(&threads[i], NULL, print_message,
- (void *) message)) {
- fprintf(stderr,
- "[ERROR]: while creating thread %d\n", i);
- exit(EXIT_FAILURE);
- }
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement