2019.4.22 祈愿我们的软件杯最终能有一个好结果
线程创建和等待
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
void *print_a(void *a){
for(int i = 0; i < 10; i++)
{
puts("aa\n");
}
return NULL;
}
void *print_b(void *b){
for(int i = 0; i < 10; i++)
{
puts("bb\n");
}
return NULL;
}
int main()
{
pthread_t t0;
pthread_t t1;
if(pthread_create(&t0, NULL, print_a, NULL) != -1){
puts("fail to create pthread t0\n");
}
if(pthread_create(&t1, NULL, print_b, NULL) != -1){
puts("fail to create pthread t1\n");
}
void *result;
if(pthread_join(t0, &result)!=-1)
{
puts("ok to recollect t0\n");
}
if(pthread_join(t1, &result)!=-1){
puts("ok to recollect t1\n");
}
return 0;
}
头文件:<pthread.h>
1.线程创建:pthread_create(&pthread, NULL, func, NULL) ;参数为,被创建的线程变量名,空指针,创建函数:
2.线程等待:pthread_join(&pthread, &result);等待的线程, 返回值(void*);
pthread_create创建成功后,两个线程分别开始运行,但是两个线程的运行时间先后是未知的,这取决于系统的状态。join可以指定运行哪个线程,此时系统阻塞等待该线程运行结束,并且在运行结束后收集该线程的内存空间
线程同步和互斥
pthread_mutex_t mutex;//声明一个锁
pthread_mutex_init(&mutex, NULL);//初始化锁为解锁状态
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;//编译时初始化锁为解锁状态
pthread_mutex_lock(&mutex);//访问对象加锁
pthread_mutex_unlock(&mutex);//访问对象解锁
这是一个线程互相竞争同一个变量导致错误的例子
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
int share = 0;
void in(){
long i ,tmp;
for(int i = 0; i <= 100000; i++)
{
tmp = share;
tmp = tmp+1;
share = tmp;
}
}
int main()
{
int ret;
pthread_t thrd1, thrd2, thrd3;
ret = pthread_create(&thrd1, NULL, (void *)in, NULL);
ret = pthread_create(&thrd2, NULL, (void *)in, NULL);
ret = pthread_create(&thrd3, NULL, (void *)in, NULL);
pthread_join(thrd1, NULL);
pthread_join(thrd2, NULL);
pthread_join(thrd3, NULL);
printf("share=%d\n",share);
return 0;
}
每一次运行结果都是不一样的,thrd123相互竞争share变量,导致错误。
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
int share = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void in(){
long i ,tmp;
for(int i = 1; i <= 500; i++)
{
//加锁,数据同步,保证只有一个线程能够取到变量的值
pthread_mutex_lock(&mutex);
tmp = share;
tmp = tmp+1;
share = tmp;
//解锁
pthread_mutex_unlock(&mutex);
}
}
int main()
{
int ret;
pthread_t thrd1, thrd2, thrd3;
ret = pthread_create(&thrd1, NULL, (void *)in, NULL);
ret = pthread_create(&thrd2, NULL, (void *)in, NULL);
ret = pthread_create(&thrd3, NULL, (void *)in, NULL);
pthread_join(thrd1, NULL);
pthread_join(thrd2, NULL);
pthread_join(thrd3, NULL);
printf("share=%d\n",share);
return 0;
}
进入区域加同一把锁,退出区域解同一把锁
加锁以后运行速度明显变慢
这个代码不能执行,我还不明白为什么
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
#define max 10
int stack[max];
int size = 0;
sem_t sem;
void pro()
{
int i;
for(int i = 0; i < max; i++)
{
stack[i] = i;
sem_post(&sem);
}
}
void con()
{
int i;
while((i=size++)<max)
{
sem_wait(&sem);
printf("乘法:%d * %d = %d\n",stack[i], stack[i]*stack[i]);
}
}
int main()
{
pthread_t proer, coner;
sem_init(&sem, 0, 0);
pthread_create(&proer, NULL, (void *)con, NULL);
pthread_create(&coner, NULL, (void *)con, NULL);
pthread_join(proer, NULL);
pthread_join(coner, NULL);
sem_destroy(&sem);
return 0;
}