- 线程的创建:
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);
}
- 线程池:通过预创建一定数量的工作线程对系统进行并发处理来提高系统运行效率。
三种线程池模型:任务队列控制的线程池模型、工作线程控制的线程池模型、主控线程控制的线程池模型、