POSIX线程(2)
#include <sys/types.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
int main(void)
{
pthread_attr_t attr;
pthread_attr_init(&attr);
int state;
pthread_attr_getdetachstate(&attr, &state);
if (state == PTHREAD_CREATE_JOINABLE)
printf("detachstate:PTHREAD_CREATE_JOINABLE\n");
else if (state == PTHREAD_CREATE_DETACHED)
printf("detachstate:PTHREAD_CREATE_DETACHED");
size_t size;
pthread_attr_getstacksize(&attr, &size);
printf("stacksize:%d\n", size);
pthread_attr_getguardsize(&attr, &size);
printf("guardsize:%d\n", size);
int scope;
pthread_attr_getscope(&attr, &scope);
if (scope == PTHREAD_SCOPE_PROCESS)
printf("scope:PTHREAD_SCOPE_PROCESS\n");
if (scope == PTHREAD_SCOPE_SYSTEM)
printf("scope:PTHREAD_SCOPE_SYSTEM\n");
int policy;
pthread_attr_getschedpolicy(&attr, &policy);
if (policy == SCHED_FIFO)
printf("policy:SCHED_FIFO\n");
else if (policy == SCHED_RR)
printf("policy:SCHED_RR\n");
else if (policy == SCHED_OTHER)
printf("policy:SCHED_OTHER\n");
int inheritsched;
pthread_attr_getinheritsched(&attr, &inheritsched);
if (inheritsched == PTHREAD_INHERIT_SCHED)
printf("inheritsched:PTHREAD_INHERIT_SCHED\n");
else if (inheritsched == PTHREAD_EXPLICIT_SCHED)
printf("inheritsched:PTHREAD_EXPLICIT_SCHED\n");
struct sched_param param;
pthread_attr_getschedparam(&attr, ¶m);
printf("sched_priority:%d\n", param.sched_priority);
pthread_attr_destroy(&attr);
int level;
level = pthread_getconcurrency();
printf("level:%d\n", level);
return 0;
}
#include <sys/types.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
typedef struct tsd
{
pthread_t tid;
char *str;
}tsd_t;
pthread_key_t key_tsd;
pthread_once_t once_control = PTHREAD_ONCE_INIT;
void destroy_routine(void *value)
{
printf("destory ...\n");
free(value);
}
void once_routine(void)
{
pthread_key_create(&key_tsd, destroy_routine);
printf("key init ...\n");
}
void* thread_routine(void *arg)
{
pthread_once(&once_control, once_routine);
tsd_t *value = (tsd_t*)malloc(sizeof(tsd_t));
value->tid = pthread_self();
value->str = (char*)arg;
pthread_setspecific(key_tsd, value);
printf("%s setspecific %p\n", (char*)arg, value);
value = pthread_getspecific(key_tsd);
printf("tid=0x%x str=%s\n", (int)value->tid, value->str);
sleep(2);
value = pthread_getspecific(key_tsd);
printf("tid=0x%x str=%s\n", (int)value->tid, value->str);
return NULL;
}
int main(void)
{
//pthread_key_create(&key_tsd, destroy_routine);
pthread_t tid1;
pthread_t tid2;
pthread_create(&tid1, NULL, thread_routine, "thread1");
pthread_create(&tid2, NULL, thread_routine, "thread2");
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_key_delete(key_tsd);
return 0;
}
CC=gcc
CFLAGS=-Wall -g
BIN=threadattr tsd
all:$(BIN)
%.o:%.c
$(CC) $(CFLAGS) -c $< -o $@
threadattr:threadattr.o
$(CC) $(CFLAGS) $^ -o $@ -lpthread
tsd:tsd.o
$(CC) $(CFLAGS) $^ -o $@ -lpthread
clean:
rm -f *.o $(BIN)
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。