Скачиваний:
20
Добавлен:
01.05.2014
Размер:
1.21 Кб
Скачать
#ifndef GLOB_DAT_H
#define GLOB_DAT_H

#include <thread.h>
#include <iostream.h>
#include <stdio.h>

class glob_dat 
{
	private:
		int 		val;
		mutex_t 	lockx;
	public:
		// constructor function 
		glob_dat( int a ) 
		{
			val = a;
			if (mutex_init(&lockx, USYNC_THREAD,0)) 						
				perror("mutex_init");
		};


		// destructor function
		~glob_dat() { if (mutex_destroy(&lockx)) perror("mutex_destroy"); };


		// set new value
		glob_dat& operator=( int new_val ) 
		{
			if (!mutex_lock(&lockx)) {
				val = new_val;
				if (mutex_unlock(&lockx)) perror("mutex_unlock");
			}else perror("mutex_lock");

			return *this;

		};

		// retrieve value
		int getval( int* valp ) 
		{
			if (!mutex_lock(&lockx)) {
				*valp= val;
				if (!mutex_unlock(&lockx)) return 0;
				//perror("mutex_unlock");
			} else perror("mutex_lock");

			return -1;
		};

		// show value to an output stream
		friend ostream& operator<<( ostream& os, glob_dat& gx) 
		{
			if (!mutex_lock(&gx.lockx)) {	
				os << gx.val;
				if (mutex_unlock(&gx.lockx)) perror("mutex_lock");

			} else	 perror("mutex_lock");

			return os;

		};

};			/* glob_dat */

#endif
Соседние файлы в папке glob_dat