Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /*
- * File: main.cpp
- * Author: docenti
- *
- * Created on 7 ottobre 2019, 13:26
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <math.h>
- //#define EXIT_FAILURE -1
- #define EXIT_SUCCESS 0
- void* print_message_function( void *ptr );
- int main()
- {
- const char *message1 = "Threadd 1";
- const char *message2 = "Thread 2";
- // Create independent threads each of which will execute 'print_message_function'
- pthread_t thread1;
- int cond1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
- if (cond1 != 0) {
- fprintf(stderr,"Error - pthread_create() return code: %d\n", cond1);
- exit(EXIT_FAILURE);
- }
- pthread_t thread2;
- int cond2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
- if (cond2 != 0){
- fprintf(stderr,"Error - pthread_create() return code: %d\n", cond2);
- exit(EXIT_FAILURE);
- }
- printf("pthread_create() for thread 1 returns: %d\n", cond1);
- printf("pthread_create() for thread 2 returns: %d\n", cond2);
- // Attendiamo che i thread siano terminati prima di proseguire con la main.
- // Se non aspettassimo, rischieremmo di eseguire la exit() che terminerrebbe
- // il processo principale ma ache i thread prima che questi abbiano completato
- // naturalmente la loro esecuzione.
- pthread_join(thread1, NULL);
- pthread_join(thread2, NULL);
- exit(EXIT_SUCCESS);
- }
- void *print_message_function( void *ptr )
- {
- char *message;
- message = (char *) ptr;
- printf("%s \n", message);
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement