LINUX 多线程,两个线程顺序影响 执行结果?

色郎中 2015-02-10 11:24:14


void main(int argc, char **argv)
{
initMutex();
initial_udp();
// thread A 代码,放在一个函数里了
udp_process();
// 这个函数里,开启了 一个线程,方法和下面的一样。 单独一下线程运行,一切正常。

// thread B 开始
/*
初始化属性值,均设为默认值
*/

pthread_attr_init(&attr2);
pthread_attr_setscope(&attr2, PTHREAD_SCOPE_SYSTEM);
pthread_attr_setdetachstate(&attr2, PTHREAD_CREATE_DETACHED);

if(pthread_create(&thread_loop_print,&attr2,(void*)pthread_handle_message2,NULL))
{
perror("pthread_creat error!");
exit(-1);
}
// thread B 结束

// udp_process();
while(1)
{}
//return 0;

}


程序开始 只有一个 thread A,,完成UDP数据的收发工作,跑起来一切正常。

想加上串口部分, 于是就写了个循环打印一个字符串的函数,给 thread B ,单独跑也没有问题。

如果两个线程都工作时,,

thread B 先执行,再执行 thread A ,两个线程的执行都没有问题也,

但是
如果,先执行thread A 线程 ,,再执行thread B 线程 ,则 thread B 无法正确执行,,

这个是什么原因造成的呢?

网上搜罗一下,,在main函数,加了while循环,,去掉返回,都不行!




...全文
278 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhxianbin 2015-02-10
  • 打赏
  • 举报
回复
百分百自己代码的问题,可能和共享数据操作有关
空的 2015-02-10
  • 打赏
  • 举报
回复
记得unix网络编程上说有些机子上是默认单线程执行的,就是无论一个进程开多少个线程,就是单线程执行 要编译的时候加个开关什么的,忘了。。。 是不是和这个有关
色郎中 2015-02-10
  • 打赏
  • 举报
回复


setting rlimit success! 
Starting listening 
IP and port bind �� 
IP  : 192.168.34.52   
port: 1234 
socket adding in  epoll success! 
kdpfd values: 8 
nfds values: 1 
[New Thread 0xb7e06b40 (LWP 23579)]
kdpfd values: 8 
socket 7 recv from : 192.168.34.33 : 8089 message: @�  4 bytes 
Hex bytes: 
400202BC

Data length:8 
F0 06 02 00 11 22 33 44 


nfds values: -1 
epoll_wait error! 
: Interrupted system call
[Thread 0xb7e06b40 (LWP 23579) exited]

[New Thread 0xb7605b40 (LWP 23580)]
this is thread 2!
[New Thread 0xb7e06b40 (LWP 23581)]
this is thread 3!

this is thread 2!
this is thread 2!
this is thread 3!

this is thread 2!
this is thread 2!
this is thread 2!
this is thread 3!

this is thread 2!
this is thread 2!
this is thread 2!
this is thread 3!

this is thread 2!
this is thread 2!
this is thread 2!
this is thread 3!

this is thread 2!

用GDB 调试时, udp_process(); 开启最先,,有数据来时,里面线程能正常运行一次,后面就报错了 但是, thread A 和 thread B 能正常循环打印 字符串 不在调试模式时,,全速跑 只有, udp_process(); 能正常运行,, thread A 和 thread B 不能打印...
色郎中 2015-02-10
  • 打赏
  • 举报
回复
udp_process(); 执行后, 后面就不执行了,为何呢?
色郎中 2015-02-10
  • 打赏
  • 举报
回复


 udp_process();
		 /* 
        初始化属性值,均设为默认值
        */    
	 
        pthread_attr_init(&attr2);
        pthread_attr_setscope(&attr2, PTHREAD_SCOPE_SYSTEM);        
        pthread_attr_setdetachstate(&attr2, PTHREAD_CREATE_DETACHED);
		
		 if(pthread_create(&thread_loop_print,&attr2,(void*)pthread_handle_message2,NULL))
    {
    	perror("pthread_creat error!");
      exit(-1);
    }     
    
     if(pthread_create(&thread_loop_print2,&attr2,(void*)pthread_handle_message3,NULL))
    {
    	perror("pthread_creat error!");
      exit(-1);
    }       
    
又加了个打印线程函数 如果, udp_process(); (这个函数里封装的线程),参与执行,则后面两个线程都不能执行, 如果 udp_process(); 这个先屏蔽掉,,后面两个线程都能各自打印 字符串信息出来,也就是后面两个线程OK 基本可以肯定 udp_process(); 这个函数里封装的线程的问题,,估计是哪里出了问题?
tiancai1912 2015-02-10
  • 打赏
  • 举报
回复
不会影响,线程是独立的

23,114

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧