线程创建(六)

总结

1.任务二:挂起线程中红色处应传地址然后类型强制转换(void*)&i
c语言好像可以按图中的
2.报错/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
原因:拼写错误main->写成mian了
3..test4.c.swp交换文件已存在
原因:vim编写时会产生对应的.swp文件,正常退出会删除,非正常退出会保存
解决方法:例子(test4.c)         rm -f .test4.c.swp
4.sleep需要添加头文件#include<unistd.h>

在这里插入图片描述



实验六 线程创建

说明:关于线程的常用函数

1)线程创建

int pthread_create( pthread_t *thread, pthread_attr_t *attr,

void *(*start_routine)(void *),void *arg );

返回值:成功-返回0,失败-返回错误编号

参数:

  thread:所创建的线程号。

  attr:所创建的线程属性。

  start_routine:即将运行的线程函数。

  arg:传递给线程函数的参数。

2)线程挂起:

int pthread_join(pthread_t th, void **thread_retrun);

返回值:成功-返回0,失败-返回错误编号

参数:

th:线程号;

thread_retrun:用户定义的指针,用来存储被等待线程的返回值

作用:挂起当前线程直到由参数th指定的线程被终止为止。

任务一:创建一个简单的线程

请添加图片描述

#include<stdio.h>
#include<sys/types.h>
#include<pthread.h>
int data;
void sub1(void)
{
        data+=1;
        printf("SubThread-->%d\n",data);
}
int main(void)
{
        data=4;
        pthread_t pthread1;
        pthread_create(&pthread1,NULL,(void*)sub1,NULL);
        pthread_join(pthread1,NULL);
        data+=1;
        printf("MainThread-->data=%d\n",data);
        return 0;
}

在这里插入图片描述

任务二:挂起线程

请添加图片描述

#include<pthread.h>
#include<unistd.h>
#include<stdio.h>
void *thread(void *str)
{
        int i;
        for(i=0;i<10;++i)
        {sleep(2);
                printf("This in the thread:%d\n",i);}
                return NULL;
}
int main(){
        pthread_t pth;
        int i;
        int ret=pthread_create(&pth,NULL,thread,(void*)&i);
        pthread_join(pth,NULL);
        for(i=0;i<10;++i)
        {sleep(1);
        printf("This in the main:%d\n",i);
        }
        return 0;
}

注释前
在这里插入图片描述
注释后
在这里插入图片描述

任务三:线程互斥

请添加图片描述
请添加图片描述
请添加图片描述

#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#include<unistd.h>
#define MAX 10
pthread_t thread[2]; //两个线程
pthread_mutex_t mut;
int number=0;
int i;
void *thread1()
{ 
	printf("thread 1 : I'm thread 1\n");	
	for (i = 0; i < MAX; i++)	
	{	
		printf( "thread 1 : number = %d\n",number);
		pthread_mutex_lock(&mut);
		number++;
		pthread_mutex_unlock(&mut);
		sleep(2); }
	printf("thread1: this is in thread 1 \n");
	pthread_exit(NULL);
}
void *thread2()
{ printf("thread2 : I'm thread 2\n");
	for(i = 0;i<MAX; i++)
	{ printf("thread2 : number = %d\n",number);
	pthread_mutex_lock(&mut);
	number++;
	pthread_mutex_unlock(&mut);
	sleep(3); }
	printf("thread2 :this is thread2\n");
	pthread_exit(NULL); }
void thread_create(void) //创建两个线程
{ 	int temp;
	memset(&thread,0,sizeof(thread));
	/*创建线程*/
	if((temp = pthread_create(&thread[0], NULL,thread1, NULL)) != 0)
	printf("create fhiled\n");
	else printf("threadl is created\n");
	if((temp = pthread_create(&thread[1], NULL,thread2, NULL)) != 0)
	printf(" create failed\n ");
	else printf("thread2 is created\n"); }
void thread_wait(void)
{	/*等荐获程结束*/
	if(thread[0] !=0)
	{ 	//comment4
		pthread_join(thread[0],NULL); 
		printf("thread1 is done\n");
	}
	if(thread[1] !=0)
	{ 	//comment5 
		pthread_join(thread[1],NULL); 
		printf("thread2 is done\n");
	}
}
int main()
{
	/*用默认属性初始化互斥锁*/ 
	pthread_mutex_init(&mut,NULL); 
	printf("this is the main function\n"); 
	thread_create();
	printf(" this is the main function\n ");
	thread_wait();
	return 0;
}

在这里插入图片描述

任务四:线程综合实验

请添加图片描述

#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
int a=200;
int b=100;
pthread_mutex_t lock;
void *ThreadA(){
        pthread_mutex_lock(&lock);
        a-=50;
        sleep(5);
        b+=50;
        pthread_mutex_unlock(&lock);
}
void *ThreadB()
{
        sleep(1);
        pthread_mutex_lock(&lock);
        printf("%d\n",a+b);
        pthread_mutex_unlock(&lock);
}
int main(){
        pthread_t t1,t2;
        pthread_mutex_init(&lock,NULL);
        pthread_create(&t1,NULL,ThreadA,NULL);
        pthread_create(&t1,NULL,ThreadB,NULL);
        pthread_join(t1,NULL);
        pthread_join(t2,NULL);
        return 1;
}

注释前
在这里插入图片描述
注释后
在这里插入图片描述

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值