初识线程

线程:

Linux下并没有真正意义上的线程,线程是模拟进程的,所以人们就自己写了一个库(pthread.h).

因此,使用这些函数库大家都知道应该怎么做。同时链接这些线程函数库时,要使用编译器命令: -lphread 选项

创建线程

#include<pthread.h>  
int pthread_create(pthread_t *thread,const pthread_attr_t *attr,void *(*start_routine)(void*),void *arg);  
参数:  
thread:返回线程id  
attr:设置线程属性,NULL表示默认属性  
start_routine:函数地址,线程启动后要执行的参数  
arg:传给线程启动函数的参数  
返回值:成功为0,失败返回错误码。 
void *rout(void *arg)
{
    int i;
    for(;;)
    {
        printf("i am thread 1\n");
        sleep(1);
    }
}

int main()
{
    pthread_t tid;
    int ret;
    if((ret = pthread_create(&tid,NULL,rout,NULL)) != 0)
    {
        fprintf(stderr,"pthread_create : %s\n",strerror(ret));
        exit(EXIT_FAILURE);
    }
    int i;
    for(;;)
    {
        printf(" i am main thread\n");
        sleep(1);
    }
}

[root@localhost pthread]# gcc pthread.c -lpthread
[root@localhost pthread]# ./a.out 
 i am main thread
i am thread 1
 i am main thread
i am thread 1
 i am main thread

或者写一个Makefile

pthread:pthread.c
	gcc -o $@ $^ -lpthread

.PHONY:clean

clean:
	rm -f pthread

注意:一定要加上链接库:-lpthread

UID         PID   PPID    LWP  C NLWP STIME TTY          TIME CMD
root       6846   3544   6846  0    2 13:36 pts/1    00:00:00 ./a.out
root       6846   3544   6847  0    2 13:36 pts/1    00:00:00 ./a.out

LWP :线程ID,或者说是getpid()系统调用的返回值

NLWP:线程组内线程的个数。

线程ID

线程库提供了一个pthread_self函数,用于查看线程自身id

#include <pthread.h>
pthread_t pthread_self(void);pth

pthread_t类型的线程ID,本质上就是进程地址空间上的一个地址。


线程终止

现在我们都清楚了线程是进程内部的一个执行分支,主线程退出进程也会终止。

所以如果只是终止线程而不终止进程的话,也有三个办法:

1.从线程函数return,但对主线程不适用,先当与在main中exit

2.线程可以调用pthrad_exit来终止自己

3.一个线程可以调用pthread_cancel终止同一个进程中的另一个线程

#include <pthread.h>
void pthread_exit(void *retval);

pthread_exit或者return 返回指针的指向必须是全局的或者是用malloc开辟的,不能在线程函数的栈上分配,因为当其他线程得到这个返回指针时,线程函数已经退出了。

//取消一个执行中的线程
#include <pthread.h>
int pthread_cancel(pthread_t thread);

成功为0,失败返回错误码。


线程等待和分离(阻塞式等待)

    为什么要等待?

1.已经退出的线程,其空间没有被释放,仍然在进程的地址空间内。

2.新创建的线程不会复用刚才退出线程的地址空间。

进程等待

#include <pthread.h>
int pthread_join(pthread_t thread, void **retval);

**retval 输出型参数,指向一个指针,指针又指向线程的返回值

调用该函数线程将会被挂起等待,直到id的线程被终止。线程通过不同的方法终止的,通过pthread_join得到的终止状态是不同的。

1.如果通过return返回,retval所指向的单元里存放的是thread线程函数的返回值。

2.若thread被其他线程调用pthrea_cancel异常终止掉,则retval所指向的单元里存放的是常熟PTHREAD_CANCELED.

3.若线程是自己调用pthread_exit终止的,retval所指向的单元里存放的是传给pthread_exit的参数。

4.若对线程的终止状态不感兴趣,可以直接将NULL传给retval。

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
void *thread1(void *arg)
{
    printf("thread 1 is returning...\n ");
    int* p = (int*)malloc(sizeof(int));
    *p = 1;
    return (void*)p;
}

void *thread2(void *arg)
{
    printf("thread 2 is exiting...\n");
    int* p = (int*)malloc(sizeof(int));
    *p = 2;
    pthread_exit((void*)p);
}

void *thread3(void* arg)
{
    while(1)
    {
        printf("thread 3 is running...\n");
        sleep(1);
    }
    return NULL;
}

int main()
{
    pthread_t tid;
    void* ret;

    //thread 1 return
    pthread_create(&tid,NULL,thread1,NULL);
    pthread_join(tid,&ret);
    printf("thread 1 return,thread 1 id %x,return code:%d\n",tid,*(int*)ret);
    free(ret);


    //thread 2 return
    pthread_create(&tid,NULL,thread2,NULL);
    pthread_join(tid,&ret);
    printf("thread 2 return,thread 2 id %x,return code:%d\n",tid,*(int*)ret);
    free(ret);

    //thread 3 return
    pthread_create(&tid,NULL,thread3,NULL);
    sleep(3);
    pthread_cancel(tid);
    pthread_join(tid,&ret);
    if(ret == PTHREAD_CANCELED)
    {
    printf("thread 3 return,thread 3 id %x,return code:PTHREAD_CANCELED\n",tid);
    }
    else
    {
    printf("thread 3 return,thread 3 id %x,return code:NULL\n",tid);
    }
}
[root@localhost wait]# gcc wait.c -lpthread
[root@localhost wait]# ./a.out 
thread 1 is returning...
 thread 1 return,thread 1 id 8df44700,return code:1
thread 2 is exiting...
thread 2 return,thread 2 id 8df44700,return code:2
thread 3 is running...
thread 3 is running...
thread 3 is running...
thread 3 return,thread 3 id 8df44700,return code:PTHREAD_CANCELED

线程分离

线程退出后,需要对其进行pthread_join操作,否则无法释放内存,从而造成内存泄漏。所以当线程退出时,可以告诉系统自动释放线程资源。

#include <pthread.h>
int pthread_detach(pthread_t thread);

以上是其他线程对目标线程进行分离

也可以自己分离:

int pthread_detach(pthread_self());

但是要注意:分离后的线程,一旦出现异常,进程也会因异常退出。

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

void *thread_run(void* arg)
{
    pthread_detach(pthread_self());
    printf("%s\n",(char*)arg);
    return NULL;
}

int main()
{
    pthread_t tid;
    if(pthread_create(&tid,NULL,thread_run,"thread1 run..\n") != 0)
    {
        perror("pthread_create");
        return 1;
    }
    int ret = 0;
    sleep(1);
    if(pthread_join(tid,NULL) == 0)
    {
        printf("wait success\n");
        ret = 0;
    }
    else
    {
        printf("wait failed\n");
        ret = 1;
    }
    return ret;
}

[root@localhost wait]# gcc separation.c -lpthread
[root@localhost wait]# ./a.out 
thread1 run..

wait failed

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值