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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:24:25+00:00 2026-05-24T11:24:25+00:00

I’m keep getting a segmentation fault from where I create thread and declare some

  • 0

I’m keep getting a segmentation fault from where I create thread and declare some variables in the struct…
Does anyone know why?

dispatch_queue_t *dispatch_queue_create(queue_type_t queueType){

int cores = num_cores();
printf("message-queuecreate1");

dispatch_queue_t *dispatch_queue = (dispatch_queue_t *) malloc(sizeof(dispatch_queue_t));
dispatch_queue->HEAD = NULL;
dispatch_queue->END = NULL;

//create a thread array for dispatcher and worker threads
dispatch_queue_thread_t *threads[cores+1];
threads[cores+1]= (dispatch_queue_thread_t *)malloc(sizeof(dispatch_queue_thread_t));

//create semaphores
sem_t queue_task_semaphore;
sem_t queue_thread_semaphore;
sem_t queue_wait_semaphore;

sem_init(&queue_task_semaphore, 0, 1);
sem_init(&queue_thread_semaphore,0,1);
sem_init(&queue_wait_semaphore,0,1);

dispatch_queue->queue_task_semaphore = queue_task_semaphore;
dispatch_queue->queue_thread_semaphore = queue_thread_semaphore;
dispatch_queue->queue_wait_semaphore = queue_wait_semaphore;




//create dispatcher thread      
//segmentation fault #1////////////////
threads[0]->current_task=NULL;
threads[0]->queue=dispatch_queue;

pthread_create(threads[0]->thread, NULL, dispatcher_threadloop, threads[0]);
//////////////////////////////////////  

if (queueType == CONCURRENT){
    int i = 0;
    int thread_id=0;
    //create worker thread array if the type of the queue is concurrent

    while(i<cores){

        //create worker thread

        thread_id = i+1;

        //segmentation fault #2//////////
        threads[i+1]->queue=dispatch_queue;
        threads[thread_id]->thread=NULL;
        threads[thread_id]->current_task=NULL;

        pthread_create(threads[thread_id]->thread, NULL, worker_threadloop, (void *)(long)i);
        ////////////////////////////////

        printf("thread %d is created\n",i);
        i++;
    }

} else {//do smth}
    //segmentation fault# 3////////////////
    threads[1]->thread=worker_thread;
    threads[1]->queue=dispatch_queue;
    threads[1]->current_task=NULL;
    //////////////////////////////////////
}



return dispatch_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-24T11:24:26+00:00Added an answer on May 24, 2026 at 11:24 am

    Your code is riddled with problems:

    Accessing threads[core + 1] is invalid. Also, you’re not allocating memory for the threads[0] … threads[core].

    dispatch_queue_thread_t *threads[cores+1];
    threads[cores+1]= ....;
    

    So these will fail:

    threads[0]->current_task=NULL; /* See above. */
    
    threads[i+1]->queue=dispatch_queue; /* Again, no memory allocated. */
    

    There could be other problems but I would start by slashing the cores+1 stuff and replacing it with:

    for (i = 0; i < cores; i++) {
        threads[i] = malloc(sizeof(*threads[i]));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post

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.