pthread 基本线程函数 和 定时器

Author-------Dansen-----xzd2734@163.com

include <pthread.h>
int  pthread_create(pthread_t  *  thread, pthread_attr_t * attr, void * (*start_routine)(void *), void * arg);
pthread_create 建立一个新线程,线程的id放入thread指针指向的pthread_t结构中,attr是线程属性,一般用NULL就可以了
start_routine 是新线程运行的函数,arg是start_routine函数的参数。
int pthread_join(pthread_t th, void **thread_return);
pthread_join 挂起当前的进程,等待th指示的线程执行结束,**thread_return是线程结束的返回值,不需要可以设为NULL。
void pthread_exit(void *retval);
在线程函数中调用,结束当前线程,并返回retval指针,*retval不能指向局部函数的变量。
下面是一个例子,更具体的可以参考man手册。
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <pthread.h>
void * hello()
{
 void *ret;
 ret=malloc(sizeof(int));
 *(int *)ret=10;
 printf("hello world1/n");
 sleep(1);
 printf("hello world2/n");
 pthread_exit(ret);
 printf("hello world3/n");
}
void * hello2()
{
 printf("pthread2 1/n");
 sleep(2);
}
int main()
{
 pthread_t id1,id2;
 int *i1;
 printf("dansen is me 1/n");
 pthread_create(&id1,NULL,hello,NULL);
 pthread_create(&id2,NULL,hello2,NULL);
 printf("dansen is me 2/n");
 printf("dansen is me 3/n");
 pthread_join(id1,(void * *)&i1);
 printf("dansen is me 4/n");
 pthread_join(id2,NULL);
 printf("id1 return is %d/n",*i1);
 free(i1);
 perror("free:");
 return 0;
}

编译:
gcc -o hello hello.c -lpthread

运行结果:
dansen is me 1
hello world1
pthread2 1
dansen is me 2
dansen is me 3
hello world2
dansen is me 4
id1 return is 10
free:: Success

Linux下有两种定时器:

1、alarm()
-------------------------------------------
    如果不要求很精确的话,用alarm()和signal()就够了
    unsigned int alarm(unsigned int seconds)
    函数说明: alarm()用来设置信号SIGALRM在经过参数seconds指定的秒数后传送给目前的进程。如果参数seconds为0,则之前设置的闹钟会被取消,并将剩下的时间返回。
    返回值: 返回之前闹钟的剩余秒数,如果之前未设闹钟则返回0。

    alarm()执行后,进程将继续执行,在后期(alarm以后)的执行过程中将会在seconds秒后收到信号SIGALRM并执行其处理函数。

#include <stdio.h>
#include <unistd.h>
#include <signal.h>

void sigalrm_fn(int sig)
{
    printf("alarm!/n");
    alarm(2);
    return;
}

int main(void)
{
    signal(SIGALRM, sigalrm_fn);
    alarm(1);
    while(1) pause();
}


2、setitimer()
-------------------------------------------
    int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue));
    setitimer()比alarm功能强大,支持3种类型的定时器:
    ITIMER_REAL :     以系统真实的时间来计算,它送出SIGALRM信号。
    ITIMER_VIRTUAL : -以该进程在用户态下花费的时间来计算,它送出SIGVTALRM信号。
    ITIMER_PROF :     以该进程在用户态下和内核态下所费的时间来计算,它送出SIGPROF信号。
    setitimer()第一个参数which指定定时器类型(上面三种之一);第二个参数是结构itimerval的一个实例
        第三个参数返回旧定时器的值。
    setitimer()调用成功返回0,否则返回-1。
    struct itimerval
 {
    struct timeval it_interval;    设置的定时时间,如下例中置位200000微秒。
    struct timeval it_value;  刚设置时定时器上的时间,如果为0则定时器无效,这段时间减到0后发出信号,以后
      就从it_interval中把时间加载到定时器中。一般设为和it_interval一样。
 }
    struct timeval
 {
    long tv_sec;  /* seconds */
    long tv_usec; /* useconds */
 }
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>

void sigalrm_fn(int sig)
{
    printf("alarm!/n");
    return;
}

int main(void)
{
    struct itimerval value, ovalue;
    signal(SIGALRM, sigalrm_fn);
    value.it_value.tv_sec=0;
    value.it_value.tv_usec=200000;
    value.it_interval.tv_sec=0;
    value.it_interval.tv_usec=200000;
    setitimer(ITIMER_REAL, &value, &ovalue);
    while(1) pause();
}

可以用usleep()函数实现微秒级延时.....

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值