线程是进程的一个执行流,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。 一个进程由几个线程组成(拥有很多相对独立的执行流的用户程序共享应用程序的大部分数据结构),线程与同属一个进程的其他的线程共享进程所拥有的全部资源。
"进程——资源分配的最小单位,线程——程序执行的最小单位" 进程有独立的地址空间,一个进程崩溃后,在保护模式下不会对其它进程产生影响,而线程只是一个进程中的不同执行路径。
线程有自己的堆栈和局部变量,但线程没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源较大,效率要差一些。但对于一些要求同时进行并且又要共享某些变量的并发操作,只能用线程,不能用进程。
进程有独立的地址空间,线程没有单独的地址空间(同一进程内的线程共享进程的地址空间)。
线程的创建:
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
void delay()
{
int x = 10000, y;
while (x > 0)
{
y = 20000;
while (y > 0)
{
y--;
}
x--;
}
}
pthread_t tid1, tid2;
void *Mythread(void *arg)
{
//pthread_detach(pthread_self()); //线程分离,线程运行结束后自动释放资源
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
printf("hello MyThread!\n");
delay();
pthread_exit((void *)100); //线程退出
}
void *Mythread2(void *arg)
{
printf("%s\n", (char *)arg);
pthread_cancel(tid1);
//exit(1); //进程退出
}
int main()
{
int ret;
ret = pthread_create(&tid1, NULL, Mythread, NULL);
if (ret != 0)
{
perror("pthread_create");
exit(1);
}
ret = pthread_create(&tid2, NULL, Mythread2, "helloworld");
if (ret != 0)
{
perror("pthread_create");
exit(1);
}
void *status;
ret = pthread_join(tid1, &status); //1、等待 2、回收资源
if (ret != 0)
{
perror("pthread_join");
}
printf("Mythread1 exit with %d\n", status);
ret = pthread_join(tid2, &status);
if (ret != 0)
{
perror("pthread_join");
}
return 0;
}
实现两个线程之间的互相收发:
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#define MSGKEY 1234
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[64]; /* message data */
};
pthread_t tid1, tid2;
void *SendHandler(void *arg)
{
int ret;
struct msgbuf mbuf;
int msgid = *(int *)arg;
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
while (1)
{
memset(&mbuf, 0, sizeof(mbuf));
mbuf.mtype = 1; //消息类型
scanf("%s", mbuf.mtext);
ret = msgsnd(msgid, &mbuf, sizeof(mbuf.mtext), 0);
if (-1 == ret)
{
perror("msgsnd");
exit(1);
}
if (!strcmp(mbuf.mtext, "bye"))
{
pthread_cancel(tid2);
break;
}
}
}
void *RecvHandler(void *arg)
{
int ret;
struct msgbuf mbuf;
int msgid = *(int *)arg;
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
while (1)
{
memset(&mbuf, 0, sizeof(mbuf));
ret = msgrcv(msgid, &mbuf, sizeof(mbuf.mtext), 2, 0);
if (-1 == ret)
{
perror("msgrcv");
exit(1);
}
if (!strcmp(mbuf.mtext, "bye"))
{
pthread_cancel(tid1);
break;
}
printf("\t\t\t%s\n", mbuf.mtext);
memset(&mbuf, 0, sizeof(mbuf));
}
}
int main()
{
int ret;
int msgid = msgget(MSGKEY, IPC_CREAT | IPC_EXCL);
if (-1 == msgid)
{
perror("msgget");
exit(1);
}
ret = pthread_create(&tid1, NULL, SendHandler, &msgid); //发送线程
if (ret != 0)
{
perror("pthread_create");
exit(1);
}
ret = pthread_create(&tid2, NULL, RecvHandler, &msgid); //接收线程
if (ret != 0)
{
perror("pthread_create");
exit(1);
}
void *status;
pthread_join(tid1, &status);
pthread_join(tid2, &status);
sleep(1);
msgctl(msgid, IPC_RMID, NULL);
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#define MSGKEY 1234
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[64]; /* message data */
};
pthread_t tid1, tid2;
void *SendHandler(void *arg)
{
struct msgbuf mbuf;
int ret;
int msgid = *(int *)arg;
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
while (1)
{
memset(&mbuf, 0, sizeof(mbuf));
mbuf.mtype = 2; //消息类型
scanf("%s", mbuf.mtext);
ret = msgsnd(msgid, &mbuf, sizeof(mbuf.mtext), 0);
if (-1 == ret)
{
perror("msgsnd");
exit(1);
}
if (!strcmp(mbuf.mtext, "bye"))
{
pthread_cancel(tid2);
break;
}
}
}
void *RecvHandler(void *arg)
{
struct msgbuf mbuf;
int ret;
int msgid = *(int *)arg;
int old;
pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
while (1)
{
memset(&mbuf, 0, sizeof(mbuf));
ret = msgrcv(msgid, &mbuf, sizeof(mbuf.mtext), 1, 0);
if (-1 == ret)
{
perror("msgrcv");
exit(1);
}
if (!strcmp(mbuf.mtext, "bye"))
{
pthread_cancel(tid1);
break;
}
printf("\t\t\t%s\n", mbuf.mtext);
memset(&mbuf, 0, sizeof(mbuf));
}
}
int main()
{
int ret;
int msgid = msgget(MSGKEY, 0);
if (-1 == msgid)
{
perror("msgget");
exit(1);
}
ret = pthread_create(&tid1, NULL, SendHandler, &msgid); //发送线程
if (ret != 0)
{
perror("pthread_create");
exit(1);
}
ret = pthread_create(&tid2, NULL, RecvHandler, &msgid); //接收线程
if (ret != 0)
{
perror("pthread_create");
exit(1);
}
void *status;
pthread_join(tid1, &status);
pthread_join(tid2, &status);
return 0;
}