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 8747367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:20:42+00:00 2026-06-13T12:20:42+00:00

I am trying to do a consumer / producer program with multiple consumers and

  • 0

I am trying to do a consumer / producer program with multiple consumers and producers. Im just trying to do something simple as have a producer add a random number to a shared array and have a consumer take it out.

I am getting errors though with the thread create. I see the error and what its saying but not sure how to fix it.

I will show you the producer function and main and i will separate them for easy reading.

#define BUFFER_SIZE 30

struct sharedBuffer{

int resource[BUFFER_SIZE];
int produced, consumed;
int in;
int out;

};

struct sharedBuffer shared;
struct sharedBuffer s_instance = { in:0, out:0, };

void *producer(void *arg){

int item = 0;
int itemCount =0;
//shared.in =0;
  while (1) {
    item = produceItem();           //generate the random number
   // while (shared.produced == BUFFER_SIZE);   //spin if buffer is full

    if(shared.produced == BUFFER_SIZE){
        fprintf (stdout, "Producer added : %d items \n",  shared.produced);
       return NULL;

    }
    /* put new item in buffer */
    shared.resource[shared.in] = item;
    shared.in = (shared.in+1) % BUFFER_SIZE;
    shared.resource[shared.in] = item;
    fprintf (stdout, "Producer added: %d \n",  item);
    shared.produced++;
    //item++;
    itemCount++;
}
}

Here is main()

int main(int argc, char* argv[])
{

int i;
int result;
int num_producers;
int num_consumers;

pthread_attr_t attrs;
pthread_attr_init (&attrs);
//int producerArray[num_producers],consumerArray[num_consumers];
pthread_t producer[num_producers],consumer[num_consumers];

printf("Enter the number of Producers: \n");

scanf("%d", &num_producers);

printf("Enter the number of Consumers: \n");

scanf("%d", &num_consumers);  

for(i=0; i< num_producers; i++)
{
    //producerArray[i]=0;
    //pthread_create(&producer[i],NULL, producer,&producerArray[i]);
    pthread_create(&producer[i],NULL, producer,NULL);
}

for(i=0; i< num_consumers; i++)
{
    //consumerArray[i]=0;
    // pthread_create(&tid[i], NULL, &compute_prime, NULL);
    //pthread_create(&consumer[i],NULL, consumer, &consumerArray[i]);
    pthread_create(&consumer[i], NULL, consumer, NULL);
}

for(i=0;i<num_producers;i++)
{
    pthread_join(producer[i],NULL);
    //printf("\nThe Producer (%d) produced: [%d] Items",i,producerArray[i]);
    sleep(1);
}

return 0;
}

Here is the error i am getting.

Consumer_producer2.c:125: warning: passing argument 3 of âpthread_createâ from  incompatible pointer type
/usr/include/pthread.h:227: note: expected âvoid * (*)(void *)â but argument is of type âpthread_t *â
Consumer_producer2.c:133: warning: passing argument 3 of âpthread_createâ from incompatible pointer type
/usr/include/pthread.h:227: note: expected âvoid * (*)(void *)â but argument is of type âpthread_t *â

if i run it i get a Segmentation fault
Thank you.

  • 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-13T12:20:43+00:00Added an answer on June 13, 2026 at 12:20 pm

    You’ve defeated yourself with your naming scheme: You have both a free function called producer and a local array called producer. Pick better names that are unique, and you should be able to fix this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a concurrent producer-consumer collection (multiple producers and consumers) that supports
I'm trying to set up a simple producer-consumer system in Gevent but my script
I am trying to solve a consumer/producer problem, and I have to create three
Trying to use the TThreadedQueue (Generics.Collections) in a single producer multiple consumer scheme. (Delphi-XE).
I currently have two clients (Producer/Consumer), and I am trying to send a large
I am trying to add asynchronous output to a my program. Currently, I have
Properties of the bounded buffer class I'm trying to build... Multiple producer, multiple consumer.
I'm trying to write a seemingly simple implementation of the classic producer - consumer
I'm trying to implement a producer/consumer model multithreaded program in C++ for a project
Right now I am trying to create a producer/consumer thread, the producer thread goes

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.