学习嵌入式的第26天

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

pthread_mutex_t lock;
pthread_mutexattr_t attr;

void* thread_func(void* arg) {
    pthread_mutex_lock(&lock);
    printf("Thread %ld: First lock acquired\n", (long)arg);

    pthread_mutex_lock(&lock);
    printf("Thread %ld: Second lock acquired (recursive)\n", (long)arg);

    pthread_mutex_unlock(&lock);
    pthread_mutex_unlock(&lock);

    printf("Thread %ld: Locks released\n", (long)arg);
    pthread_exit(NULL);
}

int main() {
    pthread_t threads[2];
    long thread_ids[2] = {1, 2};

    pthread_mutexattr_init(&attr);
    pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
    pthread_mutex_init(&lock, &attr);

    for (int i = 0; i < 2; i++) {
        pthread_create(&threads[i], NULL, thread_func, (void*)&thread_ids[i]);
    }

    for (int i = 0; i < 2; i++) {
        pthread_join(threads[i], NULL);
    }

    pthread_mutex_destroy(&lock);
    pthread_mutexattr_destroy(&attr);

    return 0;
}
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>

pthread_mutex_t lock;

void* thread_func(void* arg) {
    int ret = pthread_mutex_trylock(&lock);
    if (ret == 0) {
        printf("Thread %ld: Lock acquired\n", (long)arg);
        sleep(1); // 模拟长时间操作
        pthread_mutex_unlock(&lock);
        printf("Thread %ld: Lock released\n", (long)arg);
    } else if (ret == EBUSY) {
        printf("Thread %ld: Lock already held\n", (long)arg);
    } else {
        perror("pthread_mutex_trylock");
    }

    pthread_exit(NULL);
}

int main() {
    pthread_t threads[2];
    long thread_ids[2] = {1, 2};

    pthread_mutex_init(&lock, NULL);

    pthread_create(&threads[0], NULL, thread_func, (void*)&thread_ids[0]);
    sleep(1); // 确保第一个线程先运行
    pthread_create(&threads[1], NULL, thread_func, (void*)&thread_ids[1]);

    for (int i = 0; i < 2; i++) {
        pthread_join(threads[i], NULL);
    }

    pthread_mutex_destroy(&lock);

    return 0;
}

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

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;


int a = 0,b = 0;


pthread_cond_t c1;
pthread_cond_t c2;
pthread_mutex_t m;

void* task1(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&m);
        pthread_cond_wait(&c1,&m);
        a -= 3;
        printf("消费完成,苹果有%d个\n",a);
        pthread_mutex_unlock(&m);
        sleep(1);
    }
}


void* task2(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&m);
        pthread_cond_wait(&c2,&m);
        b -= 5;
        printf("消费完成,香蕉有%d个\n",b);
        pthread_mutex_unlock(&m);
        sleep(2);
    }
}



int main(int argc, const char *argv[])
{
    pthread_cond_init(&c1,0);// 初始化条件变量
    pthread_cond_init(&c2,0);
    pthread_mutex_init(&m,0);

    pthread_t id1,id2;
    pthread_create(&id1,0,task1,0);
    pthread_create(&id2,0,task2,0);
    pthread_detach(id1);
    pthread_detach(id2);


    srand(time(NULL));

    while (1)
    {
        a++;
        printf("苹果有%d个\n", a);
        if (a > 2)
            pthread_cond_signal(&c1);
        sleep(1);
        b += 2;
        printf("香蕉有%d个\n", b);
        if (b > 5)
            pthread_cond_signal(&c2);
        sleep(1);
    }
    return 0;
}

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

typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;


int a = 0,b = 0;


pthread_cond_t c1;
pthread_cond_t c2;
pthread_mutex_t m;

void* task1(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&m);
        pthread_cond_wait(&c1,&m);
        a -= 1;
        printf("消费完成,苹果有%d个\n",a);
        pthread_mutex_unlock(&m);
        sleep(1);
    }
}


void* task2(void* arg)
{
    while(1)
    {
        pthread_mutex_lock(&m);
        pthread_cond_wait(&c2,&m);
        b -= 4;
        printf("消费完成,香蕉有%d个\n",b);
        pthread_mutex_unlock(&m);
        sleep(2);
    }
}



int main(int argc, const char *argv[])
{
    pthread_cond_init(&c1,0);// 初始化条件变量
    pthread_cond_init(&c2,0);
    pthread_mutex_init(&m,0);

    pthread_t id1,id2;
    pthread_create(&id1,0,task1,0);
    pthread_create(&id2,0,task2,0);
    pthread_detach(id1);
    pthread_detach(id2);


    srand(time(NULL));
    int fruit_choice = rand() % 2;// 随机选择水果
    while (1)
    {
            
            pthread_mutex_lock(&m);
            if (fruit_choice == 0) 
            {
                    a +=2;
                    printf("苹果有%d个\n",a);
                    if(a>10)
                    break;
                    if(a>2)
                    pthread_cond_signal(&c1);
                    sleep(1);                
            }
            else if (fruit_choice == 1)
            {
                    b += 3;
                    printf("香蕉有%d个\n",b);
                    if(b>20)
                    break;
                    if(b>5)
                    pthread_cond_signal(&c2);
                    sleep(1);
                    
            }
            pthread_mutex_unlock(&m);
            sleep(1);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值