Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "graphic.h"
  4. #include <pthread.h>
  5. #include <sys/sysinfo.h>
  6.  
  7. //get_nprocs_conf
  8. //Remplir le tableau de la structure ppointex
  9. //Tableau de taille 25 dans tableau 8 -> tableau 1 = thread/core 1
  10.  
  11. #define N 200
  12.  
  13. typedef struct {
  14. int x,y,color;
  15. } Point;
  16.  
  17. typedef struct{
  18. Point *pointsProc[];
  19. pthread_mutex_t mutex;
  20. }Ppointex;
  21.  
  22. void *my_thread_process (void * arg) {
  23. /*Ppointex *pex = (Ppointex*) arg;
  24. while(getppid()!=1){
  25. if(pthread_mutex_lock(&pex->mutex) != 0){
  26. perror("pthread_mutex_lock");
  27. exit(1);
  28. }
  29.  
  30. (pex->pp)->x+=g_random()%2?1:-1;
  31. (pex->pp)->y+=g_random()%2?1:-1;
  32.  
  33. if(pthread_mutex_unlock(&pex->mutex) != 0){
  34. perror("pthread_mutex_unlock");
  35. exit(1);
  36. }
  37.  
  38. sleep(0.02);
  39. }
  40. pthread_exit (NULL);*/
  41. }
  42.  
  43.  
  44. int main() {
  45. Point points[N];
  46. int i,caseTableau;
  47. pthread_t th;
  48. void * ret;
  49. int nbProc= get_nprocs_conf();
  50. caseTableau=N/nbProc;
  51. Ppointex ppointex;
  52. ppointex.pointsProc=(Point*)sizeof(caseTableau*sizeof(Point));
  53. for(i=0; i<N; ++i) {
  54. points[i].x=g_random()%(G_WIDTH-200)+100;
  55. points[i].y=g_random()%(G_HEIGHT-200)+100;
  56. points[i].color=g_random()%G_NB_COLORS;
  57. }
  58.  
  59. for(i=0; i<N; ++i) {
  60. pthread_mutex_init(&p[i].mutex,NULL);
  61. p[i].pp=&points[i];
  62. if(pthread_create (&th,NULL,my_thread_process,&p[i]) < 0){
  63. fprintf (stderr,"pthread_create error for thread \n");
  64. exit (1);
  65. }
  66. }
  67. g_init();
  68. while(1){
  69. g_clear();
  70. for(i=0; i<N; ++i){
  71. g_draw(points[i].x, points[i].y, points[i].color);
  72. }
  73. g_flush();
  74. g_msleep(20);
  75.  
  76. }
  77. return 0;
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement