首页 > 试题广场 >

编程实现三个线程ABC,并让它们顺次打印ABC

[问答题]
编程实现三个线程ABC,并让它们顺次打印ABC
#include<stdio.h>
#include<sys/types.h>
#include<semaphore.h>
#include<pthread.h>

sem_t sem_id1, sem_id2, sem_id3;

void* func1(void*);    //声明
void* func2(void*);
void* func3(void*);

int main(void) {
    sem_init(&sem_id1, 0, 1);    //活动
    sem_init(&sem_id2, 0, 0);
    sem_init(&sem_id3, 0, 0);

    pthread_t pthread_id1, pthread_id2, pthread_id3;
    pthread_create(&pthread_id1, NULL, func1, NULL);
    pthread_create(&pthread_id2, NULL, func2, NULL);
    pthread_create(&pthread_id3, NULL, func3, NULL);

    pthread_join(phread_id1, NULL);
    pthread_join(phread_id1, NULL);
    pthread_join(phread_id1, NULL);

    return 0;
}

void *func1 (void*) {
    sem_wait(sem_id1);
    printf("A\n");
    sem_post(sem_id2);
}
void *func2 (void*) {
    sem_wait(sem_id2);
    printf("B\n");
    sem_post(sem_id3);
}
void *func3 (void*) {
    sem_wait(sem_id3);
    printf("C\n");
    sem_post(sem_id1);
}

发表于 2015-08-19 20:28:27 回复(1)
#include<stdio.h>
#include<pthread.h>

void *secondFunc(void *);
void *thirdFunc(void *);

void *firstFunc(void *args)
{
pthread_t id2;
pthread_create(&id2,NULL,&secondFunc,NULL);
pthread_join(id2,NULL);

printf("C\n");
}
void *secondFunc(void *args)
{
pthread_t id3;
pthread_create(&id3,NULL,&thirdFunc,NULL);
pthread_join(id3,NULL);
printf("B\n");
}

void *thirdFunc(void *args)
{
printf("A\n");
}

int main()
{
pthread_t id1;
pthread_create(&id1,NULL,&firstFunc,NULL);
pthread_join(id1,NULL);

return 0;
}

发表于 2016-05-19 20:23:53 回复(1)
package ABC;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Main {

	private static final Lock lock=new ReentrantLock();
	private static final Condition isA=lock.newCondition();
	private static final Condition isB=lock.newCondition();
	private static final Condition isC=lock.newCondition();

	static class ThreadA extends Thread {

		@Override
		public void run() {
			while(true){
				lock.lock();
				System.out.println("A");
				isB.signal();
				try {
					isA.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				lock.unlock();
			}
		}

	}

	static class ThreadB extends Thread {

		@Override
		public void run() {
			while(true){
				lock.lock();
				System.out.println("B");
				isC.signal();
				try {
					isB.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				lock.unlock();
			}
		}
	}

	static class ThreadC extends Thread {

		@Override
		public void run() {
			while(true){
				lock.lock();
				System.out.println("C");
				isA.signal();
				try {
					isC.await();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				lock.unlock();
			}
		}
	}

	public static void main(String[] args) throws InterruptedException {
		
		ThreadA a=new ThreadA();
		ThreadB b=new ThreadB();
		ThreadC c=new ThreadC();
		
		a.join();
		b.join();
		c.join();
		
		a.start();
		b.start();
		c.start();
		
	}
}


发表于 2016-03-16 12:20:46 回复(0)
lockb=threading.Lock()
lockc=threading.Lock()

def printa():
    print 'A'
    lockb.release()

def printb():
    lockb.acquire()
    print 'B'
    lockc.release()
    lockb.release()

def printc():
    lockc.acquire()
    print 'C'
    lockc.release()


def run():
    lockb.acquire()
    lockc.acquire()
    tc = threading.Thread(target=printc)
    tc.start()
    tb = threading.Thread(target=printb)
    tb.start()
    ta = threading.Thread(target=printa)
    ta.start()

if __name__ == '__main__':
    run()

发表于 2015-08-10 21:33:12 回复(0)
思路1:互斥量+条件变量  :实质互斥量就是一把锁,条件变量来做到线程函数阻塞,在使用条件变量来通知线程函数即可。
思路2:信号量:信号量和互斥锁(mutex)的区别: 互斥锁只允许一个线程进入临界区,而信号量允许多个线程同时进入临界区( 线程同步是控制线程执行和访问临界区域的方法 信号量不一定是锁定某一个资源,而是流程上的概念,比如:有A,B两个线程,B线程要等A线程完成某一任务以后再进行自己下面的步骤,这个任务并不一定是锁定某一资源,还可以是进行一些计算或者数据处理之类。
编辑于 2015-07-22 13:42:44 回复(0)
创建3个信号量-》创建3个线程-》等待线程执行
发表于 2015-07-13 15:51:07 回复(0)
思路:设置三个信号量:S1, S2, S3,S2由S1 post,S3由S2post, S1由S3 post,由A线程先开 始打印,其他线程必然在等待信号量,所以三个线程一定会按照信号量的顺序来打印
编辑于 2015-05-18 13:16:53 回复(0)