`

创建线程

 
阅读更多
pthread_create:创建新的控制流
pthread_exit:从现有的控制流中退出
pthread_join:从控制流中得到退出状态
pthread_cleanup_push:注册在退出控制流时调用的函数
pthread_self:获取控制流的ID
pthread_cancel:请求控制流的非正常退出


#include"apue.h"
//#include<stdio.h>
#include<pthread.h>

pthread_t ntid;

void printids(const char* s)
{
    pid_t   pid;
    pthread_t   tid;

    pid = getpid();
    tid = pthread_self();
    printf("%s pid %u tid %u (0x%x)\n",s,(unsigned int)pid,(unsigned int)tid,(unsigned int)tid);
}

void * thr_fn(void * arg)
{
    printids("new thread:");
    return ((void *)0);
}

void* testCallBack(void *arg)
{
    printf("this is my thread create");
}

int main()
{
    int err;

    err = pthread_create(&ntid,NULL,thr_fn,NULL);
    if(err!=0)
        err_quit("can't create thread: %s\n",strerror(err));
    printids("main thread:");
    sleep(1);//这时就会调用thr_fn
    exit(0);
}

main thread: pid 2862 tid 3078543056 (0xb77ed6d0)
new thread: pid 2862 tid 3078536048 (0xb77ebb70)
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics