Bonjour,

J'aimerais écrire un code qui permet d'affecter chaque thread sur un core.
Alors je fais quelques recherches et j'ai essayé d'écrire un code qui permet de faire ça.
D'après quelques recherches, j'ai trouvé que "sched_setaffinity" de pthread peut me donner un résultat. Ci dessous mon code, j'ai essayé de faire ça mais il n'ai pas marché:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <pthread.h>
#include <stdio.h>
#include <sched.h>
#include <sys/types.h>
 
// Number of CPUS
int n_cpus = 2;
 
 
int n_threads = 2;
 
 
int thread_counter = 0;
 
void *thread_func(void *thread_id)
{
  cpu_set_t cpuset;
  int threadnum = (int) thread_id;
 
 
  CPU_ZERO(&cpuset);
  CPU_SET(threadnum % n_cpus, &cpuset);
  CPU_CLR(1, &cpuset);
  sched_setaffinity(0, sizeof (cpuset), &cpuset);
 
  printf("Thread %d: Hello World!\n", threadnum);
 
 
 
  return (void*)NULL;
}
void *thread_func1(void *thread_id)
{
  cpu_set_t cpuset;
  int threadnum = (int) thread_id;
 
 
  CPU_ZERO(&cpuset);
  CPU_SET(threadnum % n_cpus, &cpuset);
  CPU_CLR(1, &cpuset);
  sched_setaffinity(0, sizeof (cpuset), &cpuset);
 
  printf("Thread %d: Hello World!\n", threadnum);
 
 
 
  return (void*)NULL;
}
 
//
// main func
//
int main(int argc, char* argv[])
{
  int threadnum;
  int i;
 
  printf("Main process: Hello world!\n");
 
  pthread_t thread;
  pthread_attr_t thread_attr;
  pthread_attr_init(&thread_attr);
 
 
  threadnum = 0;
  pthread_create(&thread, &thread_attr, thread_func, (void *)threadnum);
  threadnum = 1;
  pthread_create(&thread, &thread_attr, thread_func, (void *)threadnum);
 
 
 
 
  return 0;
}
Alors , y a-t-il quelqu'un qui peut m'aider ?
Merci d'avance.
Moktar.