线程顺序执行(phtread)

3 篇文章 0 订阅

编写一个程序,开启3个线程,这3个线程的ID分别为ABC,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC.依次递推。


程序代码如下:

#include<iostream>
#include<cstdlib>
using namespace std;


#ifdef __cplusplus
extern "C"
{
#endif


#include<pthread.h>
#include<semaphore.h>
#ifdef __cplusplus
}
#endif


sem_t sem1,sem2,sem3;
int var = 1;
void *PrintA(void *ptr)
{
    int i;
    for(i = 0; i < 10; i++)
    {
         sem_wait(&sem1);
         cout<<"A";
         sem_post(&sem2);
   } 
   return NULL;
}
void *PrintB(void *ptr)
{
    for(int i = 0; i < 10; i++)
    {
         
         sem_wait(&sem2);
         cout<<"B";
         sem_post(&sem3);
    }
    return NULL;
}
void *PrintC(void *ptr)
{
    for(int i = 0; i < 10; i++)
    {
         sem_wait(&sem3);
         cout<<"C";
         sem_post(&sem1);
    }
    return NULL;
}




void init_thread()
{
   int rc1,rc2,rc3;
   pthread_t thread1,thread2,thread3;
   sem_init(&sem1,0,0);
   sem_init(&sem2,0,0);
   sem_init(&sem3,0,0);
   
   if( (rc1 = pthread_create(&thread1,NULL,PrintA,NULL) ))
   {
       cout<<"create thread is failed"<<endl;
       exit(1);
   }
   
   if( (rc2 = pthread_create(&thread2,NULL,PrintB,NULL) ))
   {
       cout<<"create thread is failed"<<endl;
       exit(1);
   }
   
   if( (rc3 = pthread_create(&thread3,NULL,PrintC,NULL) ))
   {
       cout<<"create thread is failed"<<endl;
       exit(1);
   }
   sem_post(&sem1);
   pthread_join(thread1,NULL);
   pthread_join(thread2,NULL);
   pthread_join(thread3,NULL);
}
int main(int argc,char *argv[])
{
   init_thread();
   cout<<endl;   
   return 0;
}


CountDownLatch是Java中的一个同步工具类,它用于控制多个线程之间的执行顺序。CountDownLatch可以在一个或多个线程等待其他线程完成之前进行等待,直到所有线程都完成它才继续执行。 具体地说,CountDownLatch的使用场景一般是这样的:一个线程或多个线程需要等待其他线程完成某些操作之后才能执行,而其他线程完成操作之后需要通知等待的线程继续执行。这时就可以使用CountDownLatch来实现线程顺序执行。 具体实现方式如下: 1. 创建一个CountDownLatch对象,设置计数器的初始值为等待的线程数。 2. 在等待的线程中调用CountDownLatch的await()方法进行等待,直到计数器值为0。 3. 在其他线程完成操作之后,调用CountDownLatch的countDown()方法将计数器的值减1,表示有一个线程完成操作。 4. 当计数器的值为0时,等待的线程就可以继续执行。 示例代码如下: ``` import java.util.concurrent.CountDownLatch; public class CountdownLatchExample { public static void main(String[] args) throws InterruptedException { CountDownLatch latch = new CountDownLatch(3); Thread t1 = new Thread(() -> { System.out.println("Thread 1 is running"); latch.countDown(); }); Thread t2 = new Thread(() -> { System.out.println("Thread 2 is running"); latch.countDown(); }); Thread t3 = new Thread(() -> { System.out.println("Thread 3 is running"); latch.countDown(); }); t1.start(); t2.start(); t3.start(); latch.await(); System.out.println("All threads have finished running"); } } ``` 在上述代码中,创建了一个CountDownLatch对象,计数器的初始值为3,表示需要等待3个线程完成操作。然后创建3个线程,每个线程完成操作之后都会调用countDown()方法将计数器的值减1。最后在主线程中调用await()方法进行等待,直到计数器的值为0,表示所有线程都已经完成操作。当计数器的值为0时,主线程就可以继续执行
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值