Linux系统编程——线程代码


代码内容:

Linux系统编程——线程


运行环境:

Linux


编程语言:

C语言


主要语法:

函数,指针,结构体


备注:

个人日常学习代码存档


代码


thread_cond1:

#include 
#include 
#include 
#include 
int g_data = 0;

pthread_mutex_t mutex;
pthread_cond_t cond;

void *func1(void *arg)
{
     
	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("t1:param is %d\n",*((int *)arg));
	static int cnt = 0;
	while(1)
	{
     
		pthread_cond_wait(&cond,&mutex);
		printf("t1 run\n");
	
		printf("t1:%d\n",g_data);
		g_data = 0;
		sleep(1);
		if(cnt++ == 10)
			exit(1);
	}
}	
void *func2(void *arg)
{
     
        printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
        printf("t2:param is %d\n",*((int *)arg));
	while(1)
	{
     
		printf("t2: %d\n",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;
	int param = 100;
	pthread_t t1;
	pthread_t t2;
	pthread_mutex_init(&mutex,NULL);
	pthread_cond_init(&cond,NULL);

	ret = pthread_create(&t1,NULL,func1,(void *)&param);
	if(ret == 0)
	{
     
//		printf("main:creat t1 success\n");
	}
	ret = pthread_create(&t2,NULL,func2,(void *)&param);
        if(ret == 0)
	{
     
//                printf("main:creat t2 success\n");
        }

//	printf("main:%ld\n",(unsigned long)pthread_self());
	

	pthread_join(t1,NULL);
	pthread_join(t2,NULL);
	
	pthread_cond_destroy(&cond);
	pthread_mutex_destroy(&mutex);
	return 0;
}


thread1.c:

#include 
#include 

void *func1(void *arg)
{
     
	static int ret = 10;
	static char *p = "t1 is run out";
	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("t1:param is %d\n",*((int *)arg));
	pthread_exit((void *)p);
}


int main()
{
     
	int ret;
	int param = 100;
	pthread_t t1;

	char *pret = NULL;

	ret = pthread_create(&t1,NULL,func1,(void *)&param);
	if(ret == 0)
	{
     
		printf("main:creat t1 success\n");
	}
	printf("main:%ld\n",(unsigned long)pthread_self());
	
	pthread_join(t1,(void **)&pret);

	printf("main: t1 quit:%s\n",pret);

	return 0;
}


pthread2.c:


#include 
#include 
#include 

int g_data = 0;

void *func1(void *arg)
{
     
	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("t1:param is %d\n",*((int *)arg));
	while(1){
     
		printf("t1:%d\n",g_data++);
		sleep(1);
		if(g_data == 3){
     
			pthread_exit(NULL);
		}
	}
}

void *func2(void *arg)
{
     
        printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
        printf("t2:param is %d\n",*((int *)arg));
        while(1){
     
		printf("t2:%d\n",g_data++);
		sleep(1);
	}
}

int main()
{
     
	int ret;
	int param = 100;
	pthread_t t1;
	pthread_t t2;

	ret = pthread_create(&t1,NULL,func1,(void *)&param);
	if(ret == 0)
	{
     
		printf("main:creat t1 success\n");
	}
	ret = pthread_create(&t2,NULL,func2,(void *)&param);
        if(ret == 0)
        {
     
                printf("main:creat t2 success\n");
        }

	printf("main:%ld\n",(unsigned long)pthread_self());
	
	 while(1){
     
                printf("main:%d\n",g_data++);
                sleep(1);
        }

	pthread_join(t1,NULL);
	pthread_join(t2,NULL);


	return 0;
}


pthread_mutex1.c:

#include 
#include 
#include 

int g_data = 0;

pthread_mutex_t mutex;

void *func1(void *arg)
{
     
	pthread_mutex_lock(&mutex);
	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("t1:param is %d\n",*((int *)arg));
	pthread_mutex_unlock(&mutex);
}	
void *func2(void *arg)
{
     
	pthread_mutex_lock(&mutex);
        printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
        printf("t2:param is %d\n",*((int *)arg));
 	pthread_mutex_unlock(&mutex);
}
void *func3(void *arg)
{
     
        pthread_mutex_lock(&mutex);
        printf("t3:%ld thread is create\n",(unsigned long)pthread_self());
        printf("t3:param is %d\n",*((int *)arg));
        pthread_mutex_unlock(&mutex);
}


int main()
{
     
	int ret;
	int param = 100;
	pthread_t t1;
	pthread_t t2;
	pthread_t t3;

	pthread_mutex_init(&mutex,NULL);

	ret = pthread_create(&t1,NULL,func1,(void *)&param);
	if(ret == 0)
	{
     
		printf("main:creat t1 success\n");
	}
	ret = pthread_create(&t2,NULL,func2,(void *)&param);
        if(ret == 0)
	{
     
                printf("main:creat t2 success\n");
        }
	ret = pthread_create(&t3, NULL, func3, (void *)&param);
	if(ret == 0)
	{
     
		printf("main:creat t3 success\n");
	}

	printf("main:%ld\n",(unsigned long)pthread_self());
	

	pthread_join(t1,NULL);
	pthread_join(t2,NULL);
	pthread_join(t3,NULL);

	pthread_mutex_destroy(&mutex);
	return 0;
}


pthread_mutex2.c:

#include 
#include 
#include 
#include 
int g_data = 0;

pthread_mutex_t mutex;

void *func1(void *arg)
{
     
//	pthread_mutex_lock(&mutex);
	printf("t1:%ld thread is create\n",(unsigned long)pthread_self());
	printf("t1:param is %d\n",*((int *)arg));
	pthread_mutex_lock(&mutex);
	while(1)
	{
     
		printf("t1: %d\n",g_data++);
		sleep(1);
		if(g_data == 3)
		{
     
			pthread_mutex_unlock(&mutex);
	//		pthread_exit(NULL);
			exit(0);
		}
	}
}	
void *func2(void *arg)
{
     
//	pthread_mutex_lock(&mutex);
        printf("t2:%ld thread is create\n",(unsigned long)pthread_self());
        printf("t2:param is %d\n",*((int *)arg));
	while(1)
	{
     
		printf("t2: %d\n",g_data);
		pthread_mutex_lock(&mutex);
		g_data++;
		pthread_mutex_unlock(&mutex);
		sleep(1);
	}
//	pthread_mutex_unlock(&mutex);
}
int main()
{
     
	int ret;
	int param = 100;
	pthread_t t1;
	pthread_t t2;
	pthread_mutex_init(&mutex,NULL);

	ret = pthread_create(&t1,NULL,func1,(void *)&param);
	if(ret == 0)
	{
     
		printf("main:creat t1 success\n");
	}
	ret = pthread_create(&t2,NULL,func2,(void *)&param);
        if(ret == 0)
	{
     
                printf("main:creat t2 success\n");
        }

	printf("main:%ld\n",(unsigned long)pthread_self());
	

	pthread_join(t1,NULL);
	pthread_join(t2,NULL);

	pthread_mutex_destroy(&mutex);
	return 0;
}


脚本:

#include 
int main(int argc,char **argv)
{
     
	int time = atoi(argv[1]);
	int i = 0;
	for(i=0;i<time;i++)
	{
     
		system("./a.out");
	}
}

#include 
int main()
{
     
	int i = 0;
	for(i=0;i<10;i++)
	{
     
		system("./pthread");
	}
}


附:

如果文章有帮助就点个赞哦,有问题可留言。

你可能感兴趣的:(日常代码存档,linux)