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 do before any thread uses the condition variable for the F Irst 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-On-the-do 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. has 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 II threads then on one thread if buffer & Temp is updated so would it effect other thread?
I mean if there is both thread then does there would be is the 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 is allocated on the stack with each thread have its own Stack:these variables is private to each thread (They is not shared). (See this answer for more details.)
If you assign thread_data to a global pointer, for example, and other threads would be able to access Thread_data via the Globa L pointer.
Thread specific data (e.g. pthread_setspecific) is used to create variables that was global, but still specific to each th Read (not shared): They is thread-specific global variables.
You need the use of 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 have its own urgent_q,
But answer_q are for each student,so needs to use in different ways:
Urgent_q[office_no],answer_q[info.id]
System and device Programming LAB3