linux thread operations

Condition variable initialization

int pthread_cond_init (pthread_cond_t CV *, * pthread_cond_attr of cattr); 
function return value: return 0 on success, else return failure. 
Parameters: pthread_cond_attr pthread_cond_t attribute is used to set, and when the incoming value is NULL indicates the default property.

When the function returns, condition variables created is stored in the memory pointed to by cv, you can use the macro PTHREAD_COND_INITIALIZER to initialize the condition variable. It is worth noting that you can not use multiple threads to initialize the same condition variable, a thread when you want to use condition variables to ensure that it is not in use.

 

Destroy condition variable

int pthread_cond_destroy (pthread_cond_t * CV); 
Return value: Returns 0 for success, return other value indicates failure.

 

Condition variable use:

int pthread_cond_wait(pthread_cond_t *cv,pthread_mutex_t *mutex)
int pthread_cond_signal(pthread_cond_t *cv);

 

Used as follows:

pthread_mutex_lock (& mutex)
 the while or IF (condition thread of execution is established) 
    pthread_cond_wait ( & cond, & mutex); 
thread execution 
pthread_mutex_unlock ( & mutex);

 

 

Why lock

  1. Partial access thread execution resources in the process, there may be multiple threads need to access it, in order to avoid competition for resources due to the concurrently executing threads caused, so let each thread mutually exclusive access to public resources.
  2. While if the conditions are not met or if judgment execution thread, the thread back to the calling pthread_cond_wait blocking themselves. pthread_cond_wait calling thread is blocked when, pthread_cond_wait will automatically release the mutex. Thread releases the mutex calls from pthread_cond_wait to time after the operating system thread waits put him in the queue.

 

While the use of threads of execution and determine if the difference between conditional release established.

In a multithreaded resource competition, when the use of resources in a thread inside (consumer) determine whether the resource is available, unavailable, then call pthread_cond_wait, in another thread inside (the producer) to determine if resources are available, then this will send calls pthead_cond_signal a signal available resources.

But after wait succeed, resources may not necessarily be used, because while there are two or more threads are waiting for the current resource, the wait to return, resources may have been used, in this case

while(resource == FALSE)
    pthread_cond_wait(&cond,&mutex);

If, after only a consumer, you can use if.

Pthread_cond_wait decomposition operation the following steps:

  1. Thread on the wait queue, unlock
  2. After waiting pthread_cond_signal to compete lock signal or pthread_cond_broadcast
  3. If the mutex is locked into competition

 

Possible multiple threads waiting for this resource is available, and the signal to be sent after a resource is available, but there are A, B two threads are waiting, B faster, obtain a mutex, then the lock, consumption of resources, then unlock, after obtaining a mutex, but go back and find the resources it has been used, it will have two options, a loss of access to resources that do not exist, and the other is to continue to wait, then wait for the conditions to go is to use a while, after use if else if pthread_cond_wait return, the order will be implemented.

 

Waiting thread:

To lock the front pthread_cond_wait

Internal pthread_cond_wait will unlock, and then wait for the other thread condition variable is activated

pthread_cond_wait is activated will then automatically lock

 

Active threads

Lock (and waiting thread with the same lock)

pthread_cond_signal transmission signal (signal before the final step to determine whether the waiting threads)

Unlock

The above three active threads operating on internal pthread_cond_wait function of the time to wait before running thread.

/***
pthread_if.c
***/
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

int count = 0;

void *decrement(void *arg)
{
    printf("in derment\n");
    pthread_mutex_lock(&mutex);
    if(count == 0)
        pthread_cond_wait(&cond,&mutex);
    count--;
    printf("----decrement:%d\n",count);
    printf("out decrement\n");
    pthread_mutex_unlock(&mutex);
    return NULL;
}

void *increment(void *arg)
{
    printf("in increment\n");
    pthread_mutex_lock(&mutex);
    count++;
    printf("-----increment:%d\n",count);
    if(count != 0)
    {
        pthread_cond_signal(&cond);
    }
    printf("out increment\n");
    pthread_mutex_unlock(&mutex);
    return NULL;
}

int main()
{
    pthread_t tid_in,tid_de;
    pthread_create(&tid_de,NULL,(void*)decrement,NULL);
    sleep(1);
    pthread_create(&tid_in,NULL,(void*)increment,NULL);
    sleep(1);

    pthread_join(tid_de,NULL);
    pthread_join(tid_in,NULL);
    pthread_mutex_destroy(&mutex);
    pthread_cond_destroy(&cond);
    return 0;
}

 

 

/***
pthread_while.c
***/
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<unistd.h>

typedef struct node_s
{
    int data;
    struct node_s *next;
}node_t;

node_t *head = NULL;

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

void cleanup_handler(void *arg)
{
    printf("cleanup_handler is running.\n");
    free(arg);
    pthread_mutex_unlock(&mutex);
}

void *thread_func(void *arg)
{
    node_t *p = NULL;
    pthread_cleanup_push(cleanup_handler,p);
    while(1)
    {
        pthread_mutex_lock(&mutex);
        while(NULL == head)
            pthread_cond_wait(&cond,&mutex);
        p = head;
        head = head->next;
        printf("process %d node\n",p->data);
        free(p);
        pthread_mutex_unlock(&mutex);
    }
    pthread_cleanup_pop(0);
    return NULL;
}

int main()
{
    pthread_t tid;
    node_t *temp = NULL;
    int i;
    pthread_create(&tid,NULL,(void*)thread_func,NULL);

    for(i = 0; i < 10; i++)
    {
        temp = (node_t*)malloc(sizeof(node_t));
        temp->data = i;
        pthread_mutex_lock(&mutex);
        temp->next = head;
        head = temp;
        pthread_cond_signal(&cond);
        pthread_mutex_unlock(&mutex);
        sleep(1);
    }
    pthread_cancel(tid);
    pthread_join(tid,NULL);
    return 0;

}

 

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/11613064.html