- 论坛徽章:
- 0
|
本帖最后由 liu_hacker 于 2010-11-23 09:25 编辑
循环创建多个线程,每个线程调用同一个函数,但是该函数是死循环,传的参数不同,为什么我只能创建第一个线程后就一直执行那个死循环,无法创建第二、第三。。。线程了呢?- void *get_agent_attack(void * arg)
- {
- struct mysql_info *m_information;
- m_information = (struct mysql_info *)arg;
- sleep(2);
- pthread_mutex_lock(&mut);
- for(; ; )
- {
- sleep(2);
- printf("%s\n",m_information->ser_url);
- printf("%s\n",m_information->my_user);
- printf("%s\n",m_information->u_pass);
- }
- pthread_mutex_unlock(&mut);
- //pthread_exit(NULL);
- }
- void thread_create(int agent_num)
- {
- int temp,i;
- memset(&thread, 0, sizeof(thread));
- struct mysql_info my_info[MAX_NUM];
- for(i=0;i<agent_num;i++ )
- {
- my_info[i].ser_url = agent_ip[i];
- my_info[i].my_user = agent_db_user[i];
- my_info[i].u_pass = agent_db_pass[i];
- if((temp = pthread_create(&thread[i], NULL, get_agent_attack, (void *)&my_info[i])) != 0)
- {
- printf("Tread%d failed !\n",i);
- }
- else
- {
- printf("thread%d successful !\n",i);
- }
- if(thread[i] !=0)
- {
- pthread_join(thread[i], NULL);
- }
- //sleep(2);
- }
- }
- main函数
- pthread_mutex_init(&mut,NULL);
- //printf("beagin\n");
- int agent_num;
- agent_num=get_agent_info();
- //sleep(1);
- thread_create(agent_num);
- pthread_exit(NULL);
复制代码 |
|