一、线程的操作
定义函数指针数组
typedef void *(*TH)(void *)
TH th[5] = {th1, th2, th3};
线程的控制:互斥与同步
1.互斥 ---> 在多线程中对临界资源的排他性访问(读写操作)。
mutex锁:1为开,0为关
互斥机制 ---> 互斥锁 ---> 保证临界资源的访问控制。
pthread_mutex_t mutex;
互斥锁类型 互斥锁变量 内核对象
框架:
定义互斥锁 ---> 初始化锁 ---> 加锁 ---> 解锁 ---> 销毁
**** *** ***
1)定义:pthread_mutex_t mutex;
2)初始化锁:int pthread_mutex_init(
pthread_mutex_t *mutex,
const pthread_mutexattr_t *attr);
atrr 初始化的值,一般是NULL表示默认锁
返回值:成功 0、失败 非零
3)加锁:int pthread_mutex_lock(pthread_mutex_t *mutex);
4)解锁:int pthread_mutex_unlock(pthread_mutex_t *mutex);
5)销毁:int pthread_mutex_destroy(pthread_mutex_t *mutex);
6)trylock:int pthread_mutex_trylock(pthread_mutex_t *mutex);
功能:类似加锁函数效果,唯一区别就是不阻塞。
在线程的操作中,返回值为0都表示为成功
2.线程的同步 ---> 同步 ---> 有一定先后顺序的对资源的排他性访问。
原因:互斥锁可以控制排他访问但没有次序。
linux下的线程同步 --->信号量机制 --->semaphore.h posix
sem_open();
信号量的分类:
1)无名信号量 --->线程间通信
2)有名信号量 --->进程间通信
框架:信号量的定义--->信号量的初始化 --->信号量的PV操作--->信号量的销毁。
<1>semaphore
信号量的定义 :
sem_t sem;
信号量的类型 信号量的变量
<2>信号量的初始化:int sem_init(sem_t *sem, int pshared, unsigned int value);
功能:将已经定义好的信号量赋值。
参数:sem 要初始化的信号量
pshared = 0 ;表示线程间使用信号量
!=0 ;表示进程间使用信号量
<3>信号量的PV 操作
P --->申请资源--->申请一个二值信号量
V --->释放资源--->释放一个二值信号量
当有多个资源时,可以使用计数信号量
eg:sem_init(&sem_WIN, 0, 3);
P操作对应函数 --->sem_wait();
V操作对应函数 --->sem_post();
int sem_wait(sem_t *sem);
功能:判断当前sem信号量是否有资源可用。
如果sem有资源(==1),则申请该资源,程序继续运行;如果sem没有资源(==0),则线程阻塞等待,一 旦有资源则自动申请资源并继续运行程序。
注意:sem 申请资源后会自动执行 sem = sem - 1;
int sem_post(sem_t *sem);
功能:函数可以将指定的sem信号量资源释放并默认执行,sem = sem+1;
线程在该函数上不会阻塞。
<4>信号量的销毁
int sem_destroy(sem_t *sem);
功能:使用完毕将指定的信号量销毁
参数:sem要销毁的信号量
3.产生死锁的原因主要是:逻辑不对
(1) 因为系统资源不足。
(2) 进程运行推进的顺序不合适。
(3) 资源分配不当等。
如果系统资源充足,进程的资源请求都能够得到满足,死锁出现的可能性就很低,否则
就会因争夺有限的资源而陷入死锁。其次,进程运行推进顺序与速度不同,也可能产生死锁。
产生死锁的四个必要条件:
(1) 互斥条件:一个资源每次只能被一个进程使用。
(2) 请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。
(3) 不剥夺条件:进程已获得的资源,在末使用完之前,不能强行剥夺。
(4) 循环等待条件:若干进线程之间形成一种头尾相接的循环等待资源关系。
二、例题与练习
1.线程互斥---锁
#include <stdio.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
int count = 0;
pthread_mutex_t mutex;
void *th(void *arg)
{
int i = 5000;
while(i--)
{
pthread_mutex_lock(&mutex);
int tmp = count;
printf("%d\n", tmp + 1);
count = tmp + 1;
pthread_mutex_unlock(&mutex);
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t tid1,tid2;
pthread_mutex_init(&mutex, NULL);
pthread_create(&tid1, NULL, th, NULL);
pthread_create(&tid2, NULL, th, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_mutex_destroy(&mutex);
//system("pause");
return 0;
}
2.银行取钱案例-练习
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int WIN = 3;
pthread_mutex_t mutex;
void *th(void *arg)
{
while(1)
{
pthread_mutex_lock(&mutex);
if(WIN > 0)
{
WIN--;
printf("get win\n");
pthread_mutex_unlock(&mutex);
int n = rand() % 5 + 1;
sleep(n);
pthread_mutex_lock(&mutex);
WIN++;
printf("relese win\n");
pthread_mutex_unlock(&mutex);
break;
}
else
{
pthread_mutex_unlock(&mutex);
}
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t tid[10] = {0};
int i;
pthread_mutex_init(&mutex, NULL);
for(i = 0; i < 10; ++i)
{
pthread_create(&tid[i], NULL, th, NULL);
}
for(i = 0; i < 10; ++i)
{
pthread_join(tid[i], NULL);
}
pthread_mutex_destroy(&mutex);
//system("pause");
return 0;
}
3.线程同步--信号量
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <semaphore.h>
sem_t sem_H, sem_W;
void *th1(void *args)
{
int i = 10;
while(i--)
{
sem_wait(&sem_H);
printf("hello ");
fflush(stdout);
sem_post(&sem_W);
}
return NULL;
}
void *th2(void *args)
{
int i = 10;
while(i--)
{
sem_wait(&sem_W);
printf("World\n");
sem_post(&sem_H);
sleep(1);
}
return NULL;
}
int main(int argc, char **argv)
{
pthread_t tid1;
pthread_t tid2;
sem_init(&sem_H, 0, 1);
sem_init(&sem_W, 0, 0);
pthread_create(&tid1, NULL, th1, NULL);
pthread_create(&tid2, NULL, th2, NULL);
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
sem_destroy(&sem_H);
sem_destroy(&sem_W);
//system("pause");
return 0;
}
4.线程:用信号量去解决模拟银行
#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
sem_t sem_WIN;
void *th(void *arg)
{
sem_wait(&sem_WIN);
printf("get win\n");
int n = rand() % 5 + 1;
sleep(n);
printf("relese win\n");
sem_post(&sem_WIN);
return NULL;
}
int main(int argc, char **argv)
{
sem_init(&sem_WIN, 0, 3);
pthread_t tid[10] = {0};
int i;
for (i = 0; i < 10; ++i)
{
pthread_create(&tid[i], NULL, th, NULL);
}
for (i = 0; i < 10; ++i)
{
pthread_join(tid[i], NULL);
}
sem_destroy(&sem_WIN);
// system("pause");
return 0;
}