水果v5吧 关注:3贴子:26
  • 4回复贴,共1


1楼2013-01-20 09:17回复
    线程同步
    7 #include<errno.h>
    8 #include<pthread.h>
    9 int counter = 0;
    10 void *pd(void* arg)
    11 {
    12 int val,i;
    13 for(i = 0;i < N ;i++)
    14 {
    15 val = counter;
    16 printf("%x:%d\n",(unsigned int)pthread_self(),val+1 );
    17 counter = val + 1;
    18 usleep(10);
    19 }
    20
    21 return NULL;
    22 }
    23 int main(int argc,char* argv[])
    24 {
    25 int a,b;
    26 pthread_t ptid1,ptid2;
    27 a = pthread_create(&ptid1,NULL,pd,NULL);
    28 b = pthread_create(&ptid2,NULL,pd,NULL);
    29 if(a != 0)
    30 {
    31 fprintf(stderr,"cannt accees%s\n",strerror(errno));
    32 exit(1);
    33 }
    34 if(b != 0)
    35 {
    36 fprintf(stderr,"cannt accees%s\n",strerror(errno));
    37 exit(1);
    38 }
    39
    40 pthread_join(ptid1,NULL);
    41 pthread_join(ptid2,NULL);
    42 return 0;
    43 }


    2楼2013-01-20 09:57
    回复
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      #include <assert.h>
      #include <fcntl.h>
      #include <unistd.h>
      #include <pthread.h>
      #include <sys/stat.h>
      #include <sys/types.h>
      #include <errno.h>
      #define SPEED 5
      typedef struct node{
      int data;
      struct node *next;
      } node;
      typedef struct {
      struct node *head;
      } List;
      void list_init(List *list)
      {
      list->head = NULL;
      }
      void list_destroy(List *list)
      {
      struct node *n, *t;
      n = list->head;
      while(n) {
      t = n->next;
      free(n);
      n = t;
      }
      }
      static struct node * make_node(int data)
      {
      struct node *n;
      n = malloc(sizeof(struct node));
      n->data = data;
      n->next = NULL;
      return n;
      }
      void list_insert(List *list, int data)
      {
      struct node *n;
      n = make_node(data);
      n->next = list->head;
      list->head = n;
      }
      int list_delete(List *list)
      {
      int data;
      struct node *n;
      while (! (n = list->head)) {
      usleep(SPEED);
      }
      data = n->data;
      list->head = n->next;
      free(n);
      return data;
      }
      void list_show(List *list)
      {
      struct node *n;
      n = list->head;
      while(n) {
      printf("%d ", n->data);
      n = n->next;
      }
      putchar('\n');
      }
      void * consumer(void *args)
      {
      List *list = (List *)args;
      while(1) {
      printf("Remove = %d", list_delete(list));
      usleep(SPEED);
      }
      return NULL;
      }
      void * productor(void *args)
      {
      int i = 0;
      List *list = (List *)args;
      while(1) {
      printf("Add = %d\n", i);
      list_insert(list, i);
      i ++;
      usleep(1000);
      }
      return NULL;
      }
      #define NUM 2
      int main(int argc, char *argv[])
      {
      int i, ret;
      pthread_t ptid[NUM];
      List list;
      list_init(&list);
      for (i = 0; i < NUM; i ++) {
      ret = pthread_create(&ptid[i], NULL,
      (i % 2 == 0)? productor : consumer, &list);
      assert(ret == 0);
      }
      for (i = 0; i < NUM; i ++) {
      pthread_join(ptid[i], NULL);
      }
      list_destroy(&list);
      return 0;
      }这还没懂


      3楼2013-01-20 14:15
      收起回复
        #include<stdio.h>
        #include<errno.h>
        #include<unistd.h>
        #include<fcntl.h>
        #include<stdlib.h>
        #include<string.h>
        #include<pthread.h>
        #include<semaphore.h>
        #define NUM 5
        pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
        pthread_mutex_t mutex1[5];
        char kxj[5] = {'A','B','C','D','E'};
        int left[5] = {4,0,1,2,3};
        int right[5] = {0,1,2,3,4};
        void *pthreads(void *arg)
        {
        int i = (int) arg;
        while(1)
        {
        usleep(10000);
        if(EBUSY == pthread_mutex_trylock(&mutex1[left[i]]))
        {
        continue;
        }
        if(EBUSY == pthread_mutex_trylock(&mutex1[right[i]]))
        {
        pthread_mutex_unlock(&mutex1[left[i]]);
        continue;
        }
        printf("%c eating use left %d right %d\n",kxj[i],left[i],right[i]);
        usleep(1);
        pthread_mutex_unlock(&mutex1[left[i]]);
        pthread_mutex_unlock(&mutex1[right[i]]);
        }
        return (void*)i;
        }
        int main(int argc,char*argv[])
        {
        pthread_t pid[NUM];
        int ret,i;
        for(i = 0;i < NUM;i++)
        {
        ret = pthread_create(&pid[i],NULL,pthreads,(void*)i);
        if(ret != 0)
        {
        fprintf(stderr,"cannt access %s\n",strerror(errno));
        }
        }
        for(i = 0;i < NUM;i++)
        {
        pthread_join(pid[i],NULL);
        }
        return 0;
        }


        4楼2013-01-20 19:52
        回复