[转](阿里笔试)使用多线程和sleep函数生成字符串的伪随机排列
2024-08-23 02:33:14
http://blog.csdn.net/jiaowopan/article/details/12009079
C/C++(Linux平台)
注意编译连接时需要链接pthread库,即g++ XX.cpp -o XX -lpthread
- #include <stdio.h>
- #include <unistd.h>
- #include <pthread.h>
- void* thread(void* v)
- {
- char ch = *(char*)v;
- sleep(1);
- putchar(ch);
- }
- int main()
- {
- pthread_t id[7];
- int i ,ret;
- char str[] = "abcdefg";
- for(i = 0; i < 7; ++i)
- {
- ret = pthread_create(&id[i],NULL,thread,&str[i]);
- if(0 != ret)
- {
- printf("create pthread error\n");
- i--;
- }
- }
- for(i = 0; i < 7; ++i)
- pthread_join(id[i],NULL);
- printf("\n");
- return 0;
- }
最新文章
- Maven 配置远程仓库
- IO多路转接select和poll
- trigger,triggerhandler模拟事件
- Ant 入门
- OpenJudge/Poj 1005 I Think I Need a Houseboat
- Linux基础1之磁盘与分区
- bzoj3504
- USB Mass Storage协议分析
- POJ_2769同余问题
- TP5使用phpmailer实现邮件发送
- apache配置,禁止ip访问web站点
- Reveal安装
- [原]openstack-kilo--issue(二十一) instance can&#39;t get ip 虚拟机不能得到ip(2)
- centos6.5官方dvd做本地yum
- MongoDB的数据模型
- iOS UI-三种简单的动画设置
- ASP.NET下跨应用共享Session和使用Redis进行Session托管
- 【自动化专题】借助firefox插件定位web元素小技巧
- oracle中恢复删除的表
- PHP5.5的新特性