日期:2014-05-16  浏览次数:20884 次

linux多线程1

如果了解java多线程编程的话,照葫芦画瓢,是很容易理解linux下面的线程模型的。

?

#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

//define the first thread procedure.
void * thread_1_pro(void * arg)
{
	//derive current thread's id
	pthread_t  tid;
	tid=pthread_self();
	printf(" AAAAA thread id is %u\n",(unsigned int)tid);
	int * p_int_a=(int *) arg;
	while(1)
	{
		sleep(5);	
		printf("------------- a = %d\n",*p_int_a);		
		(*p_int_a)++;
		printf("+++++++++++++ a = %d\n",*p_int_a);		
    }	
}

//define the second thread procedure.
void * thread_2_pro(void * arg)
{
	//derive current thread's id
	pthread_t  tid;
	tid=pthread_self();
	printf(" BBBB thread id is %u\n",(unsigned int)tid);
	int * p_int_a=(int *) arg;
	while(1)
	{
		sleep(3);
		printf("------------- b = %d\n",*p_int_a);	
		(*p_int_a)++;
		printf("+++++++++++++ b = %d\n",*p_int_a);
		
   }	
}


int main(int argc, char ** argv)
{
	pthread_t tidA, tidB;
	//derive current thread's id
	pthread_t  tid;
	tid=pthread_self();
	printf(" CCCCCC  thread id is %u\n",(unsigned int)tid);
    int common=0;
	pthread_create(&tidA, NULL, &thread_1_pro, &common);
	pthread_create(&tidB, NULL, &thread_2_pro, &common);
	sleep(12);
	//为什么需要最下面两行呢,如果没有,12秒后,主进程结束,会自动回收线程tidA, tidB,
	//这两行就是主进程中的线程要等这两个分支线程结束后,才执行join后面的内容。
	//pthread_join(tidA, NULL);
	//pthread_join(tidB, NULL);
	return 0;	
}

?? 输出结果:

 CCCCCC  thread id is 3078366912
 BBBB thread id is 3069971312
 AAAAA thread id is 3078364016
------------- b = 0
+++++++++++++ b = 1
------------- a = 1
+++++++++++++ a = 2
------------- b = 2
+++++++++++++ b = 3
------------- b = 3
+++++++++++++ b = 4
------------- a = 4
+++++++++++++ a = 5
------------- b = 5
+++++++++++++ b = 6

??可以看到这个结果还是蛮有规律的,但是我们想说的,如果仔细调整一下sleep的位置,输出的结果可能就没有上面这么规律了,如下。

//define the first thread procedure.
void * thread_1_pro(void * arg)
{
	//derive current thread's id
	pthread_t  tid;
	tid=pthread_self();
	printf(" AAAAA thread id is %u\n",(unsigned int)tid);
	int * p_int_a=(int *) arg;
	while(1)
	{
			
		printf("------------- a = %d\n",*p_int_a);		
		(*p_int_a)++;
		sleep(7);
		printf("+++++++++++++ a = %d\n",*p_int_a);		
    }	
}

//define the second thread procedure.
void * thread_2_pro(void * arg)
{
	//derive current thread's id
	pthread_t  tid;
	tid=pthread_self();
	printf(" BBBB thread id is %u\n",(unsigned int)tid);
	int * p_int_a=(int *) arg;
	while(1)
	{
		
		printf("------------- b = %d\n",*p_int_a);	
		(*p_int_a)++;
		sleep(3);
		printf("+++++++++++++ b = %d\n",*p_int_a);
		
   }	
}

??输出的结果如下:

 CCCCCC  thread id is 3077633728
 AAAAA thread id is 3077630832
------------- a = 0
 BBBB thread id is 3069238128
------------- b = 1
+++++++++++++ b = 2
------------- b = 2
+++++++++++++ b = 3
------------- b = 3
+++++++++++++ a = 4
------------- a = 4
+++++++++++++ b = 5
------------- b = 5
+++++++++++++ b = 6
------------- b = 6
+++++++++++++ a = 7
------------- a = 7
+++++++++++++ b = 8
------------- b = 8
+++++++++++++ b = 9
------------- b = 9

?和上面相比,显的比较乱,是因为没有“同步”的原因。

?

?