Click here to Skip to main content
15,665,319 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I design one sample multithreaded application in which i read one file in one thread using circular buffer with checking isfull or not also write same buffer to output file with checking buffer isEmpty or not.So problem is that first thread complete it's execution first so that second thread gets remaining data in buffer,so output is wrong.Below i paste code of mine application,please check it & suggest any solution for shared buffer accessing.
 /* Circular Queues */

#include <iostream>
using namespace std;

const int chunk = 512;		//buffer read data 
const int MAX = 2*chunk ;	//queue size
unsigned int Size ;
FILE *fpOut;
FILE *fp=fopen("Test_1K.txt","rb");

//class queue 
class cqueue
	public :
		static int front,rear;
		static char *a;
		 a=new char[MAX];
		delete[] a;

int cqueue::front;
int cqueue::rear;
char* cqueue::a;

	int i;

	Size=ftell(fp);				//Read file size
	cout<<"\nsize is"<<Size;

 	for(i=0;i<Size;i+=chunk)	//read data in chunk as buffer half size 
		while((cqueue::rear==MAX-1)); //wait until buffer is full?
			   fread(cqueue::a,1,chunk,fp);	//read data from in buffer
 			    cqueue::rear+=chunk;		//increment rear pointer of queue to indicate buffer is filled up with data
					cqueue::front=0;		//update front pointer value to read data from buffer in Thread2
	cout<<"\nQueue write completed\n";
    return 0;
	for(int j=0;j<Size;j+=chunk)
		while((cqueue::front==-1)); //wait until buffer is empty
		cqueue::front+=chunk;		//update front pointer after read data from queue
		fwrite(cqueue::a,1,chunk,fpOut);	//write data file
		  cqueue::front=0;		//update queue front pointer when it reads upto queue Max size
	cout<<"\nQueue read completed\n";
	return 0;

void startThreads()
    DWORD  threadIDs[2];
	HANDLE threads[2];	
    threads[0] = CreateThread(NULL,0,Thread1,NULL,0,&threadIDs[0]);
    threads[1] = CreateThread(NULL,0,Thread2,NULL,0,&threadIDs[1]);	
    if(threads[0] && threads[1])
        printf("Threads Created.(IDs %d and %d)",threadIDs[0],threadIDs[1]);

void main()
	 cqueue c1;

Shared memory is a better solution, because you avoid disk access and its problems.

try it out:[^]

PS: writing a file in the directory of the app is always a bad idea
Share this answer
You describe classic Producer-Consumer problem.


Regarding queue that you implemented - much better solution is to encapsulate queue operations in the queue class, and make internal queue data private.

Another option - just use < queue > STL.
Share this answer
In addition to what has been said in other solutions, you need to use some synchronisation mechanism (lock, semaphore, mutex) or concurrent containers or some lock-free container.

In particular, the line
while((cqueue::front==-1)); //wait until buffer is empty
is really bad because it cause busy waiting which is bad for general performance or battery duration.

Given that it seems that you know nothing about threading, it would be a good idea for you to read some tutorial on this... otherwise your program won't works correctly.

MSDN documentation have a lot of information about that. The following page give you an overview of available functions at the Win32 API level:[^]

You can use a semaphore for you problem (circular buffer) or some lock and a bit more logic.

In any case, you want to use a wait function ([^]) as an example so that the CPU will be free for use by other programs until some data is ready.

Also in your cqueue class, none of the member should be static as otherwise you cannot use that class more than once. And there are many other problem with your code. It might be a good idea to read some books to help you make more robust programs.
Share this answer
See @Joseph-M-Newcomer article:Using Semaphores: Multithreaded Producer/Consumer[^].
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900