- pthread_cond_wait(buf->notfull,&buf->lock);
notfull is a pointer,so don‘t need to use &.
- if use int *p,needs to allocate space,but use int p[10],don‘t need
- use gdb to detect segmentation fault, which usually due to malloc
- /*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); - 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:
- take care of the types declared in the structure.
- 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]