malloc多线程实例
来源:互联网 发布:淘宝飞猪旅行 编辑:程序博客网 时间:2024/06/05 19:19
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
unsigned char *pszPacket[1000];
unsigned int unPosition;
unsigned int unIsRunning;
pthread_mutex_t hMallocMutex;
void *PT_MallocHandler(void *argc)
{
while(unIsRunning)
{
if(pthread_mutex_lock(&hMallocMutex) == 0)
{
pszPacket[unPosition] = (unsigned char *)malloc(600*sizeof(char));
printf("malloc : %p ---- %d \n", pszPacket[unPosition], unPosition);
unPosition ++;
pthread_mutex_unlock(&hMallocMutex);
}
usleep(100000);
}
}
void *PT_FreeHandler(void *argc)
{
int i;
while(unIsRunning)
{
if(pthread_mutex_lock(&hMallocMutex) == 0)
{
for(i=0; i<unPosition; i++)
{
printf("free : %p ---- %d--%d\n", pszPacket[i], i, unPosition);
free(pszPacket[i]);
}
unPosition = 0;
pthread_mutex_unlock(&hMallocMutex);
}
sleep(1);
}
}
int main()
{
pthread_t thMallocId, thFreeId;
unPosition = 0;
unIsRunning = 1;
if(pthread_mutex_init(&hMallocMutex,NULL) != 0)
return 0;
//启动内存申请线程
if(pthread_create(&thMallocId, NULL, PT_MallocHandler, NULL) == -1)
return 0;
usleep(500000);
//启动内存释放线程
if(pthread_create(&thFreeId, NULL, PT_FreeHandler, NULL) == -1)
return 0;
sleep(30);
unIsRunning = 0;
pthread_join(thMallocId, 0);
pthread_join(thFreeId, 0);
}
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
unsigned char *pszPacket[1000];
unsigned int unPosition;
unsigned int unIsRunning;
pthread_mutex_t hMallocMutex;
void *PT_MallocHandler(void *argc)
{
while(unIsRunning)
{
if(pthread_mutex_lock(&hMallocMutex) == 0)
{
pszPacket[unPosition] = (unsigned char *)malloc(600*sizeof(char));
printf("malloc : %p ---- %d \n", pszPacket[unPosition], unPosition);
unPosition ++;
pthread_mutex_unlock(&hMallocMutex);
}
usleep(100000);
}
}
void *PT_FreeHandler(void *argc)
{
int i;
while(unIsRunning)
{
if(pthread_mutex_lock(&hMallocMutex) == 0)
{
for(i=0; i<unPosition; i++)
{
printf("free : %p ---- %d--%d\n", pszPacket[i], i, unPosition);
free(pszPacket[i]);
}
unPosition = 0;
pthread_mutex_unlock(&hMallocMutex);
}
sleep(1);
}
}
int main()
{
pthread_t thMallocId, thFreeId;
unPosition = 0;
unIsRunning = 1;
if(pthread_mutex_init(&hMallocMutex,NULL) != 0)
return 0;
//启动内存申请线程
if(pthread_create(&thMallocId, NULL, PT_MallocHandler, NULL) == -1)
return 0;
usleep(500000);
//启动内存释放线程
if(pthread_create(&thFreeId, NULL, PT_FreeHandler, NULL) == -1)
return 0;
sleep(30);
unIsRunning = 0;
pthread_join(thMallocId, 0);
pthread_join(thFreeId, 0);
}
- malloc多线程实例
- malloc简单实例
- malloc函数两个使用实例
- 多线程实例
- 多线程实例
- 多线程实例
- 多线程实例
- 多线程实例
- 多线程实例
- 多线程实例
- 多线程实例
- 多线程实例
- 多线程实例
- 多线程实例
- 多线程实例
- malloc内存分配实例--整数排序
- 【多线程】应用Java多线程实例
- malloc
- LeetCode题目笔记(三) -- Median of Two Sorted Arrays
- lseek、fseek实例
- Linux系统加固paper
- python 写Excel操作
- 使用Spread.NET创建XY Plot图表
- malloc多线程实例
- QT下的几种透明效果
- python之import模块详解
- POJ 3070 Fibonacci
- ifconfig 截取ip地址 -linux
- 套接字选项列表(getsockopt和setsockopt )
- windows下配置Sublime Text 2开发Nodejs
- 程序员的学习之路
- Http的报文结构