環境:Linux Ubuntu 14.0.1 (VM 9 workstations)
編譯器:Code Blocks
測試項目如下:
- 如何建制
- 函式的model
- nb的用途
- stopable的用途
函式的model︰
void *func(void *);
完整程式碼︰
#include <stdio.h>
#include <stdlib.h>
#include <cprops/thread.h>
void printlnStr(void *void_str)
{
char *str = (char *)void_str;
int i;
for(i=0; i<5; i++)
{
printf("Line %d:%s\n",i+1,str);
sleep(1);
}
return;
}
void ThreadStop(void *void_str)
{
char *str = (char *)void_str;
printf("str=%s,Is abort\n",str);
return;
}
void printfsplit()
{
printf("-------------------------------------------\n");
}
int main()
{
printf("thread pool init (min num,max num)\n");
cp_thread_pool *tp = cp_thread_pool_create(1,2);
printf("cp_thread_pool_get 取得thread pool裡面空的位置(non-block)\n");
int cnt =cp_thread_pool_get(tp,printlnStr, (void *)"Hello");
printf("cp_thread_pool_get_nb return:%d\n",cnt);
printf("cp_thread_pool_get 取得第2個空位置 如果沒有空位置,則等待空閒位置出來(block)\n");
cnt = cp_thread_pool_get(tp,printlnStr, (void *)"World");
printf("cp_thread_pool_get_nb return:%d\n",cnt);
printf("cp_thread_pool_get_nb 取得第3個空位置 如果沒有空位置,則跳過不等待(non-block)\n");
cnt = cp_thread_pool_get_nb(tp,printlnStr, (void *)"Hey");
printf("cp_thread_pool_get_nb return:%d\n",cnt);
printf("cp_thread_pool_wait 等待pool中所有thread結束(block)\n");
cp_thread_pool_wait(tp);
printf("All Thread Is Doen\n");
printfsplit();
printf("stopable 當thread被中斷時執行\n");
printf("cp_thread_pool_get_stoppable 取得thread pool裡面空的位置(non-block)\n");
cp_thread_pool_get_stoppable(tp,printlnStr, (void *)"Hello",ThreadStop,(void *)"Hello");
printf("cp_thread_pool_get_stoppable 取得第2個空位置 如果沒有空位置,則等待空閒位置出來(block)\n");
cp_thread_pool_get_stoppable(tp,printlnStr, (void *)"World",ThreadStop,(void *)"World");
printf("cp_thread_pool_get_stoppable_nb 取得第3個空位置 如果沒有空位置,則跳過不等待(non-block)\n");
cp_thread_pool_get_stoppable_nb(tp,printlnStr, (void *)"World",ThreadStop,(void *)"Hey");
printf("cp_thread_pool_wait 等待pool中所有thread結束(block)\n");
cp_thread_pool_wait(tp);
printf("All Thread Is Doen\n");
cp_thread_pool_destroy(tp);
printf("Thread_pool destory\n");
return 0;
}
沒有留言:
張貼留言