pthread_create创建线程失败,返回值22

yuzhong1992 2017-05-03 02:41:20
使用pthread_create创建线程,如果在之前加上这句代码(如下),就总是失败,返回值22。
pthread_attr_getschedparam(&attr, SCHED_RR);
我的想法就是需要创建实时调度的线程。用上sudo执行程序,也是不行的。
...全文
3149 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
LubinLew 2017-05-14
  • 打赏
  • 举报
回复
设置了PTHREAD_EXPLICIT_SCHED ,就要手动设置参数(pthread_attr_setschedparam)

#include <stdio.h>
#include <stdlib.h> //for exit
#include <unistd.h> //for sleep
#include <string.h> //for strerror
#include <errno.h>
#include <pthread.h>

#define PTHREAD_RET_CHK(ret, func)  { \
	if (ret != 0) {	\
		if (ret == -1) { \
			printf("%s failed with code %d, %s\n", #func, ret, strerror(errno));\
		} else { \
			printf("%s failed with code %d, %s\n", #func, ret, strerror(ret)); \
		} \
	exit(EXIT_FAILURE); \
	} \
}


void* routine_entry(void* arg)
{
	sleep(1);
	printf("i am the new thread\n");
	sleep(1);
	pthread_exit(arg);
}

int main(void)
{
	int ret = 0;
	int status = 0;
	pthread_attr_t attr;
	struct sched_param param;
	pthread_t pid;

	ret = pthread_attr_init(&attr);
	PTHREAD_RET_CHK(ret, pthread_attr_init);

	ret = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
	PTHREAD_RET_CHK(ret, pthread_attr_setinheritsched);

	ret = pthread_attr_setschedpolicy(&attr, SCHED_RR);
	PTHREAD_RET_CHK(ret, pthread_attr_setschedpolicy);

	param.sched_priority = sched_get_priority_max(SCHED_RR);
	ret = pthread_attr_setschedparam(&attr, ¶m);
	PTHREAD_RET_CHK(ret, pthread_attr_setschedparam);

	ret = pthread_create(&pid, &attr, routine_entry, (void*)123);
	PTHREAD_RET_CHK(ret, pthread_create);

	ret = pthread_attr_destroy(&attr);
	PTHREAD_RET_CHK(ret, retpthread_attr_destroy);	

	ret = pthread_join(pid, (void**)&status);
	PTHREAD_RET_CHK(ret, retpthread_join); 

	printf("new thread is finish with code %d\n", status);

	exit(EXIT_SUCCESS);
}


yuzhong1992 2017-05-09
  • 打赏
  • 举报
回复
设置了线程不继承创建者的调度测试:PTHREAD_EXPLICIT_SCHED
LubinLew 2017-05-09
  • 打赏
  • 举报
回复
使用流程对吗

#include <stdio.h>
#include <stdlib.h> //for exit
#include <unistd.h> //for sleep
#include <string.h> //for strerror
#include <errno.h>
#include <pthread.h>

#define PTHREAD_RET_CHK(ret, func)  { \
	if (ret != 0) {	\
		if (ret == -1) { \
			printf("%s failed with code %d\n, %s", #func, ret, strerror(errno));\
		} else { \
			printf("%s failed with code %d\n", #func, ret); \
		} \
	exit(EXIT_FAILURE); \
	} \
}


void* routine_entry(void* arg)
{
	sleep(1);
	printf("i am the new thread\n");
	sleep(1);
	pthread_exit(arg);
}

int main(void)
{
	int ret = 0;
	int status = 0;
	pthread_attr_t attr;
	pthread_t pid;

	ret = pthread_attr_init(&attr);
	PTHREAD_RET_CHK(ret, pthread_attr_init);

	ret = pthread_attr_setschedpolicy(&attr, SCHED_RR);
	PTHREAD_RET_CHK(ret, pthread_attr_setschedpolicy);	

	ret = pthread_create(&pid, &attr, routine_entry, (void*)123);
	PTHREAD_RET_CHK(ret, pthread_create);

	ret = pthread_attr_destroy(&attr);
	PTHREAD_RET_CHK(ret, retpthread_attr_destroy);	

	ret = pthread_join(pid, (void**)&status);
	PTHREAD_RET_CHK(ret, retpthread_join); 

	printf("new thread is finish with code %d\n", status);

	exit(EXIT_SUCCESS);
}

lyl_709156147 2017-05-09
  • 打赏
  • 举报
回复
有没有使用pthread_attr_setinheritsched(pthread_attr_t *attr,int inheritsched)函数,设置线程是否继承创建者的调度策略。 inheritsched: PTHREAD_INHERIT_SCHED 继承创建者的调度策略 PTHREAD_EXPLICIT_SCHED 使用属性变量中的调度策略
yuzhong1992 2017-05-08
  • 打赏
  • 举报
回复
发帖时,头晕,写错了。 代码是这个: pthread_attr_setschedpolicy(&attr, SCHED_RR );
  • 打赏
  • 举报
回复
int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param); int pthread_attr_getschedparam(pthread_attr_t *attr, struct sched_param *param); 你这个参数都写错了

23,215

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧