Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <string.h>
- #include <sys/time.h>
- #define MAIN_THPOOL_NUM 4
- #define WHILE_FOR_NUM 10
- #define MALLOC_SIZE 4096
- int g_done[MAIN_THPOOL_NUM];
- pthread_t pthreads[MAIN_THPOOL_NUM];
- void sleepmsec(long msec)
- {
- struct timeval stTimeVal;
- stTimeVal.tv_sec = msec / 1000;
- msec -= stTimeVal.tv_sec * 1000;
- stTimeVal.tv_usec = msec * 1000;
- select(0, 0, 0, 0, &stTimeVal);
- }
- void alloc_free(int ri)
- {
- void *test_array[WHILE_FOR_NUM];
- int i;
- for (i = 0; i < WHILE_FOR_NUM; i++) {
- test_array[i] = NULL;
- test_array[i] = malloc(MALLOC_SIZE);
- if (test_array[i]) {
- memset(test_array[i],i,MALLOC_SIZE);
- } else {
- printf("alloc error %d\n",ri);
- }
- }
- for(i=0; i<WHILE_FOR_NUM; i++) {
- if (test_array[i]) {
- free(test_array[i]);
- test_array[i]=NULL;
- }
- }
- }
- void *polling_task(void *arg)
- {
- void *test_array[WHILE_FOR_NUM];
- int i = *(int*)arg;
- printf("i=%d\n",i);
- while ( 1 ) {
- alloc_free(i);
- sleepmsec(10);
- }
- return NULL;
- }
- int main(int argc, char *argv[])
- {
- int i;
- int status[MAIN_THPOOL_NUM];
- //int cnt = 1000000;
- for (i = 0; i < MAIN_THPOOL_NUM; i++) {
- pthread_create(&pthreads[i], NULL, (void *)polling_task, &i);
- sleepmsec(1);
- }
- while (1) {
- sleepmsec(10);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement