微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 多线程编程之:Linux线程编程

多线程编程之:Linux线程编程

时间:10-17 来源:互联网 点击:

"Setting attribute failed\n");

exit(res);

}

res = pthread_create(&thread, &attr, thrd_func, NULL);

if (res != 0)

{

printf("Create thread failed\n");

exit(res);

}

/* 释放线程属性对象 */

pthread_attr_destroy(&attr);

printf("Create tread success\n");

while(!finish_flag)

{

printf("Waiting for thread to finish...\n");

sleep(2);

}

return 0;

}

接下来可以在线程运行前后使用“free”命令查看内存的使用情况。以下是运行结果:

$ ./thread_attr

Create tread success

Waiting for thread to finish...

Thread is starting

Waiting for thread to finish...

Thread : job 0 delay = 3

Waiting for thread to finish...

Thread : job 1 delay = 2

Waiting for thread to finish...

Waiting for thread to finish...

Waiting for thread to finish...

Waiting for thread to finish...

Thread : job 2 delay = 9

Thread finished

/* 程序运行之前 */

$ free

total used free shared buffers cached

Mem: 255556 191940 63616 10 5864 61360

-/+ buffers/cache: 124716 130840

Swap: 377488 18352 359136

/* 程序运行之中 */

$ free

total used free shared buffers cached

Mem: 255556 191948 63608 10 5888 61336

-/+ buffers/cache: 124724 130832

Swap: 377488 18352 359136

/* 程序运行之后 */

$ free

total used free shared buffers cached

Mem: 255556 191940 63616 10 5904 61320

-/+ buffers/cache: 124716 130840

Swap: 377488 18352 359136

可以看到,线程在运行结束后就收回了系统资源,并释放内存。

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top