c语言 free函数报段错误,pthread_join 函数报段异常 ?

pthread_join 函数报段错误 ??????

#include 

#include 

#include 

void *thread_fn(void*);

struct foo* foo_aloc(void);

void foo_hold(struct foo*);

void foo_rele(struct foo*);

void my_err(char *);

struct foo

{

int f_count;

pthread_mutex_t f_lock;

/* More stuff here */

};

//主函数

int main(void)

{

printf("start\n");

struct foo*fp;

pthread_t tid;

/*  为了便于调试错误,注掉了

if((fp=foo_aloc())==NULL)

my_err("foo_aloc error\n");

*/

if((tid=pthread_create(&tid,NULL,thread_fn,NULL))!=0)

my_err("pthread_create error\n");

sleep(1);

printf("will join\n");

/* Wait the second thread run over */

//错误就在这里:join函数报段错误!谁能告诉我,这是为什么?????

if(pthread_join(tid,NULL)!=0)

my_err("pthread_join error");

//    printf("after join,pthread_exit return:%d\n",(int)ret);

int i;

printf("after join\n");

/*

for(i=0;i<10;i++)

{

printf("f_count:%d\n",fp->f_count);

foo_rele(fp);

}

if(fp==NULL)

printf("fp is NULL now");

*/

printf("end\n");

exit(0);

}

void* thread_fn(void *arg)

{

printf("thread_fn is running\n");

/*    int i;

struct foo *fp=(struct foo*)arg;

for(i=0;i<10;i++)

{

fp->f_count++;

}

*/

printf("thread will return \n");

return (void*)0;

}

struct foo* foo_aloc(void)

{

struct foo *fp;

if((fp=malloc(sizeof(struct foo)))!=NULL)

{

//fp->f_count=1;

if(pthread_mutex_init(&fp->f_lock,NULL)!=0)

{

free(fp);

return NULL;

}

/* Continue initialization */

}

return fp;

}

/* Add a reference to the object */

void foo_hold(struct foo *fp)

{

pthread_mutex_lock(&fp->f_lock);

fp->f_count++;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C语言实现SCHED_FIFO算法的代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sched.h> #include <errno.h> #define STACK_SIZE 1024*1024 void* thread_function(void* arg); int main() { int thread_id; pthread_t thread; pthread_attr_t attr; struct sched_param param; int policy; void* stack; // 分配线程栈 stack = malloc(STACK_SIZE); if (stack == NULL) { perror("malloc"); exit(EXIT_FAILURE); } // 初始化线程属性 if (pthread_attr_init(&attr) != 0) { perror("pthread_attr_init"); exit(EXIT_FAILURE); } // 设置线程栈 if (pthread_attr_setstack(&attr, stack, STACK_SIZE) != 0) { perror("pthread_attr_setstack"); exit(EXIT_FAILURE); } // 设置调度策略为SCHED_FIFO policy = SCHED_FIFO; if (pthread_attr_setschedpolicy(&attr, policy) != 0) { perror("pthread_attr_setschedpolicy"); exit(EXIT_FAILURE); } // 设置线程优先级 param.sched_priority = 50; if (pthread_attr_setschedparam(&attr, &param) != 0) { perror("pthread_attr_setschedparam"); exit(EXIT_FAILURE); } // 创建线程 if (pthread_create(&thread, &attr, &thread_function, NULL) != 0) { perror("pthread_create"); exit(EXIT_FAILURE); } // 等待线程结束 if (pthread_join(thread, NULL) != 0) { perror("pthread_join"); exit(EXIT_FAILURE); } // 销毁线程属性 if (pthread_attr_destroy(&attr) != 0) { perror("pthread_attr_destroy"); exit(EXIT_FAILURE); } free(stack); exit(EXIT_SUCCESS); } void* thread_function(void* arg) { // 打印线程信息 printf("Thread running.\n"); printf("Thread priority: %d\n", sched_get_priority_min(SCHED_FIFO)); printf("Thread policy: %d\n", sched_getscheduler(0)); // 让线程休眠5秒钟 sleep(5); printf("Thread finished.\n"); return NULL; } ``` 在以上示例中,我们使用了pthread库来创建线程,设置线程属性和等待线程结束。在主函数中,我们首先分配线程栈,然后初始化线程属性。接着,我们设置线程栈和调度策略为SCHED_FIFO,并设置线程优先级为50。然后,我们创建线程,等待线程结束,销毁线程属性,释放线程栈,并退出程序。 在线程函数中,我们打印线程信息,让线程休眠5秒钟,然后打印线程结束信息并返回NULL。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值