23,215
社区成员




#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);
}
#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);
}