gpt4 book ai didi

c - 子 pthread 发生段错误

转载 作者:行者123 更新时间:2023-11-30 17:01:57 24 4
gpt4 key购买 nike

我目前正在开发在 C 中使用 pthread 的自定义线程调度程序项目。我一直在概念上苦苦挣扎,但最终得到了我期望看到的行为,除了段错误。

我的工作是注册五个子线程,并根据数组中存储的 ID 顺序来调度每个子线程。我所做的是调用 pthread_mutex_lock 并告诉不首先调度的子线程等待。我对计数器做了一些事情,以跟踪​​何时应该安排下一个子线程,并且在一个子线程将计数器增加到 5 后,它应该唤醒其他线程,并按照定义的主循环次数执行相同的操作。

所有变量:

#define NTHREADS         5             /* Number of child threads        */
#define NUM_LOOPS 10 /* Number of local loops */
#define SCHEDULE_INTERVAL 1 /* thread scheduling interval */

#define errexit(code,str) fprintf(stderr,"%s: %s\n",(str),strerror(code));exit(1);

int schedule_vector[NTHREADS]; /* The thread schedule vector */

int flag = 0;
pthread_cond_t cv; // condtitional variable
pthread_mutex_t mtx; // mutex semaphore 1
int globalcounter = 0;
int currentThread;
#define TASK_LIMIT 6

这是父线程:

int main(int argc,char *argv[])
{
int i;
int worker;
int ids[NTHREADS];
int errcode;
int *status;
int policy;

pthread_t threads[NTHREADS];

/* Create child threads --------------------------------------------- */
for (worker = 0; worker < NTHREADS; worker++)
{
ids[worker] = worker;

printf("creating child thread using id %d\n", worker);

/* Create a child thread ----------------------------------------- */
pthread_create (
&threads[worker],
NULL,
my_thread,
&ids[worker]);
}

/* Initialize the thread schedule vector -------------------------- */
schedule_vector[0] = 0; /* First thread to be executed (0) */
schedule_vector[1] = 1; /* Second thread to be exceuted (1) */
schedule_vector[2] = 2; /* Third thread to be executed (2) */
schedule_vector[3] = 3; /* Fourth thread to be executed (3) */
schedule_vector[4] = 4; /* Fifth thread to be executed (4) */

signal(SIGALRM, clock_interrupt_handler);
alarm(SCHEDULE_INTERVAL);
printf("handler set up\n");

/* Reap the threads as they exit ----------------------------------- */
for (worker = 0; worker < NTHREADS; worker++)
{
/* Wait for thread to terminate --- */
if (errcode=pthread_join(threads[worker],(void *) &status))
{ errexit(errcode,"pthread_join"); }

/* Check thread's exit status and release its resources -------- */
if (*status != worker)
{
fprintf(stderr,"thread %d terminated abnormally\n",worker);
exit(1);
}
}

/* The main (parent) thread terminates itself ---------------------- */
return(0);
}

这是子线程函数:

void *my_thread(void * arg)
{
long int i;
long int counter;
int myid=*(int *) arg;
counter = 0;
printf("\nI am thread #%d\n\n", myid);

/* Main loop ------------------------------------------ */
for (i = 0; i < NUM_LOOPS; i++)
{

currentThread = myid;
pthread_mutex_lock(&mtx);
while(myid != schedule_vector[flag]){
pthread_cond_wait(&cv, &mtx);
}
counter++;
globalcounter = counter;


printf("Thread: %d is running ...\n", myid);
usleep(100000);
}
return arg;
}

这是我的中断处理程序:

void clock_interrupt_handler(void)
{

printf("scheduler started ++++++++++++++++++++++++++++++++++ \n");
if (currentThread == schedule_vector[flag]) {
printf("scheduled thread received: thread %d, and it's counter is at %d\n", currentThread, globalcounter);
while(globalcounter < TASK_LIMIT){
if(globalcounter == 5){
flag++;
pthread_cond_broadcast(&cv);
}
pthread_mutex_unlock(&mtx);
alarm(SCHEDULE_INTERVAL);
}
} else {
printf("unscheduled thread received, putting thread %d with count %d to sleep...\n", currentThread, globalcounter);
alarm(SCHEDULE_INTERVAL);

}


}

这是输出:

scheduler started ++++++++++++++++++++++++++++++++++
scheduled thread received: thread 0, and it's counter is at 1
Thread: 0 is running ...
Thread: 0 is running ...
Thread: 0 is running ...
Thread: 0 is running ...
Segmentation fault (core dumped)

它基本上重复此行为,但针对每个线程。我想了解到底是什么导致了段错误

最佳答案

似乎 pthread_mutex_lock/pthread_mutex_unlock 不配对。

正确的代码如下

pthread_mutex_lock()
pthread_cond_broadcast()
pthread_mutex_unlock()

pthread_mutex_lock()
pthread_cond_wait()
pthread_mutex_unlock()

关于c - 子 pthread 发生段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36757064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com