programación linux --- hilo --- bloqueo de lectura-escritura

Bloqueos de lectura-escritura mecanismos de comunicación

bloqueo de escritura en bloqueos de lectura y escritura, las siguientes funciones
(1) Si un subproceso se aplica para un bloqueo de lectura, otros subprocesos pueden solicitar bloqueos de lectura, pero no pueden solicitar un bloqueo de escritura.
(2) Si un hilo solicita un bloqueo de escritura, otros hilos no pueden solicitar un bloqueo de lectura o escritura.

La inicialización de bloqueo de escritura
int a pthread_rwlock_init (pthread_rwlock_t el restringen rwlock se adquiere *,
            const pthread_rwlockattr_t el restringen * attr);
            
destrucción bloqueo de escritura
int pthread_rwlock_destroy (pthread_rwlock_t * rwlock);

la aplicación de un bloqueo de lectura
int la pthread_rwlock_rdlock (pthread_rwlock_t * rwlock se adquiere);
int pthread_rwlock_tryrdlock (pthread_rwlock_t * rwlock se adquiere);

Solicitar bloqueo de escritura
int pthread_rwlock_trywrlock (pthread_rwlock_t * rwlock);
int pthread_rwlock_wrlock (pthread_rwlock_t * rwlock); ejemplo de

desbloqueo de programa
int pthrwrlock

:
程序 实现 4 个 线程 , 两个 读 线程 , 两个 写 线程。

#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <pthread.h>
#include <stdlib.h >
#include <string.h>
#include <pthread.h>

static pthread_rwlock_t rwlock;
#define BUFSIZE 1024
char gdata [BUFSIZE];
int gtime;

void * thread_fun_read1 (void * arg);
void * thread_fun_read2 (void * arg);
void * thread_fun_write1 (void * arg);
void * thread_fun_write2 (void * arg);


int main (int argc, char * argv [])
{
    int res;
    pthread_t th1, th2, th3, th4;
    void * thret;
    
    res = pthread_rwlock_init (& rwlock, NULL);
    if (res! = 0)
    {
        perror ("falló la inicialización de rwlock");
        salir (-1);
    }
    
    res = pthread_create (& th1, NULL, thread_fun_read1, NULL);
    if (res! = 0)
    {
        perror ("error al crear el hilo");
        salir (-1);
    }
    
    res = pthread_create (& th2, NULL, thread_fun_read2, NULL);
    if (res! = 0)
    {
        perror ("error al crear el hilo");
        salir (-1);
    }

    res = pthread_create (& th3, NULL, thread_fun_write1, NULL);
    si (res!

        perror ("error al crear el hilo");
        salir (-1);
    }

    res = pthread_create (& th4, NULL, thread_fun_write2, NULL);
    if (res! = 0)
    {
        perror ("error al crear el hilo");
        salir (-1);
    }

    res = pthread_join (th1, & thret);
    if (res! = 0)
    {
        perror ("error en la unión del hilo");
        salir (-1);
    }

    res = pthread_join (th2, & thret);
    if (res! = 0)
    {
        perror ("error en la unión del hilo");
        salir (-1);
    }

    res = pthread_join (th3, & thret);
    if (res! = 0)
    {
        perror ("error en la unión del hilo");
        salir (-1);
    }

    res = pthread_join (th4, & thret);
    if (res! = 0)
    {
        perror ("error en la unión del hilo");
        salir (-1);
    }
    
    pthread_rwlock_destroy (& rwlock);
    salir (0);        
}

void * thread_fun_read1 (void * arg)
{
    printf ("hilo leído un intento de bloqueo \ n");
    pthread_rwlock_rdlock (& ​​rwlock);
    while (strncmp ("end", gdata, 3)! = 0)
    {
        printf ("este es el hilo leído uno.% s \ n", gdata);
        pthread_rwlock_unlock (& ​​rwlock);
        dormir (2);
        pthread_rwlock_rdlock (& ​​rwlock);
        while (gdata [0] == '\ 0')
        {
            pthread_rwlock_unlock (& ​​rwlock);
            dormir (2);
            pthread_rwlock_rdlock (& ​​rwlock);
        }
    }
    pthread_rwlock_unlock (& ​​rwlock);
    gtime = 1;
    pthread_exit (0);    
}

void * thread_fun_read2 (void * arg)
{
    printf ("hilo leído dos intenta obtener el bloqueo \ n");
    pthread_rwlock_rdlock (& ​​rwlock);
    while (strncmp ("end", gdata, 3)! = 0)
    {
        printf ("este es un hilo leído dos.% s \ n", gdata);
        pthread_rwlock_unlock (& ​​rwlock);
        dormir (5);
        pthread_rwlock_rdlock (& ​​rwlock);
        while (gdata [0] == '\ 0')
        {
            pthread_rwlock_unlock (& ​​rwlock);
            dormir (5);
            pthread_rwlock_rdlock (& ​​rwlock);
        }
    }
    pthread_rwlock_unlock (& ​​rwlock);
    gtime = 1;
    pthread_exit (0);    
}

void * thread_fun_write1 (void * arg)
{
    printf ("este es un hilo de escritura un intento de bloquearlo \ n");
    while (! gtime)
    {
        pthread_rwlock_wrlock (& ​​rwlock);
        printf ("esto es escribir el hilo uno. \ n ingresa algo de texto, ingresa 'end' para terminar \ n");
        fgets (gdata, BUFSIZE, stdin);
        pthread_rwlock_unlock (& ​​rwlock);
        dormir (15);
    }
    pthread_rwlock_unlock (& ​​rwlock);
    pthread_exit (0);
}

void * thread_fun_write2 (void * arg)
{
    printf ("este es el hilo de escritura dos, intenta obtener el bloqueo \ n");
    while (! gtime)
    {
        pthread_rwlock_wrlock (& ​​rwlock);
        printf ("este es el hilo de escritura dos. \ n ingresa algo de texto, ingresa 'end' para terminar \ n");
        fgets (gdata, BUFSIZE,
        pthread_rwlock_unlock (& ​​rwlock);
        dormir (15);
    }
    pthread_rwlock_unlock (& ​​rwlock);
    pthread_exit (0);
}

Supongo que te gusta

Origin blog.csdn.net/yinhua405/article/details/77506966
Recomendado
Clasificación