Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9204779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:47:29+00:00 2026-06-17T23:47:29+00:00

I am getting acquainted with pthreads programming; the following code is a simple producer/consumer

  • 0

I am getting acquainted with pthreads programming; the following code is a simple producer/consumer design where data is put/fetched from a global list. The problem is: the data pointer in the consumer function is tried to be freed twice. Beside, if I add a printf() at the beginning of the loop, everything seems ok… What am I doing wrong? I suspect a misuse of the volatile keyword, or something hidden by the cache… Unless it’s just a design issue (which probably is :p).

Thanks for your insights.

Note: malloc()/free() is thread-safe on my system. I am compiling with $ gcc -pthread -O0 which should, I guess, reduce possible design errors due to misuse of volatile. Finally, we don’t care in this code snippet with running out of memory (in case of more producer than consumer).

Edit: Changed code to a single list head.

#include <pthread.h>
#include <stdlib.h>
#include <string.h>

pthread_mutex_t lock;
pthread_cond_t new_data;

struct data {
  int i;
  struct data *next;
};

struct data *list_head = NULL;

void *consume(void *arg)
{
  struct data *data;

  while (1) {
    pthread_mutex_lock(&lock);
    while (list_head == NULL) {
      pthread_cond_wait(&new_data, &lock);
    }
    data = list_head;
    list_head = list_head->next;
    pthread_mutex_unlock(&lock);
    free(data);
  }

  return NULL;
}

void *produce(void *arg)
{
  struct data *data;

  while (1) {
    data = malloc(sizeof(struct data));
    pthread_mutex_lock(&lock);
    data->next = list_head;
    list_head = data;
    pthread_mutex_unlock(&lock);
    pthread_cond_signal(&new_data);
  }

  return NULL;
}

int main()
{
  pthread_t tid[2];
  int i;

  pthread_mutex_init(&lock, NULL);
  pthread_cond_init(&new_data, NULL);
  pthread_create(&tid[0], NULL, consume, NULL);
  pthread_create(&tid[1], NULL, produce, NULL);
  for (i = 0; i < 2; i++) {
    pthread_join(tid[i], NULL);
  }
}

And the output:

$ ./a.out 
*** glibc detected *** ./a.out: double free or corruption (fasttop): 0x00007f5870109000 ***
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:47:30+00:00Added an answer on June 17, 2026 at 11:47 pm

    Consider the following scenario:

    1. produce -> acquired lock
    2. consume -> wait lock
    3. produce -> allocate d0, write_ptr = d0, read_ptr = d0, signal, unlock
    4. consume -> acquired lock
    5. produce -> wait lock
    6. consume -> satisfied condition
    7. consume -> data = d0, read_ptr = NULL, unlock
    8. consume -> free d0
    9. produce -> acquired lock, allocate d1
    10. consume -> wait lock
    11. produce -> write_ptr != null so write_ptr->next = d1
    12. produce -> read_ptr == null so read_ptr = d1

    Check out step 11. write_ptr is still d0 even though it was free’d independently of the producer thread. You need to make sure consume does not invalidate write_ptr.

    A doubly linked list would allow you to avoid some of these difficulties (as the readers and writers work from separate ends).

    Main:

    • Create sentinel nodes HEAD and TAIL, link HEAD and TAIL
    • Spawn producer
    • Spawn consumer

    Producer:

    • Lock
    • Create node
    • Link HEAD->next->prev to node
    • Link node->next to HEAD->next
    • Link HEAD->next to node
    • Link node->prev to HEAD
    • Unlock
    • Signal

    Consumer:

    • Lock
    • Wait for TAIL->prev != HEAD (do { pthread_cond_wait } while (condition);)
    • data = TAIL->prev
    • Link TAIL->prev to data->prev
    • Link TAIL->prev->next to TAIL
    • Unlock
    • Use data
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting this error when I follow the simple code example from jsoncpp, that basically
Getting data from a database table to an object in code has always seemed
Getting following error while connecting to mongodb using Spring Data: java.io.IOException: couldn't connect to
I'm getting acquainted with c++ but don't know how to compare indexed characters from
Getting large data around 200MB using stored procedure from Database. Previously used DataTable.Load() method
I'm just getting acquainted with Vim's CTags functionality - and it's damn handy. One
I'm getting myself acquainted with MVC by making a 'for-fun' site in django. I'm
I'm getting acquanted with Core Animation and trying to implement simple movement along a
I'm just getting acquainted with implementing REST web services in Java using JAX-RS and
Getting some very annoying behaviour from my MVC3 app. So I've got a model

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.