Question related to pointer

S

Sanjay

Hi all,

I was trying to read data from a file to a buffer1 and copy 'n' bytes
of data from the buffer into buffer2 in small amount. The data filling
into buffer 1 is done by spinning a thread. ex- if the buffer1 has
1024 bytes of data and still filling up the data into buffer1, and now
i want to copy the 128 bytes of data from buffer1 to buffer2 by
calling a function, how can i do it?

i'm currrently doing like this, but the copy never happens.
int thread()
{
while(nBytesRead)
{
success = ReadFile(hRead,pinBufRead, SAMPLE_READ_SIZE, &nBytesRead,
NULL);
}
return 0;
}

int copy(int size)
{
char *pbufvoid;
poutBufvoid=malloc(4096);
char *temp= &pinBufRead[0];
memset(poutBufvoid,'\0',4096);
memcpy(poutBufvoid, temp+count, size);
return 0;
}

and when i print the contents of the poutBufvoid, it is always
printing blank. Can anyone tell me how i can resolve this issue?
 
T

Thomas Matthews

Sanjay said:
Hi all,

I was trying to read data from a file to a buffer1 and copy 'n' bytes
of data from the buffer into buffer2 in small amount. The data filling
into buffer 1 is done by spinning a thread. ex- if the buffer1 has
1024 bytes of data and still filling up the data into buffer1, and now
i want to copy the 128 bytes of data from buffer1 to buffer2 by
calling a function, how can i do it?

i'm currrently doing like this, but the copy never happens.
int thread()
{
while(nBytesRead)
{
success = ReadFile(hRead,pinBufRead, SAMPLE_READ_SIZE, &nBytesRead,
NULL);
}
return 0;
}

int copy(int size)
{
char *pbufvoid;
poutBufvoid=malloc(4096);
char *temp= &pinBufRead[0];
memset(poutBufvoid,'\0',4096);
memcpy(poutBufvoid, temp+count, size);
return 0;
}

and when i print the contents of the poutBufvoid, it is always
printing blank. Can anyone tell me how i can resolve this issue?
First, let me start by saying the the C++ language does not support
multi-threading. Thus your question is off topic.

You will need some platform specific method to protect the pointers
from being updated by the same task.

I suggest that the thread that performs writing into the buffer,
set some kind of flag or semaphore to indicate that there is
data in the buffer.

The reading thread will pull data out of the buffer and may also
change the flag or semaphore to indicate that the buffer is
empty.

Study circular queues or ring buffers.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
J

Jonathan Lee

Hello,

I was trying to read data from a file to a buffer1 and copy 'n' bytes
of data from the buffer into buffer2 in small amount. The data filling
into buffer 1 is done by spinning a thread. ex- if the buffer1 has
1024 bytes of data and still filling up the data into buffer1, and now
i want to copy the 128 bytes of data from buffer1 to buffer2 by
calling a function, how can i do it?

The method here:

http://msmvps.com/blogs/vandooren/a...-consumer-queue-in-c-without-using-locks.aspx

may be useful to you. It sounds like you have a situation of one
consumer and one producer thread. In this case you can (apparently) do
this in "pure" C++. That's what the article claims. I ran across it a
couple weeks ago, but didn't read it closely. Also, you'd probably get
better answers in comp.programming.threads, but I think that's already
been mentioned.

--Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top