Home > OS >  Create an dynamic array of buffers?
Create an dynamic array of buffers?

Time:03-10

I'm trying to create an array of buffers.

I have a loop that does:

for(int i = 0; i < threadCount; i  ){
    read(textFile, buffer, spacer);
    pthread_create(&myThread, NULL, processData, (void *)buffer);
}

The issue is that while the thread is running, the next iteration of the loop changes the buffer I'm assuming. And that makes the second loop not run correctly. Would I need to create an array? As in

char *array[threadCount]

And each index of that array is a buffer?

I'm not sure how I would allocate memory for that. If there's another obvious solution I'm missing, that would be great.

CodePudding user response:

Given you're using dynamic allocation, the correct solution would typically be to change:

char *buffer = malloc(spacer);
// Check for allocation error here
for(int i = 0; i < threadCount; i  ){
    read(textFile, buffer, spacer);
    pthread_create(&myThread, NULL, processData, (void *)buffer);
}
// Wait for threads and maybe do something with buffer here
free(buffer);

to:

char **buffers = malloc(threadCount * sizeof(*buffers));
// Check for allocation error here
// Alternative if relying on VLA support is okay, or threadCount is a program constant
// char *buffers[threadCount];
// saves an allocation and for non-absurd thread counts shouldn't be too hard on the stack

for(int i = 0; i < threadCount; i  ){
    buffers[i] = malloc(spacer);
    // Check for allocation error here
}
for(int i = 0; i < threadCount; i  ){
    read(textFile, buffers[i], spacer);
    pthread_create(&myThread, NULL, processData, (void *)buffers[i]);
}
// Wait for threads and maybe do something with buffers here
for(int i = 0; i < threadCount; i  ){
    free(buffers[i]);
}
free(buffers); // Omit if buffers itself was not malloc-ed above

If the main thread doesn't need to use the contents of the buffers after the threads are done processing, you can have the threads free their own buffer when they're done with it and save the work in the main thread (the main thread would only be responsible for freeing the top level buffers array).

CodePudding user response:

You are passing the same pointer to every thread. And you're repeatedly overwriting the memory to which that pointer points.

You have a similar problem with myThread. You place the thread id of every thread into the same variable.

You'll need an array of thread ids. For the buffer, you could create an array (as you mentioned) and pass the index. Or you could replace

pthread_t myThread;
char buffer[spacer];
for(int i = 0; i < threadCount; i  ){
   read(textFile, buffer, spacer);
   pthread_create(&myThread, NULL, processData, buffer);
}

with something like

pthread_t myThreads[threadCount];
for(int i = 0; i < threadCount; i  ){
   char *buffer = malloc(spacer);
   read(textFile, buffer, spacer);
   pthread_create(myThreads i, NULL, processData, buffer);
}

(The thread is responsible for freeing the buffer.)

(Removed the useless cast.)

  •  Tags:  
  • c
  • Related