-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadCreate.c
More file actions
50 lines (36 loc) · 1.07 KB
/
threadCreate.c
File metadata and controls
50 lines (36 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include "macros.h"
void* thread_fun(void*);
int main(int argc, char *argv[]) {
pthread_t tid1;
int argument = 1;
void * retValue;
// Creazione Thread
SYST(pthread_create(&tid1, NULL, thread_fun, &argument));
// Allocazione memoria per il valore di ritorno
SYSCN(retValue, malloc(sizeof(int)), "nella malloc");
// Attesa terminazione Thread
SYST(pthread_join(tid1, &retValue));
if(retValue != NULL) {
printf("Valore restituito dal thread: %d\n", *(int*)retValue);
free(retValue);
} else {
printf("Errore terminazione Thread");
exit(EXIT_FAILURE);
}
}
void* thread_fun(void *arg) {
int *retData;
pthread_t tid;
// Tid del thread
SYST((tid = pthread_self()));
printf("Thread %d, value: %d\n", tid, *(int*)arg);
// Allocazione memoria per il valore di ritorno
SYSCN(retData, malloc(sizeof(int)), "nella malloc");
*retData = 69;
// Terminazione del thread
pthread_exit(retData);
}