pthread.c

来自「内含uclinux的多线程实例、makefile、及rules.mak文件」· C语言 代码 · 共 71 行

C
71
字号
#include <stdio.h>
#include <stdlib.h>#include <unistd.h>

#include <pthread.h>
#include <signal.h>
void *test2(void);void *test3(void);
pthread_t th_1,th_2,th_3;
void *v1,*v2,*v3;
void *test1(void)
{
	char c=0;
	printf("Test1\n");
	while(1)
	{
		c=getchar();
		if(c=='2')
		{
			pthread_cancel(th_2);
			printf("Kill a thread\n");
		}
		if(c=='3')
		{
			pthread_cancel(th_3);
			printf("Kill a thread\n");
		}
		if(c=='k')
		{
			pthread_create( &th_2,NULL,test2,0 );
			printf("start a thread\n");
		}		if(c=='s')
		{
			pthread_create( &th_3,NULL,test3,0 );
			printf("start a thread\n");
		}
	}
	return NULL;
}
void *test2(void)
{
	char a[100]="/bin/cs 2 2000 /var/tmp/f1.txt";
	printf("Test 2\n");
	system( a );
	return NULL;
}
void *test3(void)
{	int i=0;
	for( i=0;i<1000;i++ )
	{
		printf("Test3=%d\n",i);
	}
	return NULL;
}
int main()
{
	pthread_create( &th_1,NULL,test1,0 );
	//pthread_create( &th_2,NULL,test2,0 );
	pthread_create( &th_3,NULL,test3,0 );
	pthread_join( th_1,&v1 );
	//pthread_join( th_2,&v2 );
	pthread_join( th_3,&v3 );
	return 1;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?