- 線程的創建:
include
include
include
include
include
define uint unsigned int
typedef struct
{
uint a;
uint b;
} hello;
void print_ids( const char *s)
{
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf(“%s pid %u tid %u (0x%x)\n”, s,(uint)pid, (uint)tid, (uint)tid);
}
void thread_func( void arg)
{
hello myarg;
print_ids(“new thread: “);
printf(“thread param is :%d and %d\n”,((hello )arg)->a, ((hello )arg)->b);
return NULL;
}
int main(void)
{
int err;
pthread_t newtid;
//int myarg;
hello myarg;
myarg.a = 071734;
myarg.b = 18;
//myarg = 1;
err = pthread_create( &newtid, NULL, thread_func, &myarg);
printf(“the value of newtid is %u\n”, newtid);
if(err != 0)
{
perror(“create thread error\n”);
exit(1);
}
print_ids(“main thread: “);
sleep(1); //等待new thread function finish。(這個新的線程退出方式:從啓動例程中返回,經測試如果子線程未執行完成,這是主進程退出,子線程也被迫結束)
exit(0);
}
- 線程池:通過預創建一定數量的工作線程對系統進行併發處理來提高系統運行效率。
三種線程池模型:任務隊列控制的線程池模型、工作線程控制的線程池模型、主控線程控制的線程池模型、