YANE-Framework Tutorial 1.1.0

examples/semaphore/src/semaphoredemo.cpp

00001 /***************************************************************************
00002  *                                                                         *
00003  * Copyright (C) 2011 by www.nonlinearmpc.com                              *
00004  *                                                                         *
00005  * Authors:                                                                *
00006  *  Juergen Pannek <juergen.pannek@googlemail.com>                         *
00007  *  Michael Schamel <michael.schamel@uni-bayreuth.de>                      *
00008  *                                                                         *
00009  ***************************************************************************/
00010 
00011 #include <iostream>
00012 #include <cstdlib>
00013 
00014 #include <yane/utils.h>
00015 
00016 #include "pcthread.h"
00017 
00018 using namespace std;
00019 using namespace yane::Utils;
00020 
00021 int main ( int argc, char *argv [ ] )
00022 {
00023         ProduceConsumeThread * produce_thread, *consume_thread;
00024 
00025         // Create DebugMaster
00026         RTClock *clock = new RTClock ( );
00027         DebugMasterConsole *dm = new DebugMasterConsole ( clock, 90 );
00028         DebugClient *debug_client = dm->newClient ( "Main" );
00029         dm->setLevel ( 6 );
00030 
00031         Semaphore * semaphore = new Semaphore ( 0 );
00032 
00033         // Create threads
00034         produce_thread = new ProduceConsumeThread ( "Producethread", dm, semaphore,
00035                 true );
00036         consume_thread = new ProduceConsumeThread ( "Consumethread", dm, semaphore,
00037                 false );
00038 
00039         // Start threads
00040         produce_thread->start ( );
00041         consume_thread->start ( );
00042 
00043         // Wait until threads are finished
00044         produce_thread->wait ( );
00045 
00046         // stop consumer manually and give 10 ressources to trigger loop exit
00047         consume_thread->stop ( );
00048         semaphore->release ( 10 );
00049         consume_thread->wait ( );
00050 
00051         // Result
00052         debug_client->sendDebugInfo ( "Threads finished.", "", 0, 0, 0, 3 );
00053 
00054         // Free
00055         delete produce_thread;
00056         delete consume_thread;
00057 
00058         delete semaphore;
00059 }