美文网首页
pthread_create

pthread_create

作者: OK2018 | 来源:发表于2018-05-09 10:27 被阅读0次
#include<stdio.h>
#include<stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <string.h>

pthread_t ntid;

void printids(const char *s)
{
//    pid_t   pid;
//    pthread_t   tid;
    
//    pid = getpid();
    pthread_t tid = pthread_self();

//    printf("%s pid %lu tid %lu (0x%lx)\n", s, (unsigned long)pid, (unsigned long)tid, (unsigned long)tid);
    printf("%s pid tid %lu (0x%lx)\n", s,  (unsigned long)tid, (unsigned long)tid);
}

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

int main()
{
    int err;
    
    err = pthread_create(&ntid, NULL, thr_fn, NULL);
    if(err!=0)
    {
        printf("can't create thread\n");
        exit(1);
    }
    printids("main thread:");
    sleep(2);
    exit(0);
}

编译运行命令
gcc test.c -o test
./test

相关文章

网友评论

      本文标题:pthread_create

      本文链接:https://www.haomeiwen.com/subject/lnhnrftx.html