linux-线程资源释放-线程属性-exit资源释放规律
#include <stdio.h>
#include <string.h>
#include <fcntl.h> 
#include <unistd.h>  
#include <time.h>
#include <stdlib.h>
#include <pthread.h> 
#include <errno.h>

void printf_thread_info()
{
	pthread_attr_t gattr;
	int s = pthread_getattr_np(pthread_self(), &gattr);
	if (s != 0)
	{
		printf("printf_thread_info %x pthread_getattr_np error\n", pthread_self());
		return;
	}
	int i;
	s = pthread_attr_getdetachstate(&gattr, &i);
	if (s != 0)
	{
		printf("printf_thread_info %x pthread_attr_getdetachstate error\n", pthread_self());
		return;
	}
	printf("printf_thread_info %x pthread_attr_getdetachstate %d\n", pthread_self(),i);
}

void* test_fun(void* fd)
{
	printf("test_fun exit\n");
	exit(0);
	printf_thread_info();
	char char_recv[1024 * 2];//´æ·ÅÊý¾Ý
	memset(char_recv, 0, sizeof(char_recv));
	sleep(5);
	printf("leave test_fun\n");
}

void create_test_thread()
{
	int i;
	scanf("%d", &i);
	pthread_t thread;

	if (pthread_create(&thread, NULL, test_fun, NULL) != 0)//´´½¨×ÓÏß³Ì  
	{
		perror("pthread_create check_inite_rknn_stuck");
	}
	else {
		//int k = pthread_detach(thread);

	}
	sleep(2);
	pthread_create(&thread, NULL, test_fun, NULL);
	printf("create_test_thread %d \n", thread);
	int res = pthread_join(thread, NULL);
	if (res != 0) {
		if (res == EINVAL) {
			printf("not support join\n");
		}
	}

}

class T
{
public:
	T()
	{

	};
	~T()
	{
		printf("exit T1\n");
		sleep(10);
		printf("exit T2\n");
	}
};

T gt;
int main()
{

 	while (true)
 	{
 		create_test_thread();
 	}
	return 0;
}
//while true; do pmap -x $(pidof test_mem.bin) > test1; sleep 2; pmap -x $(pidof test_mem.bin) > test2;echo xxxxxxxxxxx;diff test1 test2;sleep 1; done

/*
while true; do pmap -x $(pidof sh.bin) > test1;  echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; diff test2 test1; sleep 1; pmap -x $(pidof sh.bin) > test2; echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; diff test1 test2; sleep 1; done
*/
/*
while true; do pmap -x 42971 > test1;  echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; diff test2 test1; sleep 1; pmap -x 42971 > test2; echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx; diff test1 test2; sleep 1; done
*/

  

posted on 2022-07-19 17:46  DuoRuaiMi4567  阅读(67)  评论(0编辑  收藏  举报