---
# System prepended metadata

title: 'linux2021: abcdtony1'

---

# linux2021: abcdtony1

## 測驗 δ-1
在function: con_pop
```cpp
void *con_pop(con_queue_t *queue)
{
    mtx_lock(queue->first_mutex);

    node_t *node = queue->first;             /* Node to be removed */
    node_t *new_header = queue->first->next; /* become the first in the queue */

    /* Queue is empty */
    if (!new_header) {
        mtx_unlock(queue->first_mutex);
        return NULL;
    }

    /* Queue not empty: retrieve data and rewire */
    void *return_value = BBB;
    CCC;

    mtx_unlock(queue->first_mutex);

    /* Free removed node and return */
    free(node);
    return return_value;
}
```
如上面程式碼中 /*Queue is empty*/的部分
其中的判斷條件應該改為"(!node)"
否則最後一個thread的最後一個node
會因為new_header等於NULL
此function會return NULL回原function
使此thread無法從while中break出來

## 測驗 δ-2

function: con_push 更改如下
```
int con_push(con_queue_t *restrict queue, void *restrict new_element)
{
    node_t *node = _con_node_init(new_element);
    if (!node)
        return Q_ERROR;
    while(1){
    if(__sync_bool_compare_and_swap (&queue->last->next, NULL, node)){
    	queue->last = node;
        return Q_OK;
        }
    }  
}
```
function: con_pop 更改如下
```
void *con_pop(con_queue_t *queue)
{
    node_t *new_header = NULL;
    node_t *node = NULL;
    while(1){
       node = queue->first;                 
       if (!node) return NULL;
       new_header = node->next;   
       if(__sync_bool_compare_and_swap (&queue->first, node, new_header)){
         void *return_value = node->value;
         free(node);
         return return_value;
         }
    }
}
```

