两个线程实现同步代码

#include<myhead.h>

//1、定义条件变量
pthread_cond_t cond;

//11、定义互斥锁
pthread_mutex_t mutex; 
//定义生产者线程
void *task1(void *arg)
{

    sleep(1);
    printf("%#lx:生产了四辆小鹏汽车\n", pthread_self());

    //3、唤醒等待队列中的所有线程
    pthread_cond_broadcast(&cond);

    //退出线程
    pthread_exit(NULL);
}

//定义消费者线程
void *task2(void *arg)
{

    //sleep(1);

    //33、获取锁资源
    pthread_mutex_lock(&mutex);

    //4、等待生产者线程的资源    
    pthread_cond_wait(&cond, &mutex);


    printf("%#lx:购买了一辆小鹏汽车\n", pthread_self());

    //44、释放锁资源
    pthread_mutex_unlock(&mutex);


    //退出线程
    pthread_exit(NULL);
}

int main(int argc, const char *argv[])
{
    //定义两个线程号
    pthread_t tid1, tid2, tid3, tid4, tid5;


    //2、初始化条件变量
    pthread_cond_init(&cond, NULL);
    //22、初始化互斥锁
    pthread_mutex_init(&mutex, NULL);

    //创建生产者线程
    if(pthread_create(&tid1, NULL, task1, NULL) != 0)
    {
        printf("tid1 create error\n");
        return -1;
    }

    //创建消费者线程
    if(pthread_create(&tid2, NULL, task2, NULL) != 0)
    {
        printf("tid2 create error\n");
        return -1;
    }
    if(pthread_create(&tid3, NULL, task2, NULL) != 0)
    {
        printf("tid3 create error\n");
        return -1;
    }

    if(pthread_create(&tid4, NULL, task2, NULL) != 0)
    {
        printf("tid4 create error\n");
        return -1;
    }
    if(pthread_create(&tid5, NULL, task2, NULL) != 0)
    {
        printf("tid5 create error\n");
        return -1;
    }

    //回收线程资源
    pthread_join(tid1 , NULL);
    pthread_join(tid2 , NULL);

    //5、销毁条件变量
    pthread_cond_destroy(&cond);

    //55、销毁互斥锁
    pthread_mutex_destroy(&mutex);


    return 0;
}

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用互斥量实现两个线程同步的示例代码: ``` #include <windows.h> #include <stdio.h> HANDLE hMutex; DWORD WINAPI ThreadFunc1(LPVOID lpParam) { while (1) { // 等待互斥量 WaitForSingleObject(hMutex, INFINITE); // 访问共享资源 printf("Thread 1 is running.\n"); // 释放互斥量 ReleaseMutex(hMutex); // 等待一段时间 Sleep(1000); } return 0; } DWORD WINAPI ThreadFunc2(LPVOID lpParam) { while (1) { // 等待互斥量 WaitForSingleObject(hMutex, INFINITE); // 访问共享资源 printf("Thread 2 is running.\n"); // 释放互斥量 ReleaseMutex(hMutex); // 等待一段时间 Sleep(1000); } return 0; } int main() { HANDLE hThread1, hThread2; // 创建互斥量 hMutex = CreateMutex(NULL, FALSE, NULL); if (hMutex == NULL) { // 创建互斥量失败 return 1; } // 创建线程1 hThread1 = CreateThread(NULL, 0, ThreadFunc1, NULL, 0, NULL); if (hThread1 == NULL) { // 创建线程1失败 CloseHandle(hMutex); return 1; } // 创建线程2 hThread2 = CreateThread(NULL, 0, ThreadFunc2, NULL, 0, NULL); if (hThread2 == NULL) { // 创建线程2失败 CloseHandle(hMutex); CloseHandle(hThread1); return 1; } // 等待线程1结束 WaitForSingleObject(hThread1, INFINITE); // 等待线程2结束 WaitForSingleObject(hThread2, INFINITE); // 关闭句柄 CloseHandle(hMutex); CloseHandle(hThread1); CloseHandle(hThread2); return 0; } ``` 在示例中,使用 CreateMutex() 函数创建了一个未命名的互斥量,并返回一个句柄。然后创建了两个线程,分别执行 ThreadFunc1() 和 ThreadFunc2() 函数。这两个函数都会访问共享资源,并使用互斥量进行同步。最后,等待两个线程执行完毕,关闭互斥量和线程句柄。 需要注意的是,这里使用的是未命名的互斥量,如果需要在不同进程间同步,则需要使用命名互斥量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值