linux线程的简单测试(一)

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

int test_1=0;
int test_2=0;
void * test1(void * data)
{
	while(1)
	{	
		test_1++;
		sleep(2);
	}
  return NULL;
}
/*--------------------------------------------------------*/
void * test2(void * data)
{
	
  	while (1) 
	{
		test_2++;
		printf("test1 is : %d \t test2 is : %d \t",test_1,test_2);
		fflush(stdout);		
		sleep(1);	
		printf("\r");
  	}
  return NULL;
}
/*--------------------------------------------------------*/
int main(void)
{
  	pthread_t th_a, th_b;
  	void * retval;
 	pthread_create(&th_a, NULL, test1, 0);
  	pthread_create(&th_b, NULL, test2, 0);

  	pthread_join(th_a, &retval);
  	pthread_join(th_b, &retval);

  	return 0;
}

输入 gcc -o pthread pthread.c  提示

由于pthread库不是Linux系统默认的库,连接时需要使用库libpthread.a,所以在使用pthread_create创建线程时,在编译中要加-lpthread参数

gcc -o pthread pthread.c -lpthread






发布了24 篇原创文章 · 获赞 10 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/CSDNZSX/article/details/78862773