我是靠谱客的博主 土豪西装,最近开发中收集的这篇文章主要介绍linux多线程编程--学习笔迹4,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

进程——资源分配的最小单位,线程——程序执行的最小单位
进程有独立的地址空间,一个进程崩溃后,在保护模式下不会对其它进程产生影响,而线程只是一个进程中的不同执行路径。线程有自己的堆栈和局部变量,但线程没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源较大,效率要差一些。但对于一些要求同时进行并且又要共享某些变量的并发操作,只能用线程,不能用进程

1、创建线程

原型:

#include <pthread.h>
int pthread_create(pthread_t *restrict tidp, const pthread_attr_t *restrict attr, void *(*start_rtn)(void *), void *restrict arg);
// 返回:若成功返回0,否则返回错误编号
#include <pthread.h>
int pthread_exit(void *rval_ptr);
#include <pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr);
// 返回:若成功返回0,否则返回错误编号

例1

#include"stdio.h"
#include"pthread.h"
void *func1(void *arg)
{
printf("second: %ld thread is createn",(unsigned long)pthread_self());
printf("param is %dn",*((int *)arg));
}
int main()
{
int ret;
pthread_t t1;
int param=100;
ret =pthread_create(&t1,NULL,func1,(void *)&param);
if(ret==0)
{
printf("create is successn");
}
printf("main:%ld thread is createn",(unsigned long)pthread_self());
while(1);
return 0;
}

例2:

#include"stdio.h"
#include"pthread.h"
void *func1(void *arg)
{
static int ret=10;
printf("second: %ld thread is createn",(unsigned long)pthread_self());
printf("param is %dn",*((int *)arg));
pthread_exit((void *)&ret);
}
int main()
{
int ret;
pthread_t t1;
int param=100;
int *pret;
ret =pthread_create(&t1,NULL,func1,(void *)&param);
if(ret==0)
{
printf("create is successn");
}
printf("main:%ld thread is createn",(unsigned long)pthread_self());
//
while(1);
pthread_join(t1,(void **)&pret);
printf("main:t1 quit:%dn",*pret);
return 0;
}

共享内存验证
例3

#include"stdio.h"
#include"pthread.h"
#include"stdlib.h"
#include"unistd.h"
int g_data =0;
void *func1(void *arg)
{
static int ret=10;
printf("second: %ld thread is createn",(unsigned long)pthread_self());
printf("param is %dn",*((int *)arg));
while(1)
{
printf("t1: %dn",g_data++);
sleep(1);
}
pthread_exit((void *)&ret);
}
void *func2(void *arg)
{
static int ret=10;
printf("second: %ld thread is createn",(unsigned long)pthread_self());
printf("param is %dn",*((int *)arg));
while(1)
{
printf("t2: %dn",g_data++);
sleep(1);
}
pthread_exit((void *)&ret);
}
int main()
{
int ret;
pthread_t t1;
pthread_t t2;
int param=100;
int *pret;
ret =pthread_create(&t1,NULL,func1,(void *)&param);
if(ret==0)
{
printf("create t1 successn");
}
ret =pthread_create(&t2,NULL,func2,(void *)&param);
if(ret==0)
{
printf("create t2 successn");
printf("main:%ld thread is createn",(unsigned long)pthread_self());
}
while(1)
{
printf("main:%dn",g_data++);
sleep(1);
}
pthread_join(t1,(void **)&pret);
pthread_join(t2,(void **)&pret);
printf("main:t1 quit:%dn",*pret);
return 0;
}

2、互斥锁

原型:

#include <pthread.h>
int pthread_mutex_init(pthread_mutex_t *restrict mutex, const pthread_mutexattr_t *restrict attr);
int pthread_mutex_destroy(pthread_mutex_t *mutex);
// 返回:若成功返回0,否则返回错误编号
#include <pthread.h>
int pthread_mutex_lock(pthread_mutex_t mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
int pthread_mutex_unlock(pthread_mutex_t *mutex);
// 返回:若成功返回0,否则返回错误编号

例4

#include"stdio.h"
#include"pthread.h"
#include"stdlib.h"
#include"unistd.h"
int g_data =0;
pthread_mutex_t mutex;
void *func1(void *arg)
{
static int ret=10;
printf("second: %ld thread is createn",(unsigned long)pthread_self());
printf("param is %dn",*((int *)arg));
pthread_mutex_lock(&mutex);
while(1)
{
printf("t1: %dn",g_data++);
if(g_data==3)
{
pthread_mutex_unlock(&mutex);
printf("t1 quit============");
pthread_exit(NULL);
}
}
}
void *func2(void *arg)
{
static int ret=10;
printf("second: %ld thread is createn",(unsigned long)pthread_self());
printf("param is %dn",*((int *)arg));
while(1)
{
printf("t2: %dn",g_data);
pthread_mutex_lock(&mutex);
g_data++;
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
int ret;
pthread_t t1;
pthread_t t2;
int param=100;
int *pret;
pthread_mutex_init(&mutex,NULL);
ret =pthread_create(&t1,NULL,func1,(void *)&param);
if(ret==0)
{
printf("create t1 successn");
}
ret =pthread_create(&t2,NULL,func2,(void *)&param);
if(ret==0)
{
printf("create t2 successn");
printf("main:%ld thread is createn",(unsigned long)pthread_self());
}
while(1)
{
printf("main:%dn",g_data++);
sleep(1);
}
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_mutex_destroy(&mutex);
return 0;
}

死锁的情况:
例5

#include"stdio.h"
#include"pthread.h"
#include"stdlib.h"
#include"unistd.h"
int g_data =0;
pthread_mutex_t mutex;
pthread_mutex_t mutex2;
void *func1(void *arg)
{
static int ret=10;
printf("second: %ld thread is createn",(unsigned long)pthread_self());
printf("param is %dn",*((int *)arg));
pthread_mutex_lock(&mutex);
sleep(1);
pthread_mutex_lock(&mutex2);
while(1)
{
printf("t1: %dn",g_data++);
if(g_data==3)
{
pthread_mutex_unlock(&mutex);
printf("t1 quit============");
pthread_exit(NULL);
}
}
}
void *func2(void *arg)
{
static int ret=10;
pthread_mutex_lock(&mutex2);
sleep(1);
pthread_mutex_lock(&mutex);
printf("second: %ld thread is createn",(unsigned long)pthread_self());
printf("param is %dn",*((int *)arg));
printf("t2: %dn",g_data);
g_data++;
pthread_mutex_unlock(&mutex);
}
int main()
{
int ret;
pthread_t t1;
pthread_t t2;
int param=100;
int *pret;
pthread_mutex_init(&mutex,NULL);
pthread_mutex_init(&mutex2,NULL);
ret =pthread_create(&t1,NULL,func1,(void *)&param);
if(ret==0)
{
printf("create t1 successn");
}
ret =pthread_create(&t2,NULL,func2,(void *)&param);
if(ret==0)
{
printf("create t2 successn");
printf("main:%ld thread is createn",(unsigned long)pthread_self());
}
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&mutex2);
return 0;
}

3、条件

#include <pthread.h>
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr);
int pthread_cond_destroy(pthread_cond_t *cond);
// 返回:若成功返回0,否则返回错误编号
#include <pthread.h>
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);
// 返回:若成功返回0,否则返回错误编号
#include <pthread.h>
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);
// 返回:若成功返回0,否则返回错误编号

例6:

#include"stdio.h"
#include"pthread.h"
#include"stdlib.h"
#include"unistd.h"
int g_data =0;
pthread_mutex_t mutex;
pthread_cond_t cond;
void *func1(void *arg)
{
static int ret=10;
static int cnt=0;
printf("second: %ld thread is createn",(unsigned long)pthread_self());
printf("param is %dn",*((int *)arg));
while(1)
{
pthread_cond_wait(&cond,&mutex);
printf("t1 run============");
printf("t1: %dn",g_data);
g_data=0;
sleep(1);
if(cnt++==10)
{
exit(1);
}
}
}
void *func2(void *arg)
{
static int ret=10;
printf("second: %ld thread is createn",(unsigned long)pthread_self());
printf("param is %dn",*((int *)arg));
while(1)
{
printf("t2: %dn",g_data);
pthread_mutex_lock(&mutex);
g_data++;
if(g_data==3)
{
pthread_cond_signal(&cond);
}
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
int ret;
pthread_t t1;
pthread_t t2;
int param=100;
int *pret;
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond,NULL);
ret =pthread_create(&t1,NULL,func1,(void *)&param);
if(ret==0)
{
printf("create t1 successn");
}
ret =pthread_create(&t2,NULL,func2,(void *)&param);
if(ret==0)
{
printf("create t2 successn");
printf("main:%ld thread is createn",(unsigned long)pthread_self());
}
pthread_join(t1,NULL);
pthread_join(t2,NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}

最后

以上就是土豪西装为你收集整理的linux多线程编程--学习笔迹4的全部内容,希望文章能够帮你解决linux多线程编程--学习笔迹4所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部