关于线程的一些东东

1、linux中的中主要用的是进程,对线程的操作优势不如windows

2、主要以pthread_开头,在用gcc来build时要加上-lpthread选项。

3、头文件在<pthread.h>

4、一个进程中的大部分东西都是多个线程共享的,但是一下东西在每个线程是独立的:线程id号,寄存器集合,用于保存局部变量和返回地址的堆栈,errno,信号掩码,运行的优先级。

5、5个函数:

a、int pthread_create(pthread_t *tid, const pthread_attr_t *attr, void*(*func)(void*), void *arg); 相当于fork函数。

注意成功返回0,线程id通过tid返回。

一般情况下,attr只需要传递空指针即可,(属性用系统默认的就足够了)

注意:func是线程调用的函数,参数是通用指针void*,返回也是通用指针void*,arg是该函数的参数。

b、int pthread_join(pthread_t tid, void **status); 相当于waitpid函数

等待tid线程终止,如果status非空,则tid线程终止的返回值将传到*status地方。

c、pthread_t pthread_self(void); 相当于getpid函数。

d、int ptrhead_detach(pthread_t tid)

线程状态或者是可汇合的(joinable),或者是可脱离的(detached)(默认为可汇合的)

脱离的线程像守护进程(daemon):所有的资源将会被释放,因此不能用pthread_join等待其终止。

一般用法,使自己脱离:pthread_detach(pthread_self());

e、void pthread_exit(void *status)

如果该线程未脱离,则其线程id和推出状态一直保留到某个线程调用pthread_join为止。
终止线程的其他情况:

启动线程的func函数调用return 或者是 本进程的main函数返回 或者 某个线程调用了exit(这种情况本进程及其所有线程都终止)

ok, 还是贴一下pthread常用的几个方法,以免遗忘了。

#include <pthread.h>

 

int pthread_create(struct pthread_attr_t *, pthread_t *, void *f*(void *), void *);

    创建一个线程,f为线程函数。默认情况下线程属性为joinable,即当主线程退出时,线程也会退出。

int pthread_join(pthread_t, void **);

    主线程阻塞直到这个thread结束后才继续往下走。这里第二个参数接收线程退出的返回值。

int pthread_exit(void);

    线程自己主动退出。

int pthread_cancel(pthread_t);

    被动退出。一个线程结束另外一个线程的运行。这里包括同步退出(默认),即线程立刻结束。

    以及异步退出,即线程直到下一个cancellation point才退出。线程可以设置这样的cancellation point,

    当然也可以设置为不被其他线程命令退出。

int pthread_self(void);

    得到自己的thread id。

----------------------------------------

 

编译时需要加上-lpthread选项。

另外,发现c99不允许在for中直接声明变量,厄。。。

---------------------------------------------------------------------------------------------------------------

很久很久以前,我对C语言的了解并不是很多,我最早听说多线程编程是用Java,其实C语言也有多线程编程,而且更为简单、方便、强大。下面就让我们简单领略一下Unix C语言环境下的多线程编程吧!
下面先看一个简单的单线程程序:
/* 06.3.6
   Sghello.c
   Hello,world -- Single Thread
*/
#include<stdio.h>
#define NUM 6
int main()
{
    void print_msg(char*);
    print_msg("hello,");
    print_msg("world!");
}
void print_msg(char* m)
{
    int i;
    for(i=0;i<NUM;i++)
    {
        printf("%s",m);
        fflush(stdout);
        sleep(1);
    }
}
下图反映了程序的执行流程:
程序执行路线
执行结果:
$ ./sghello.exe
hello,hello,hello,hello,hello,hello,world!world!world!world!world!world!
 
那么如果想同时执行两个对于print_msg函数的调用,就想使用fork建立两个新的进程一样,那该怎么办?这种思想清楚的体现在下图:
多线程版本的执行路线
那么怎么才能达到这种效果呢?
我们可以使用函数pthread_create创建一个新的线程。
函数原型:
int pthread_create(pthread_t          *thread,
pthread_attr_t *attr,
void               *(*func)(void*),
void               *arg);
参数:                      thread     指向pthread_t类型变量的指针
                             attr         指向pthread_attr_t类型变量的指针,或者为NULL
                             func        指向新线程所运行函数的指针
                             arg          传递给func的参数
返回值                         0            成功返回
                                  errcode    错误
我们可以使用函数pthread_join等待某进程结束。
函数原型:int pthread_join(pthread_t thread,void ** retval);
参数:      thread         所等待的进程
              retval          指向某存储线程返回值的变量
返回值: 0                 成功返回
             errorcode    错误
以上两个函数都包含在头文件pthread.h中。
       下面请看多线程版的Hello,world!
/* 06.3.6
   Mhello1.c
   Hello,world -- Multile Thread
*/
#include<stdio.h>
#include<pthread.h>
#define NUM 6
int main()
{
    void print_msg(void*);
   
    pthread_t t1,t2;
    pthread_create(&t1,NULL,print_msg,(void *)"hello,");
    pthread_create(&t2,NULL,print_msg,(void *)"world!\n");
   
    pthread_join(t1,NULL);
    pthread_join(t2,NULL);  
}
void print_msg(void* m)
{
    char *cp=(char*)m;
    int i;
    for(i=0;i<NUM;i++)
    {
        printf("%s",m);
        fflush(stdout);
        sleep(1);
    }
}
运行结果:
$ gcc mhello1.c -o mhello1.exe
$ ./mhello1.exe
hello,world!
hello,world!
hello,world!
hello,world!
hello,world!
hello,world!
 
C语言有一次拓展了我的视野,多线程的问题还有很多,像线程间的分工合作、使用互斥机制保证线程间数据的安全共享、使用条件变量同步线程间的数据传输、传递多个参数给线程等,若读者有兴趣,可自行深入。
推荐《Understanding Unix/Linux Programming》





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值