线程学习

  1. 线程的创建:

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);
}

  1. 线程池:通过预创建一定数量的工作线程对系统进行并发处理来提高系统运行效率。

三种线程池模型:任务队列控制的线程池模型、工作线程控制的线程池模型、主控线程控制的线程池模型、

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值