linux线程

 

 

 

#include <stdio.h>
#include <iostream>
#include "unistd.h"
#include "assert.h"
#include <stdlib.h>
#include "sys/wait.h"
#include <pthread.h>

pthread_t ntid;

void printids(std::string s){
    pid_t pid;
    pthread_t tid;

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

void* thr_callback(void* arg){
    sleep(20);
    printids("new thread:");

    return 0;
}


int main(int argc, char* argv[]){
    int err;
    err = pthread_create(&ntid, NULL, thr_callback, NULL);
    if(err != 0)
        printf("can't create thread, error code: %d\n", err);

    printids("main thread:");
    sleep(10);
    pthread_exit(0);
}

 

posted on 2015-03-31 00:09  allen__  阅读(104)  评论(0编辑  收藏  举报

导航