[转](阿里笔试)使用多线程和sleep函数生成字符串的伪随机排列
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;
- }