I have a main loop that parses winsock input. Once the parsed input has been received, it creates a thread and passes an array element id to a global array of names to the thread.

The thread then downloads the requested file, but first checks to see if it already exists. If it exists it exits the download. Now, this is crashing because it is unstable so I have decided to rewrite it.

What would be the best and most efficient method to do?

The program receives a msg every few seconds or so, and it eventually crashes due to a buffer overflow. Since I am planning on writing a good copy for my application, what should I do?

I assume that the maximum vector elements of arrays with 20 length would be around 20~30

1. Create a vector struct with a character array of 20 each and add the download names to this vector if it does not already exist, and then download.

2. Check to see if the file already exists, if it doesn't download it.

3. Other ?

So basically, should I loop through the vector to see if it already exists and then download it, or just check to see if the file exists (it will be called every 2~5 seconds or so) and then download if it doesnt?

I am using GetFileAttributesA to see if the file exists and URLDownloadToFileA (threaded) to download it if it doesn't.

PS: It basically is a chat site client, that would have to download users pics. So it would only really have to download 5~10 pictures, but it would be running for several hours. What should I do?

This is the typical producer-consumer scenario.

The most efficient way would be to use a wait-free or lock-free fifo or ringbuffer.

You could use one of

Boost Lockfree http://tim.klingt.org/boost_lockfree/
(Recently voted into Boost, but not yet part of the distribution. Download from http://tim.klingt.org/boost_lockfree.tar.gz

Herb Sutter's implementation presented in DDJ

Microsoft's (VC++10) Concurrency::concurrent_queue<> in <concurrent_queue.h>
{Windows/Intel/Microsoft specific).

This article has been dead for over six months. Start a new discussion instead.