pthread

线程对于共享资源不安全,线程拥有各自的栈空间.
主动终止:  return  pthread_eixt()参数通用指针
被动终止: pthread_cancle(pthid);同一个进程的其他线程

exit(0)  _exit(0)  进程终止

pthread_join(pthread_t th_id,void **thread_return);
阻塞 ,等待子线程结束,并且释放资源
参数2 等待线程返回值(  return(0) pthread_exit(0)




#include<stdlib.h>
#include <pthread.h>
#include <stdio.h>
#include <math.h>
typedef struct
{
    char name[20];
    int time;
    int start;
    int end;
}ReacArg;

//线程函数
void* th_fn(void *arg)
{
    ReacArg *r = (ReacArg*)arg;
    int i=r->start;
    for(;i<=r->end;i++)
    {
        printf("%s(%lx) run %d\n",r->name,pthread_self(),i);

        usleep(r->time);//微妙
    }

    return (void*)0;
}
int main()
{

    int err ;
    //创建创建线程rabbit
    pthread_t rabbit ,turtle;
    ReacArg r_a={"rabbit",(int)(drand48()*100000),20,50};
    ReacArg t_a={"turtle",(int)(drand48()*1000),20,50};

    if((err = pthread_create(&rabbit,NULL,th_fn,(void*)&r_a))!=0)
    {

        perror("pthread_create error");
    }
    //创建线程turtle   第四个参数 结构体
    if((err = pthread_create(&turtle,NULL,th_fn,(void*)&t_a))!=0)
    {

        perror("pthread_create error");
    }
    pthread_join(rabbit,NULL);
    pthread_join(turtle,NULL);

    printf("contrl thread id: %lx",pthread_self());
    printf("finished\n");
return 0;

}


#include<stdlib.h>
#include <pthread.h>
#include <stdio.h>
typedef struct
{
    int d1;
    int d2;

}Arg;

void *th_fn(void* arg)
{

    Arg *r = (Arg*)arg;
    return (void*)r;

}

int main(void)
{

    int err;
    //int *result;  //接受返回值

    pthread_t th;

    printf("%lx\n",sizeof(enum type));
    Arg r={30,50};
    if((err = pthread_create(&th,NULL,th_fn,(void*)&r))!=0)
    {
        perror("create erro\n");

    }
//    pthread_join(th,(void**)&result);
//   printf("获得结果:%d\n",((Arg*)result)->d1);
int re;
pthread_join(th,(void*)&re);
printf("获得结果:%d\n",((Arg*)re)->d1);
    return 0;
}
//定义结构体(多个对象需要同样的处理)  开启线程(处理)    返回结果

主要用到的函数:
sem_t sem;

int sem_init(sem_t *sem, int pshared, unsigned int value);,其中sem是要初始化的信号量,pshared表示此信号量是在进程间共享还是线程间共享,value是信号量的初始值。
int sem_destroy(sem_t *sem);,其中sem是要销毁的信号量。只有用sem_init初始化的信号量才能用sem_destroy销毁。
int sem_wait(sem_t *sem);等待信号量,如果信号量的值大于0,将信号量的值减1,立即返回。如果信号量的值为0,则线程阻塞。相当于P操作。成功返回0,失败返回-1。
int sem_post(sem_t *sem); 释放信号量,让信号量的值加1。相当于V操作。



#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

将信号量定义为全局变量,方便多个线程共享
sem_t sem;


void * get_service(void *thread_id)
{
    int customer_id = *((int *)thread_id);

    if(sem_wait(&sem) == 0) {
        usleep(100);                 service time: 100ms
        printf("customer %d receive service ...id:%ld\n", customer_id,getpid());
        printf("l%d\n",pthread_self());
        sem_post(&sem);
    }
}

#define CUSTOMER_NUM 10

int main(int argc, char *argv[])
{
     初始化信号量,初始值为2,表示有两个顾客可以同时接收服务

    sem_init(&sem, 0, 2);


    pthread_t customers[CUSTOMER_NUM];

    int i, ret;

    for(i = 0; i < CUSTOMER_NUM; i++){
        int customer_id = i;
        ret = pthread_create(&customers[i], NULL, get_service, &customer_id);
        if(ret != 0){
            perror("pthread_create");
            exit(1);
        }
        else {
            printf("Customer %d arrived.\n", i);
        }
        usleep(10);
    }


    int j;
    for(j = 0; j < CUSTOMER_NUM; j++) {
        pthread_join(customers[j], NULL);
    }

    sem_destroy(&sem);
    return 0;
}
*/
posted @ 2019-06-04 16:26  countryboy666  阅读(223)  评论(0编辑  收藏  举报