线程创建及退出

线程创建及退出

例1: 线程的创建及打印所在进程的进程号/线程号

mypthread_create.c

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

// gcc -g mypthread_create.c -o mypthread_create -lpthread

#define ERROR(flag,msg)            \
if(flag)                        \
{                                \
    printf("%d: ",__LINE__);    \
    fflush(stdout);                \
    perror(msg);                \
    exit(errno);                \
}

void print(char *s)
{
    pid_t pid = getpid();
    pthread_t tid = pthread_self();
    printf("%s pid = %d,  tid = %u\n", s, pid, tid);
}

void *thread_func(void *arg)
{
    print(arg);
    return NULL;
}

int main(int argc, char *argv[])
{
    pthread_t tid;

    int ret = pthread_create(&tid,NULL,thread_func,"new thread");
    ERROR(ret != 0, "pthread_create()");

    print("main thread");
    
    thread_func("main thread_func");

    sleep(2);
    puts("byebye...");

    return 0;
}

编译链接运行, 输出如下:

以上输出一表明: 一个进程内创建一个线程后, 两线程的进程号pid相同, 线程号tid不同.

例2: 取得线程的退出状态

mypthread_exit.c

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

//gcc -g mypthread_exit.c -o mypthread_exit -lpthread

#define ERROR(flag,msg)            \
if(flag)                        \
{                                \
    printf("%d: ",__LINE__);    \
    fflush(stdout);                \
    perror(msg);                \
    exit(errno);                \
}

void print(char *s)
{
    pid_t pid = getpid();
    pthread_t tid = pthread_self();
    printf("%s pid = %d,  tid = %lu\n", s, pid, tid);
}

void *thread_func(void *arg)
{
    print(arg);
    sleep(3);
//pthread_exit((void *)888); puts(
"i'm pthread..."); return (void *)9999; } int main(int argc, char *argv[]) { pthread_t tid; int ret = pthread_create(&tid,NULL,thread_func,"new thread"); ERROR(ret != 0,"pthread_create()"); print("main thread"); int stat; pthread_join(tid,(void **)&stat); printf("thread exit code = %d\n",stat); puts("byebye..."); return 0; }

编译链接运行, 输出如下:

前一次注释掉pthread_exit(), 线程的返回码是"return 9999"返回的值.

后一次调用pthread_exit()退出, 线程的返回码是pthread_exit()传入的参数888.  

例3: 线程"被退出"

mypthread_cancel.c

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

//gcc -g mypthread_cancel.c -o mypthread_cacel -lpthread

#define ERROR(flag,msg)            \
if(flag)                        \
{                                \
    printf("%d: ",__LINE__);    \
    fflush(stdout);                \
    perror(msg);                \
    exit(errno);                \
}

void print(char *s)
{
    pid_t pid = getpid();
    pthread_t tid = pthread_self();
    printf("%s pid = %d,  tid = %lu(0x%lx)\n", s, pid, tid, tid);
}

void *thread_func(void *arg)
{
    print("new thread");
    sleep(2);
    printf("thread num = %ld\n", (long)arg);

    return arg;
}

#define MAX_THREAD 5

int main(int argc, char *argv[])
{
    pthread_t tid[MAX_THREAD];
    int i;

    for(i = 0;i < MAX_THREAD;i++)
    {
        int ret = pthread_create(&tid[i], NULL, thread_func, (void *)(long)i);
        ERROR(ret != 0,"pthread_create()");
    }

    print("main thread");

    for(i = 0;i < MAX_THREAD;i++)
    {
        pthread_cancel(tid[i]);
    }

    int stat[MAX_THREAD];
    for(i = 0; i < MAX_THREAD; i++)
    {
        pthread_join(tid[i], (void **)&stat[i]);
        printf("%d: thread %lu exit code = %d\n", i, tid[i], stat[i]);
    }

    puts("byebye...");

    return 0;
}

编译链接运行, 输出如下:

 

 

相对地, 如果注释掉pthread_cancel()函数, 输出如下:

posted @ 2016-01-17 21:51  zhanglong71  阅读(415)  评论(0编辑  收藏  举报