Linux 多线程的概念
1.线程的创建
#include <pthread.h>
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
2.线程的退出
#include <pthread.h>
int pthread_exit(void *rval_ptr);
3.线程的等待
#include <pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr);
4.线程ID获取
#include <pthread.h>
pthread_t pthread_self(void);
函数应用
#include <stdio.h>
#include <pthread.h>
void *func1(void *arg)
{
static char *p = "t1 run out\n";
printf("t1:%ld,this is create\n",(unsigned long)pthread_self());
printf("t1:parm is:%d\n",*(int *)arg);
pthread_exit((void *)p);
}
int main()
{
int ret;
pthread_t t1;
int parm = 100;
char *pret;
ret = pthread_create(&t1, NULL, func1, (void *)&parm);
printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1, (void **)&pret);
printf("main:t1 quit:%s\n",pret);
return 0;
}
验证共享内存
#include <stdio.h>
#include <pthread.h>
int data = 0;
void *func1(void *arg)
{
static char *p = "t1 run out\n";
printf("t1:%ld,this is create\n",(unsigned long)pthread_self());
printf("t1:parm is:%d\n",*(int *)arg);
while(1){
printf("t1:data = %d\n",data++);
sleep(1);
}
pthread_exit((void *)p);
}
void *func2(void *arg)
{
static char *p = "t2 run out\n";
printf("t2:%ld,this is create\n",(unsigned long)pthread_self());
printf("t2:parm is:%d\n",*(int *)arg);
while(1){
printf("t2:data = %d\n",data++);
sleep(1);
}
pthread_exit((void *)p);
}
int main()
{
int ret;
int ret2;
pthread_t t1;
pthread_t t2;
int parm = 100;
char *pret;
ret = pthread_create(&t1, NULL, func1, (void *)&parm);
ret2 = pthread_create(&t2, NULL, func2, (void *)&parm);
printf("main:%ld\n",(unsigned long)pthread_self());
while(1){
printf("main:data = %d\n",data++);
sleep(1);
}
pthread_join(t1, (void **)&pret);
printf("main:t1 quit:%s\n",pret);
return 0;
}
5.创建锁
#include <pthread.h>
pthread_mutex_t mutex;
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
6.销毁锁
#include <pthread.h>
int pthread_mutex_destroy(pthread_mutex_t *mutex);
7.加锁
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
8.解锁
int pthread_mutex_unlock(pthread_mutex_t *mutex);
9.函数应用
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex;
void *func1(void *arg)
{
int i;
pthread_mutex_lock(&mutex);
for(i=0;i<5;i++){
printf("t1:%ld,this is create\n",(unsigned long)pthread_self());
printf("t1:parm is:%d\n",*(int *)arg);
sleep(1);
}
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
void *func2(void *arg)
{
pthread_mutex_lock(&mutex);
printf("t2:%ld,this is create2\n",(unsigned long)pthread_self());
printf("t2:parm is:%d\n",*(int *)arg);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main()
{
int ret;
pthread_t t1;
pthread_t t2;
int parm = 100;
pthread_mutex_init(&mutex, NULL);
ret = pthread_create(&t1, NULL, func1, (void *)&parm);
ret = pthread_create(&t2, NULL, func2, (void *)&parm);
printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
10.互斥锁(实现t1获取到3)
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex;
int data = 0;
void *func1(void *arg)
{
pthread_mutex_lock(&mutex);
while(1){
printf("t1:%ld,this is create\n",(unsigned long)pthread_self());
printf("t1:parm is:%d\n",*(int *)arg);
printf("t1 data = %d\n",data++);
sleep(1);
if(data == 3){
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
}
}
void *func2(void *arg)
{
while(1){
pthread_mutex_lock(&mutex);
printf("t2:%ld,this is create2\n",(unsigned long)pthread_self());
printf("t2:parm is:%d\n",*(int *)arg);
printf("t2 data = %d\n",data);
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}
int main()
{
int ret;
pthread_t t1;
pthread_t t2;
int parm = 100;
pthread_mutex_init(&mutex, NULL);
ret = pthread_create(&t1, NULL, func1, (void *)&parm);
ret = pthread_create(&t2, NULL, func2, (void *)&parm);
printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
这样可以实现,不管是t1还是t2线程先拿到锁,t1线程都可以遍历到3再解锁。
11.死锁
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex;
pthread_mutex_t mutex2;
void *func1(void *arg)
{
int i;
pthread_mutex_lock(&mutex);
sleep(1);
pthread_mutex_lock(&mutex2);
for(i=0;i<5;i++){
printf("t1:%ld,this is create\n",(unsigned long)pthread_self());
printf("t1:parm is:%d\n",*(int *)arg);
sleep(1);
}
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
void *func2(void *arg)
{
pthread_mutex_lock(&mutex2);
sleep(1);
pthread_mutex_lock(&mutex);
printf("t2:%ld,this is create2\n",(unsigned long)pthread_self());
printf("t2:parm is:%d\n",*(int *)arg);
pthread_mutex_unlock(&mutex);
pthread_exit(NULL);
}
int main()
{
int ret;
pthread_t t1;
pthread_t t2;
int parm = 100;
pthread_mutex_init(&mutex, NULL);
pthread_mutex_init(&mutex2, NULL);
ret = pthread_create(&t1, NULL, func1, (void *)&parm);
ret = pthread_create(&t2, NULL, func2, (void *)&parm);
printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&mutex);
return 0;
}
有两个锁时,当线程1获取到一把锁时,想要获取另一把锁,但是另一把锁被线程2获取,同时线程2想要获取线程1获取的锁,两个线程相互竞争对方获取的锁,从而形成死锁。
12.条件
- 创建
pthread_cond_t cond;
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
2.销毁
int pthread_cond_destroy(pthread_cond_t *cond);
3.等待
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, cond struct timespec *restrict timeout);
4.触发
int pthread_cond_signal(pthread_cond_t *cond);
5.广播
int pthread_cond_broadcast(pthread_cond_t *cond);
13.条件示例
#include <stdio.h>
#include <pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int data = 0;
void *func1(void *arg)
{
while(1){
pthread_cond_wait(&cond, &mutex);
printf("t1:%ld,this is create\n",(unsigned long)pthread_self());
printf("t1:parm is:%d\n",*(int *)arg);
printf("t1 data = %d=====================\n",data);
data = 0;
sleep(1);
}
}
void *func2(void *arg)
{
while(1){
pthread_mutex_lock(&mutex);
printf("t2:%ld,this is create2\n",(unsigned long)pthread_self());
printf("t2:parm is:%d\n",*(int *)arg);
printf("t2 data = %d\n",data++);
if(data == 3){
pthread_cond_signal(&cond);
}
pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}
int main()
{
int ret;
pthread_t t1;
pthread_t t2;
int parm = 100;
ret = pthread_create(&t1, NULL, func1, (void *)&parm);
ret = pthread_create(&t2, NULL, func2, (void *)&parm);
printf("main:%ld\n",(unsigned long)pthread_self());
pthread_join(t1, NULL);
pthread_join(t2, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
14.程序测试流程
1.创建测试文件
int main(int argc,char **argv)
{
int time = atoi(argv[1]);
int i;
for(i = 0;i < time;i++){
system("./test");
}
}
- 编辑它
- ./a.out 10 >>test.txt & //把要跑的程序,跑10次,存放到test.txt文件中
- 查阅test.txt文件,查看结果 。
15.生产者与消费者的问题
生产者与消费者的问题
|