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

  • Home
  • SEARCH
  • 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 5971633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:32:36+00:00 2026-05-22T20:32:36+00:00

I have: struct elem { data d; struct elem *next; }; typedef struct elem

  • 0

I have:

struct elem {                  
   data          d;
   struct elem   *next;
};

typedef   struct elem   elem;

struct queue {
   int    cnt;                  
   elem   *front;              
   elem   *rear;               
};

void enqueue(data d, queue *q);

void enqueue(data d, queue *q)
{
   elem   *p;
   p = malloc(sizeof(elem));
   p -> d = d;
   p -> next = NULL;
   if (!empty(q)) {
      q -> rear -> next = p;
      q -> rear = p;
   }
   else
      q -> front = q -> rear = p;
   q -> cnt++;
}

which would be call:

int main(){
   struct queue Q;
   initialize(&Q); //init the queue
   enqueue( 10000, &Q);  
return 0;
}

and some thread creation like:

   #include <pthread.h>
   #include <stdio.h>
   #include <stdlib.h>
   #define NUM_THREADS 5
   /**
    * 
    *
    */
  pthread_t threads[NUM_THREADS];
  long t;
  for(t=0;t<NUM_THREADS;t++){
      pthread_create(&threads[t], NULL, enqueue, (void *)t);
  }

How should I modify the enqueue function so in the pthread_create each thread call

enqueue( variable, &Q);  

(I am doing a lock free queue, and already has the logic, but I am stuck in How would each thread call enqueue function…)

–EDIT–

I am doing the answer proposed and getting:

    queue.c: In function ‘main’:
    queue.c:130: 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 ‘void (*)(data,  struct queue *)’
  • 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-05-22T20:32:37+00:00Added an answer on May 22, 2026 at 8:32 pm

    There is an example, without any error checking, etc. Also, if you use thread you should use mutex to prevent simultaneous access to your queue (or use some lock free algo).
    Just add the next changes:

    struct thread_data {
        data          d;
        struct queue *q;
    };
    
    pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
    struct queue Q;
    
    void* thread_func(void* a)
    {
        struct thread_data *t = (struct thread_data *)a;
    
        pthread_mutex_lock(&mutex); // Just example, better use Lock in enqueue
        enqueue(t->d, t->q);
        pthread_mutex_unlock(&mutex);
    
        free(t);
        return NULL;
    }
    
    int main(){
        pthread_t threads[NUM_THREADS];
        long t; // I hope you will initialize it in some proper way
    
        initialize(&Q); //init the queue
        for(t=0;t<NUM_THREADS;t++){
            struct thread_data *arg = (struct thread_data *) malloc(sizeof(struct thread_data));
            arg->q = &Q;
            arg->d = t; // Actually it is should be your data
            pthread_create(&threads[t], NULL, thread_func, (void *)arg); //FIXED: thread_func is the thread function
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i have struct node { int number; struct node *next; struct deck{ int
#include <stdio.h> typedef struct point{ int x; int y; }; void main (void){ struct
Say we have typedef struct { int value1; int value2; } values_t; and values_t*
I have this struct: struct Map { public int Size; public Map ( int
Say I have a struct s with an int pointer member variable i. I
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
struct elem { int i; char k; }; elem user; // compile error! struct
I have seen many programs consisting of structures like the one below typedef struct
I have a struct like this: typedef struct string { unsigned long length; unsigned
ok well i have structure struct balls { balls(UINT_PTR const &val) : BALL_ID(val){} int

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.