多线程编程之:Linux线程编程
"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
可以看到,线程在运行结束后就收回了系统资源,并释放内存。
- REDIce-Linux--灵活的实时Linux内核(11-12)
- linux文件系统基础(02-09)
- Linux标准趋向统一(11-12)
- linux基础技术(02-09)
- LINUX的目录树(02-09)
- 在Windows下启动Linux(02-09)