1 /* each thread tries to update variable best_cost as follows */ 2 if (my_cost < best_cost) 3 best_cost = my_cost;
1 int 2 pthread_mutex_lock ( 3 pthread_mutex_t *mutex_lock);
1 int 2 pthread_mutex_unlock ( 3 pthread_mutex_t *mutex_lock);
1 int 2 pthread_mutex_init ( 3 pthread_mutex_t *mutex_lock, 4 const pthread_mutexattr_t *lock_attr);This function initializes the mutex-lock mutex_lock to an unlocked state. The attributes of the mutex-lock are specified by lock_attr. If this argument is set to NULL, the default mutex-lock attributes are used (normal mutex-lock).
1 #include <pthread.h>
2 void *find_min(void *list_ptr);
3 pthread_mutex_t minimum_value_lock;
4 int minimum_value, partial_list_size;
5
6 main() {
7 /* declare and initialize data structures and list */
8 minimum_value = MIN_INT;
9 pthread_init();
10 pthread_mutex_init(&minimum_value_lock, NULL);
11
12 /* initialize lists, list_ptr, and partial_list_size */
13 /* create and join threads here */
14 }
15
16 void *find_min(void *list_ptr) {
17 int *partial_list_pointer, my_min, i;
18 my_min = MIN_INT;
19 partial_list_pointer = (int *) list_ptr;
20 for (i = 0; i < partial_list_size; i++)
21 if (partial_list_pointer[i] < my_min)
22 my_min = partial_list_pointer[i];
23 /* lock the mutex associated with minimum_value and
24 update the variable as required */
25 pthread_mutex_lock(&minimum_value_lock);
26 if (my_min < minimum_value)
27 minimum_value = my_min;
28 /* and unlock the mutex */
29 pthread_mutex_unlock(&minimum_value_lock);
30 pthread_exit(0);
31 }
1 pthread_mutex_t task_queue_lock;
2 int task_available;
3
4 /* other shared data structures here */
5
6 main() {
7 /* declarations and initializations */
8 task_available = 0;
9 pthread_init();
10 pthread_mutex_init(&task_queue_lock, NULL);
11 /* create and join producer and consumer threads */
12 }
13
14 void *producer(void *producer_thread_data) {
15 int inserted;
16 struct task my_task;
17 while (!done()) {
18 inserted = 0;
19 create_task(&my_task);
20 while (inserted == 0) {
21 pthread_mutex_lock(&task_queue_lock);
22 if (task_available == 0) {
23 insert_into_queue(my_task);
24 task_available = 1;
25 inserted = 1;
26 }
27 pthread_mutex_unlock(&task_queue_lock);
28 }
29 }
30 }
31
32 void *consumer(void *consumer_thread_data) {
33 int extracted;
34 struct task my_task;
35 /* local data structure declarations */
36 while (!done()) {
37 extracted = 0;
38 while (extracted == 0) {
39 pthread_mutex_lock(&task_queue_lock);
40 if (task_available == 1) {
41 extract_from_queue(&my_task);
42 task_available = 0;
43 extracted = 1;
44 }
45 pthread_mutex_unlock(&task_queue_lock);
46 }
47 process_task(my_task);
48 }
49 }
1 int 2 pthread_mutex_trylock ( 3 pthread_mutex_t *mutex_lock);
1 void *find_entries(void *start_pointer) {
2
3 /* This is the thread function */
4
5 struct database_record *next_record;
6 int count;
7 current_pointer = start_pointer;
8 do {
9 next_record = find_next_entry(current_pointer);
10 count = output_record(next_record);
11 } while (count < requested_number_of_records);
12 }
13
14 int output_record(struct database_record *record_ptr) {
15 int count;
16 pthread_mutex_lock(&output_count_lock);
17 output_count ++;
18 count = output_count;
19 pthread_mutex_unlock(&output_count_lock);
20
21 if (count <= requested_number_of_records)
22 print_record(record_ptr);
23 return (count);
24 }
1 int output_record(struct database_record *record_ptr) {
2 int count;
3 int lock_status;
4 lock_status = pthread_mutex_trylock(&output_count_lock);
5 if (lock_status == EBUSY) {
6 insert_into_local_list(record_ptr);
7 return(0);
8 }
9 else {
10 count = output_count;
11 output_count += number_on_local_list + 1;
12 pthread_mutex_unlock(&output_count_lock);
13 print_records(record_ptr, local_list,
14 requested_number_of_records - count);
15 return(count + number_on_local_list + 1);
16 }
17 }