c,pthread_create 给出分段错误?

发布于 2024-11-28 17:00:35 字数 2110 浏览 2 评论 0原文

我从创建线程并在结构中声明一些变量的地方不断收到分段错误...
有谁知道为什么?

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;

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉梦枕江山 2024-12-05 17:00:35

您的代码充满了问题:

访问线程[core + 1]无效。此外,您没有为 threads[0] ... threads[core] 分配内存。

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

所以这些都会失败:

threads[0]->current_task=NULL; /* See above. */

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

可能还有其他问题,但我首先会削减 cores+1 内容并将其替换为:

for (i = 0; i < cores; i++) {
    threads[i] = malloc(sizeof(*threads[i]));
}

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]));
}
你是我的挚爱i 2024-12-05 17:00:35

假设

threads[]->thread 

是 pthread_t (而不是 pthread_t *),

您需要提供参考:

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

Assuming

threads[]->thread 

is a pthread_t (and not a pthread_t *)

You need to give the reference:

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

.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文