Пример #1
0
int main() {
        threadpool_t *tpool;
        int rc = threadpool_init(&tpool);
        task_t t1 = { test1, (void *) 0};
        task_t t2 = { test2, (void *) 0};
        assert(rc == 0);
        threadpool_add_task(tpool, &t1);
        threadpool_add_task(tpool, &t2);
        threadpool_run(tpool);
        threadpool_exit(tpool);
        pthread_exit(NULL);
}
Пример #2
0
void worker_thread_process(void *data)
{
    packet_t *packet;
    packet_t *next;
    assert(data);

    packet = (packet_t *)data;
    packet->meta_hd = meta_hd_pp[sys_thread_id_get()];
    assert(packet->meta_hd != NULL);

    //meta_buffer_sys_clear(packet->meta_hd);
    packet->flag &= ~PKT_HANDLE_MASK;/*清理上次的结果*/
    module_list_process(module_hd_p, pktag_hd_p, packet->pktag, packet);
    if (packet->flag & PKT_LOOP_NEXT) {
       threadpool_add_task(tp, worker_thread_process, packet, 0);
    } else if (packet->flag & PKT_DONT_FREE) {
            meta_buffer_sys_clear(packet->meta_hd);
    } else {
        do {
            next = packet->next_packet;
            meta_buffer_sys_clear(packet->meta_hd);
            zone_free(packet_zone, packet);
            packet = next;
        } while (packet != NULL);
    }
}
Пример #3
0
int main()
{
	my_count1 = 0;
	my_count2 = 0;
	pthread_mutex_init(&lock1, NULL);
	pthread_mutex_init(&lock2, NULL);
	threadpool_t *pool = threadpool_create(2);
	
		
	threadpool_add_task(pool, mk_task(f1, NULL));
	threadpool_add_task(pool, mk_task(f2, NULL));
	threadpool_add_task(pool, mk_task(f1, NULL));
	threadpool_destroy(pool, immediate_shutdown);	
	threadpool_add_task(pool, mk_task(f2, NULL));

	pthread_mutex_destroy(&lock1);	
	pthread_mutex_destroy(&lock2);	
	
	printf("my_count1 : %d\n", my_count1);
	printf("my_count2 : %d\n", my_count2);
}
Пример #4
0
void process_loop(module_hd_t *module_head)
{
	int32_t status, tag_id;
    uint64_t pkt_num = 0;
	extern int system_exit;
    module_info_t *recv;
    packet_t *packet;

    if (g_conf.mode == MODE_LIVE || g_conf.mode == MODE_FILE) {
    /*收包,线程中加入处理*/
        status = 0;
        packet_zone = zone_init("pcap_read", sizeof(packet_t) + MAX_PACKET_LEN, MAX_PACKET_HANDLE);
        assert(packet_zone);
        recv = module_info_get_from_name(module_head, "recv");
        assert(recv && recv->ops->process);
        do {
            do {
                packet = (void *)zone_alloc(packet_zone, 0);
            } while(packet == NULL);

            tag_id = recv->ops->process(recv, packet);
            if (tag_id > 0) {
                assert(packet->data);
                pkt_num++;
                status = threadpool_add_task(tp, worker_thread_process, packet, 0);
                if (status != 0) {
                    log_error(syslog_p, "Threadpool add task, status %d\n", status);
                    status = 0;
                }
                if (g_conf.pkt_num != 0 && pkt_num >= g_conf.pkt_num) {
                    system_exit = 1;
                }
           } else {
                status = tag_id;
                zone_free(packet_zone, packet);
            }
        } while( status >= 0 && !system_exit);
        if (g_conf.mode == MODE_FILE) {
            sleep(1);/*等待最后加入的work处理完成*/
        }

    } else if (g_conf.mode == MODE_SE) {
        do {
            status = module_list_process(module_head, pktag_hd_p, -1, NULL);
            pkt_num++;
            if (g_conf.pkt_num != 0 && pkt_num >= g_conf.pkt_num) {
                system_exit = 1;
            }
	    } while (status >= 0 && !system_exit);
    }
}
Пример #5
0
int main(int argc, char *argv[]) {
    long i = 10000;
    threadpool_t *pool = threadpool_create(10, 100, 0);
    assert(pool);
    while (i > 0) {
        if (i % 3 == 0) {
            assert(threadpool_add_task(pool, task3, 
                (void*)(long)(pool->threads_num), 1000) == 0);
        } else if (i % 3 == 1) {
            assert(threadpool_add_task(pool, task2, 
                (void*)(long)(pool->threads_num), 500) == 0);
        } else {
            assert(threadpool_add_task(pool, task1, 
                (void*)(long)(pool->threads_num), 0) == 0);
        }
        i--;
    }

    while (threadpool_task_over(pool, 1, 3) != 0) {};
    threadpool_exit(pool);
    assert(threadpool_destroy(pool, 1, 3) == 0);
    exit(0);
}
Пример #6
0
void demo3_thread_pool_test()
{
	struct threadpool *pool;
	int arr[ARR_SIZE], i, ret, failed_count = 0;

	for (i = 0; i < ARR_SIZE; i++) {
		arr[i] = i;
	}

	if ((pool = threadpool_init(10)) == NULL) {
		printf("Error! Failed to create a thread pool struct.\n");
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < ARR_SIZE; i++) {
		if (i % 10000 == 0) {
			/* blocking. */
			ret = threadpool_add_task(pool,slow_task,arr + i,1);
		}
		else {
			/* non blocking. */
			ret = threadpool_add_task(pool,fast_task,arr + i,0);
		}

		if (ret == -1) {
			printf("An error had occurred while adding a task.");
			exit(EXIT_FAILURE);
		}

		if (ret == -2) {
			failed_count++;
		}
	}

	threadpool_free(pool,1);
}
int main(void)
{
	threadpool_t pool;
	threadpool_init(&pool,3);
	int i=0;
	for(i=0; i<10; i++)
	{
		int *arg = (int *)malloc(sizeof(int));
		*arg=i;
		threadpool_add_task(&pool,mytask,arg);//往线程池中添加10个任务
	}
	//sleep(10);
	threadpool_destroy(&pool);
	return 0;
}
Пример #8
0
int main()
{
	threadpool_t pool;
	threadpool_init(&pool,3);
	
	int i ;
	for(i = 0; i < 10; i++)
	{
		int *arg = (int *)malloc(sizeof(int));
		*arg = i;
		threadpool_add_task(&pool,mytask,arg);
	}
	
	sleep(15);
	threadpool_destory(&pool);
	return 0;
}
Пример #9
0
int main( void ) 
{
   threadpool_t pool ; // 定义一个线程池变量
   threadpool_init( &pool, MAX_POOL_SIZE ) ; // 初始化线程池

   // 向线程池中添加 10 个任务,每个任务的处理函数都是 mytask
   for ( int i = 0; i < 10; ++ i ) 
   {
      int* arg = (int*)malloc( sizeof( int ) ) ;
      *arg = i ;
      threadpool_add_task( &pool, mytask, arg ) ;
   }

   threadpool_destroy( &pool ) ; // 销毁线程池
   
   return 0 ;
}
Пример #10
0
void _create_async_local(struct Msg* msg) {
	if(!USE_THREAD_POOL){
	pthread_t t;
	pthread_attr_t attr;
	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	int32_t rc = pthread_create(&t, &attr, _thread_start_local, msg);
	if (rc) {
		printf("ERROR: return code from pthread_create() is %d\n", rc);
		exit(-1);
	}
	pthread_attr_destroy(&attr);
	}else{
		//add a blocking task
		threadpool_add_task(pool,_thread_start_local,msg,1);
		
	}
}
int main(int argc, const char* argv[]) {
  if (argc <= 1) {
    printf("PLEASE ENTER THREAD NUMBER!\n");
    return 0;
  }
  int nthreads=atoi(argv[1]);
  printf("Number of threads: %d\n", nthreads);
  init_pos();
  double total = 0;
  int N = 3;
  int count;
  for (count = 1; count <= N; count ++) {
    struct timeval begin, end;
    double time_spent;
    
    gettimeofday(&begin, NULL);
    int n;
    for (n=0; n<NUM_TOPICS; n++) {
      // printf("Processing topic %d...\n", topics2011[n][0]);
      heap h_array[nthreads];
      memset(h_array,0,sizeof(h_array));
      struct threadpool *pool;
      pool = threadpool_init(nthreads);
      int i = 0;
      for (i=0; i<nthreads; i++) {
        struct arg_struct *args = malloc(sizeof *args);
        args->topic = n;
        args->startidx = i*(int)(ceil((double)NUM_DOCS / nthreads));
        if ((i+1)*(int)(ceil((double)NUM_DOCS / nthreads)) > NUM_DOCS) {
          args->endidx = NUM_DOCS;
        } else {
          args->endidx = (i+1)*(int)(ceil((double)NUM_DOCS / nthreads));
        }
        args->base = termindexes[nthreads-1][i];
        heap h;
        h_array[i] = h;
        args->h = &h_array[i];
        threadpool_add_task(pool,search,args,0);
      }
      threadpool_free(pool,1);

      heap h_merge;
      heap_create(&h_merge,0,NULL);
      float* min_key_merge;
      int* min_val_merge;
      for (i=0; i<nthreads; i++) {
        float* min_key;
        int* min_val;
        while(heap_delmin(&h_array[i], (void**)&min_key, (void**)&min_val)) {
          int size = heap_size(&h_merge);
          if ( size < TOP_K ) {
            heap_insert(&h_merge, min_key, min_val);
          } else {
            heap_min(&h_merge, (void**)&min_key_merge, (void**)&min_val_merge);
            if (*min_key_merge < *min_key) {
              heap_delmin(&h_merge, (void**)&min_key_merge, (void**)&min_val_merge);
              heap_insert(&h_merge, min_key, min_val);
            }
          }
        }
        heap_destroy(&h_array[i]);
      }

      int rank = TOP_K;
      while (heap_delmin(&h_merge, (void**)&min_key_merge, (void**)&min_val_merge)) {
        printf("MB%02d Q0 %ld %d %f Scan1_pos_multithread_intraquery\n", (n+1), tweetids[*min_val_merge], rank, *min_key_merge);
        rank--;
      }
      heap_destroy(&h_merge);
    }
    
    gettimeofday(&end, NULL);
    time_spent = (double)((end.tv_sec * 1000000 + end.tv_usec) - (begin.tv_sec * 1000000 + begin.tv_usec));
    total = total + time_spent / 1000.0;
  }
  printf("Total time = %f ms\n", total/N);
  printf("Time per query = %f ms\n", (total/N)/NUM_TOPICS);
}
Пример #12
0
//#define DEBUG
int main() {
	pthread_t schedulerThread;
	epfd = epollCreate(100);
	struct threadpool *pool;
	if ((pool = threadpool_init(100)) == NULL ) {
		printf("Error! Failed to create a thread pool struct.\n");
		exit(EXIT_FAILURE);
	}
	int sockfd;
	int newsockfd, newschedule;
	sockfd = Create();
	int schedulerfd = Create();

	if (Bind(35000, sockfd) < 0) {
		printf("error in bind");
		exit(0);
	}
	if (Bind(35001, schedulerfd) < 0) {
		printf("error in bind");
		exit(0);
	}
	Listen(sockfd, 10);
	Listen(schedulerfd, 10);
	pthread_create(&schedulerThread, 0, (void*) &schedule, NULL);
	int i = 0;
	while (i < 100) {
		incClient();
		newsockfd = Accept(sockfd);

		if (numClient() <= 100) {
			Send(newsockfd, "ACCEPT", strlen("ACCEPT") + 1);
			newschedule = Accept(schedulerfd);
			fprintf(stderr, "Connection established with scheduler");
			sockets newsock;
			newsock.active = 1;
			newsock.priority = 1;
			newsock.schedulerSocket = newschedule;
			newsock.workerSocket = newsockfd;
			socketList = addToList(socketList, newsock);
#ifdef DEBUG
			if(socketList==NULL) {
				fprintf(stderr,"socketlist is null");
				exit(0);
			}
#endif
			epollAdd(epfd, newschedule);
			int ret = threadpool_add_task(pool, negotiator, (void*) &newsockfd,
					1);
		} else {
			Send(newsockfd, "REJECT", strlen("REJECT") + 1);
			close(newsockfd);
		}

		i++;

	}
	sleep(10);
	Close(sockfd);
	printf("shutting down");

	return 0;
}
Пример #13
0
void *DataReceiver::epollReadData(void *arg) {
	struct epoll_event ev_read[10];
	int nfds = 0; //return the events count
	ThreadPool *tp = threadpool_create(5, 10240);
	int recBufferLen = 1024;
	char *recData = (char*) (malloc(recBufferLen));
	int start = 0, end = 0, totleLen = 0;
	int index = 0;
	int dataLen;
	int readLen = 0;
	SocketPool *sp = NULL;
	std::map<int, SocketPool*>::iterator it;
	while (1) {
		nfds = epoll_wait(*(int*) (arg), ev_read, 10, 300);
		for (int i = 0; i < nfds; ++i) {
			start = 0;
			end = 0;
			totleLen = 0;
			readData: readLen = CSocket::anetRead(ev_read[i].data.fd, recData + end, recBufferLen - end);
			if (readLen <= 0) {
				pthread_mutex_lock(&mutex);
				it = socketMap->find(ev_read[i].data.fd);
				if (it != socketMap->end()) {
					sp = it->second;
				}
				pthread_mutex_unlock(&mutex);
				if (sp) {
					sp->closeSocket(ev_read[i].data.fd);
					sp = NULL;
				}
				gaeaLog(GAEA_WARNING, "DataReceiver::anetRead,happened socket error%d,%d\n", end, recBufferLen);
				continue;
			}
			totleLen += readLen;
			while (end < totleLen) {
				if (recData[end] == P_END_TAG[index]) {
					++index;
					if (index == sizeof(P_END_TAG)) {
						dataLen = end - start - sizeof(P_START_TAG);
						char *retData = (char*) (malloc(dataLen));
						memcpy(retData, recData + start + sizeof(P_START_TAG), dataLen);
						readDataStruct *rds = new readDataStruct;
						rds->fd = ev_read[i].data.fd;
						rds->len = dataLen;
						rds->data = retData;
						threadpool_add_task(tp, socketReadData, rds);
						index = 0;
						++end;
						start = end;
					}
				} else if (recData[end] == P_END_TAG[0]) {
					index = 1;
				} else {
					index = 0;
				}
				++end;
			}

			if (end == totleLen) {
				if (start == 0 && end >= recBufferLen) {
					recData = (char*) (realloc(recData, recBufferLen * 2));
					recBufferLen = recBufferLen * 2;
					goto readData;
				} else if (start < end - 1) {
					memcpy(recData, recData + start, end - start);
					end -= start;
					start = 0;
					totleLen = end;
					goto readData;
				}
			}
		}

	}

	return NULL;
}