111. 多线程 - 主线程

这里的12/34/67 是字符串

book@100ask:~/C_coding/0122$ cat main_thread.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct student{
	int age;
	char name[20];
	char id[4];
};

void *thread_fun(void *stu)
{
	printf("student age is %d\n,name is %s\n,id is %s\n",((struct student *)stu)->age,((struct student *)stu)->name,((struct student *)stu)->id);
	return (void *)0;
}

int main(int argc,char *argv[])
{
	pthread_t tid;
	int err;

	struct student stu;
	stu.age = 20;
	memcpy(stu.name,"zhangsan",20);
	memcpy(stu.id,"0007",5);

	err = pthread_create(&tid,NULL,thread_fun,(void *)(&stu));
	if(err != 0)
	{
		printf("create new thread fialed\n");
		return 0;
	}

	int i;
	printf("main thread have %d args\n",argc);
	for(i=0; i<argc; i++)
	{
		printf("main thread args is %s\n",argv[i]);
	}
	
	sleep(1);
	return 0;
}


book@100ask:~/C_coding/0122$ sudo gcc -lpthread -pthread  -o main_thread main_thread.c 
book@100ask:~/C_coding/0122$ ./main_thread
student age is 20
,name is zhangsan
,id is 0007
main thread have 1 args
main thread args is ./main_thread

 上例,主线程结束,sleep 1秒后就return

book@100ask:~/C_coding/0122$ cat main_thread.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct student{
	int age;
	char name[20];
	char id[4];
};

void *thread_fun(void *stu)
{
	sleep(1);
	printf("student age is %d\n,name is %s\n,id is %s\n",((struct student *)stu)->age,((struct student *)stu)->name,((struct student *)stu)->id);
	return (void *)0;
}

int main(int argc,char *argv[])
{
	pthread_t tid;
	int err;

	struct student stu;
	stu.age = 20;
	memcpy(stu.name,"zhangsan",20);
	memcpy(stu.id,"0007",5);

	err = pthread_create(&tid,NULL,thread_fun,(void *)(&stu));
	if(err != 0)
	{
		printf("create new thread fialed\n");
		return 0;
	}

	int i;
	printf("main thread have %d args\n",argc);
	for(i=0; i<argc; i++)
	{
		printf("main thread args is %s\n",argv[i]);
	}
	
//	sleep(2);
	return 0;
}


book@100ask:~/C_coding/0122$ sudo gcc -lpthread -pthread  -o main_thread main_thread.c 
book@100ask:~/C_coding/0122$ ./main_thread 12 34 56
main thread have 4 args
main thread args is ./main_thread
main thread args is 12
main thread args is 34
main thread args is 56

如果main函数先退出,进程内的其它线程也结束。

book@100ask:~/C_coding/0122$ cat main_thread.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct student{
	int age;
	char name[20];
	char id[4];
};

void *thread_fun(void *stu)
{
	sleep(1);
	printf("student age is %d\n,name is %s\n,id is %s\n",((struct student *)stu)->age,((struct student *)stu)->name,((struct student *)stu)->id);
	return (void *)0;
}

int main(int argc,char *argv[])
{
	pthread_t tid;
	int err;

	struct student stu;
	stu.age = 20;
	memcpy(stu.name,"zhangsan",20);
	memcpy(stu.id,"0007",5);

	err = pthread_create(&tid,NULL,thread_fun,(void *)(&stu));
	if(err != 0)
	{
		printf("create new thread fialed\n");
		return 0;
	}

	int i;
	printf("main thread have %d args\n",argc);
	for(i=0; i<argc; i++)
	{
		printf("main thread args is %s\n",argv[i]);
	}
	
	sleep(2);
	return 0;
}


book@100ask:~/C_coding/0122$ sudo gcc -lpthread -pthread  -o main_thread main_thread.c 
book@100ask:~/C_coding/0122$ ./main_thread 12 34 56
main thread have 4 args
main thread args is ./main_thread
main thread args is 12
main thread args is 34
main thread args is 56
student age is 20
,name is zhangsan
,id is 0007

main函数sleep多一点,子进程就会被执行。

book@100ask:~/C_coding/0122$ cat main_thread.c
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct student{
	int age;
	char name[20];
	char id[4];
};

void *thread_fun(void *stu)
{
	sleep(1);
	printf("student age is %d\n,name is %s\n,id is %s\n",((struct student *)stu)->age,((struct student *)stu)->name,((struct student *)stu)->id);
	return (void *)0;
}

int main(int argc,char *argv[])
{
	pthread_t tid;
	int err;
	int *rval;

	struct student stu;
	stu.age = 20;
	memcpy(stu.name,"zhangsan",20);
	memcpy(stu.id,"0007",5);

	err = pthread_create(&tid,NULL,thread_fun,(void *)(&stu));
	if(err != 0)
	{
		printf("create new thread fialed\n");
		return 0;
	}

	int i;
	printf("main thread have %d args\n",argc);
	for(i=0; i<argc; i++)
	{
		printf("main thread args is %s\n",argv[i]);
	}
	
        pthread_exit(rval);
	return 0;
}


book@100ask:~/C_coding/0122$ sudo gcc -lpthread -pthread  -o main_thread main_thread.c 
book@100ask:~/C_coding/0122$ ./main_thread 12 34 56
main thread have 4 args
main thread args is ./main_thread
main thread args is 12
main thread args is 34
main thread args is 56
student age is 20
,name is zhangsan
,id is 0007

加了pthread_exit(rval),这样,主进程退出,子线程程序还会被执行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值