码迷,mamicode.com
首页 > 其他好文 > 详细

System and device programming lab3

时间:2016-03-28 10:28:07      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:mutex   thread   

  1. pthread_cond_wait(buf->notfull,&buf->lock);

    notfull is a pointer,so don‘t need to use &.

  2. if use int *p,needs to allocate space,but use int p[10],don‘t need

  3. use gdb to detect segmentation fault, which usually due to malloc

  4. /*pthread_cond_init: initialize a condition variable: must be done before any thread uses the condition variable for the first time*/
      pthread_cond_init(b->notfull,NULL);

  5. check the urgent_Q first,which is for satisfying priority of serving urgent

service first

  6.urgent_Q=(Buffer **)malloc(NUM_OFFICES*sizeof(Buffer *)); it‘s not Buffer

but Buffer*

  7./*for(i=0;i<NUM_OFFICES;){
  printf("i=%d\n",i);
  //id[i]=rand()%5;
  pjj=i;
  i++;
  pthread_create(&pj[i],NULL,office,&pjj);// &pj[i],not pj[i]
}*///this way will make order confused of value pjj which may have 1 2 3 3

so can use:

for(i=0;i<NUM_OFFICES;i++){
pjj[i]=i;
pthread_create(&pj[i],NULL,office,&pjj[i]);
}

8.For Buffer **answer_Q:

answer_Q=malloc(NUM_OFFICES*sizeof(Buffer *));

for(i=0;i<k;i++)
   answer_Q[i]=B_init(MaxQue);

For Buffer *special_Q:

special_Q=B_init(MaxQue);

9. have declared some local variable in one function like this:

void* thread_function (void* parameter)
{
   struct parameter * thread_data = (struct parameter *)parameter;
   char buffer[20];
   int temp;
}

Here if I have created two threads then in one thread if buffer & temp is updated so will it effect other thread ?

i mean if there are two thread then does there will be two copy of all local variable?

EDIT : then in which case i need to used thread specific data.? i mean pthread_setspecific & all such stuff

These variables are allocated on the stack, and each thread has its own stack: these variables are private to each thread (they are not shared). (See this answer for more details.)

If you assign thread_data to a global pointer, for example, other threads will be able to access thread_data via the global pointer.

Thread specific data (e.g. pthread_setspecific) is used to create variables that are global, but still specific to each thread (not shared): They are thread-specific global variables.

You need to use thread specific variables when you want global variables, but don‘t want to share them between threads.

conclusion:

  1. take care of  the types declared in the structure.

  2. watch carefully in the picture,like every office has its own urgent_Q,

but answer_Q is for each student,so needs to use in different ways:

urgent_Q[office_no],answer_Q[info.id]

System and device programming lab3

标签:mutex   thread   

原文地址:http://11259454.blog.51cto.com/11249454/1757386

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!