#Multi Thread Signal Handling

1 messages · Page 1 of 1 (latest)

last crow
#

I'm encountering an issue with my program where the producer thread gets stuck at the condition variable part after I send a SIGINT signal to the program. The consumer threads are quitting as expected, but the producer thread remains stuck.


__sig_atomic_t sig_flag = 0;

void signalHandler(int signo) { sig_flag = signo; }

void *consumer(void *args) {
  while (!sig_flag) {
    pthread_mutex_lock(&buffer_mutex);

    while (buffer_count == 0 && !done) {
      pthread_cond_wait(&buffer_empty, &buffer_mutex);
    }

    if (buffer_count == 0 && done) {
      pthread_mutex_unlock(&buffer_mutex);
      break;
    }

    // Consume from buffer

    pthread_cond_signal(&buffer_full);
    pthread_mutex_unlock(&buffer_mutex);

   // use consumed data here
  pthread_exit(NULL);
}

void processDirectory(FileData parent_data) {
  DIR* src_dir = opendir(parent_data.src_path);
  struct dirent* entry;

  while (!sig_flag && (entry = readdir(src_dir)) != NULL) {
    if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
      continue;

    if (entry->d_type == DT_REG || entry->d_type == DT_FIFO) {
      pthread_mutex_lock(&buffer_mutex);

      while (!sig_flag && buffer_count == buffer_size) {
        pthread_cond_wait(&buffer_empty, &buffer_mutex);
      }

      // Add to buffer
      pthread_cond_signal(&buffer_full);
      pthread_mutex_unlock(&buffer_mutex);
    }
    else if (entry->d_type == DT_DIR) {
      // Directory processing logic

      // Create a new FileData structure for the subdirectory
      FileData dir_data;

      // Set the source and destination paths for the subdirectory
      // Call processDirectory recursively for the subdirectory
      processDirectory(dir_data);
    }
  }

  closedir(src_dir);
}

int main() {
  // Set signal handler using sigaction
  // Create one producer and N consumer
  // Join threads
}
hollow voidBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

digital leaf
#

If you are waiting on them you need to make them send a signal before quitting