Click here to Skip to main content
15,884,237 members
Articles / Programming Languages / Objective C

Producer/Consumer Implementation Using Thread, Semaphore and Event

Rate me:
Please Sign up or sign in to vote.
4.64/5 (45 votes)
6 Jul 200415 min read 202.6K   3.5K   91  
Using thread, semaphore and event classes to implement a specific version of Producer/Consumer model
#ifndef mySemaphore_H
#define mySemaphore_H

#define OPEN 0
#define CREATE 1

/*
#define _MSC_VER 6000
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
*/
#define _WIN32_WINNT 0x0400

#include <windows.h>
#include <winuser.h>
#include <winbase.h>
#include <string>
using namespace std;

class mySemaphore
{

public:

    mySemaphore(string& sName=string(""),
		        long initialCount=0,long maxCount=1,
				int accessMode=CREATE,BOOL canInherit=TRUE);

    virtual ~mySemaphore();

public:

    void closeSemaphore();    
	BOOL lock(BOOL wait=TRUE);
    void unlock();

public:

	string& getSemaphoreName() { return semaphoreName; }
	long    getInitialCount()  { return initCount; }
    long    getMaxCount()      { return maxCount; }
	BOOL    getInerit()        { return inherit; }
	
private:

	void getErrorDetail(int&,string&);

private:

	long initCount;   // initialCount 
	long maxCount;    // max count
    BOOL inherit;     // can be inherited by child

    string semaphoreName;
    HANDLE semaphore;
    LPSECURITY_ATTRIBUTES sa;
};
#endif

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
United States United States
I love to tell jokes and today, I finally came up with my own joke. here it goes:

I decide to make my first son a medical doctor so later on when I am old and sick, I can get medical care any time I need and for free..., in fact, better to make my second son a medical doctor too so I can get a second opinion.

here is another version of this joke:

I decide to make my first son a medical doctor so later on when I am old and sick, I can get medical care any time I need and for free..., in fact, better to make my second son a medical doctor too so I can get a second opinion. well, perhaps my third son should be a lawyer - in case something is wrong with my medical care, I can sue the first two for free.

if you happen to visit this page and read these two jokes, tell me which one you like...

Comments and Discussions