多线程编程

线程和进程的区别:

进程:

进程是一个具有一定独立功能的程序的一次运行活动,同时也是资源分配的最小单元。

Linux系统是一个多进程的系统,它的进程之间具有并行性、互不干扰等特点。
每个进程都是一个独立的运行单位,拥有各自的权利和责任。其中,各个进程都运行在独立的虚拟地址空间,因此,即使一个进程发生异常,它也不会影响到系统中的其他进程。


线程:

线程是进程的一个执行流,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。
一个进程由几个线程组成(拥有很多相对独立的执行流的用户程序共享应用程序的大部分数据结构),线程与同属一个进程的其他的线程共享进程所拥有的全部资源。

进程有独立的地址空间,一个进程崩溃后,在保护模式下不会对其它进程产生影响,而线程只是一个进程中的不同执行路径。


线程有自己的堆栈和局部变量,但线程没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源较大,效率要差一些。但对于一些要求同时进行并且又要共享某些变量的并发操作,只能用线程,不能用进程。


进程有独立的地址空间,线程没有单独的地址空间(同一进程内的线程共享进程的地址空间)。


线程的优点:

1.和进程相比,它是一种非常“节俭”的多任务操作方式。

运行于一个进程中的多个线程,它们之间使用相同的地址空间,而且线程间彼此切换所需的时间也远远小于进程间切换所需要的时间。


2.线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其它线程所用,这不仅快捷,而且方便。


3.使多CPU系统更加有。。


4.改善程序结构。


多线程:

Linux系统下的多线程遵循POSIX线程接口,称为pthread。编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用libpthread.a


创建:

#include <pthread.h>
int pthread_create(pthread_t * tidp,const pthread_attr_t*attr,void*(*start_rtn)(void),void*arg)

tidp:线程id
attr: 线程属性(通常为空)
start_rtn:线程要执行的函数
arg:start_rtn的参数


编译:

因为pthread的库不是linux系统的库,所以在进行编译的时候要加上-lpthread


线程退出:

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


线程等待:
#include <pthread.h>
int pthread_join(pthread_t tid,void **rval_ptr)

功能:阻塞调用线程,直到指定的线程终止。
Tid :等待退出的线程id
Rval_ptr:线程退出的返回值的指针


进行多线程编程,因为无法知道哪个线程会在哪个时候对共享资源进行操作,因此让如何保护共享资源变得复杂,通过下面这些技术的使用,可以解决线程之间对资源的竞争:

互斥量:

#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t 、*mutex,const pthread_mutexattr_t *attr)
int pthread_mutex_destroy(pthread_mutex_t *mutex)


加锁:

对共享资源的访问, 要使用互斥量进行加锁, 如果互斥量已经上了锁, 调用线程会阻塞, 直到互斥量被解锁。
int pthread_mutex_lock(pthread_mutex_t *mutex)
int pthread_mutex_trylock(pthread_mutex_t *mutex)
返回值: 成功则返回0, 出错则返回错误编号。

解锁:

在操作完成后,必须给互斥量解锁,也就是前面所说的释放。这样其他等待该锁的线程才有机会获得该锁,否则其他线程将会永远阻塞。
int pthread_mutex_unlock(pthread_mutex_t *mutex)


例:

消息队列线程:

互相通信

#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>


#define MSGKEY   1234


struct msgbuf {
long mtype;     /* message type, must be > 0 */
char mtext[100];  /* message data */
};


pthread_t tid[2] = {0};


void *MyReceive(void *arg)
{
struct msgbuf buf;
int ret;


while (1)
{
memset(&buf, 0, sizeof(buf));


ret = msgrcv(*(int *)arg, &buf, sizeof(buf.mtext), 2, 0);
if (-1 == ret)
{
perror("msgrcv");
exit(1);
}


if (!strncmp(buf.mtext, "bye", 3))
{
pthread_cancel(tid[1]);
break;
}
printf("\t\t\t%s\n", buf.mtext);
}
}


void *MySend(void *arg)
{
struct msgbuf buf;
int ret, oldtype;


pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);


while(1)
{
memset(&buf, 0, sizeof(buf));
scanf("%s", buf.mtext);
buf.mtype = 1;


ret = msgsnd(*(int *)arg, &buf, sizeof(buf) - sizeof(long), 0);
if (-1 == ret)
{
perror("msgsnd");
exit(1);
}
if (!strncmp("bye", buf.mtext, 3))
{
buf.mtype = 2;
ret = msgsnd(*(int *)arg, &buf, sizeof(buf) - sizeof(long), 0);
if (-1 == ret)
{
perror("msgsnd");
exit(1);
}
break;
}
}
}




int main()
{
int msgid, ret;
struct msgbuf buf;
pid_t pid;


msgid = msgget(MSGKEY, IPC_CREAT | IPC_EXCL);
if (-1 == msgid)
{
perror("msgget");
exit(1);
}

ret = pthread_create(&tid[0], NULL, MyReceive, (void *)&msgid);
if (0 != ret)
{
perror("pthread_create1");
exit(1);
}


ret = pthread_create(&tid[1], NULL, MySend, (void *)&msgid);
if (0 != ret)
{
perror("pthread_create2");
exit(1);
}


pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);


msgctl(msgid, IPC_RMID, NULL);


return 0;
}



#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>


#define MSGKEY   1234


struct msgbuf {
long mtype;     /* message type, must be > 0 */
char mtext[100];  /* message data */
};


pthread_t tid[2] = {0};


void *MyReceive(void *arg)
{
struct msgbuf buf;
int ret;


while (1)
{
memset(&buf, 0, sizeof(buf));


ret = msgrcv(*(int *)arg, &buf, sizeof(buf.mtext), 1, 0);
if (-1 == ret)
{
perror("msgrcv");
exit(1);
}


if (!strncmp(buf.mtext, "bye", 3))
{
pthread_cancel(tid[1]);
break;
}
printf("\t\t\t%s\n", buf.mtext);
}
}


void *MySend(void *arg)
{
struct msgbuf buf;
int ret, oldtype;


pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);


while(1)
{
memset(&buf, 0, sizeof(buf));
scanf("%s", buf.mtext);
buf.mtype = 2;


ret = msgsnd(*(int *)arg, &buf, sizeof(buf) - sizeof(long), 0);
if (-1 == ret)
{
perror("msgsnd");
exit(1);
}
if (!strncmp("bye", buf.mtext, 3))
{
buf.mtype = 1;
ret = msgsnd(*(int *)arg, &buf, sizeof(buf) - sizeof(long), 0);
if (-1 == ret)
{
perror("msgsnd");
exit(1);
}
break;
}
}
}




int main()
{
int msgid, ret;
struct msgbuf buf;
pid_t pid;


msgid = msgget(MSGKEY, 0);
if (-1 == msgid)
{
perror("msgget");
exit(1);
}

ret = pthread_create(&tid[0], NULL, MyReceive, (void *)&msgid);
if (0 != ret)
{
perror("pthread_create1");
exit(1);
}


ret = pthread_create(&tid[1], NULL, MySend, (void *)&msgid);
if (0 != ret)
{
perror("pthread_create2");
exit(1);
}


pthread_join(tid[0], NULL);
pthread_join(tid[1], NULL);


return 0;
}
            



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值