手机版

linux下C语言多线程编程实例(2)

时间:2025-04-28   来源:未知    
字号:

linux下C语言多线程编程实例

{

printf("thread2 : number = %d\n",number);

pthread_mutex_lock(&mut);

number++;

pthread_mutex_unlock(&mut);

sleep(3);

}

printf("thread2 :主函数在等我完成任务吗?\n");

pthread_exit(NULL);

}

void thread_create(void)

{

int temp;

memset(&thread, 0, sizeof(thread)); //comment1

/*创建线程*/

if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0) //comment2

printf("线程1创建失败!\n");

else

printf("线程1被创建\n");

if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0) //comment3 printf("线程2创建失败");

else

printf("线程2被创建\n");

}

void thread_wait(void)

{

/*等待线程结束*/

if(thread[0] !=0) { //comment4

pthread_join(thread[0],NULL);

printf("线程1已经结束\n");

}

if(thread[1] !=0) { //comment5

pthread_join(thread[1],NULL);

printf("线程2已经结束\n");

}

}

int main()

{

linux下C语言多线程编程实例(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
×
二维码
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)