[问题]一个线程初级问题
- Roots
- 帖子: 662
- 注册时间: 2005-08-17 19:20
[问题]一个线程初级问题
在C程序中第一次使两个线程对同一个变量操作,看下这样用是否有问题,会不会产生混乱:
int g_num = 0;
线程1:
{
while(1)
{
if(g_num <= 15)
{
g_num++;
}
sleep(1);
}
}
主程序:
{
……
……
if(g_num >= 15)
{
g_num = 0;
}
}
int g_num = 0;
线程1:
{
while(1)
{
if(g_num <= 15)
{
g_num++;
}
sleep(1);
}
}
主程序:
{
……
……
if(g_num >= 15)
{
g_num = 0;
}
}
上次由 Roots 在 2008-08-10 19:36,总共编辑 1 次。
- BigSnake.NET
- 帖子: 12522
- 注册时间: 2006-07-02 11:16
- 来自: 廣州
- 联系:
- Roots
- 帖子: 662
- 注册时间: 2005-08-17 19:20
- kofshower
- 帖子: 1343
- 注册时间: 2007-03-13 11:23
- 联系:
怎么能这么写多线程.线程1和主线程的操作是atom操作么?
"We are all in the mud, but some of us are looking at the stars." (Oscar Wilde)
We are not born for ourselves.
人生天地间,并非为自己
Homepage:http://sites.google.com/site/polarisnotme/
We are not born for ourselves.
人生天地间,并非为自己
Homepage:http://sites.google.com/site/polarisnotme/
- Roots
- 帖子: 662
- 注册时间: 2005-08-17 19:20
- BigSnake.NET
- 帖子: 12522
- 注册时间: 2006-07-02 11:16
- 来自: 廣州
- 联系:
多线程操作共享对象很易出问题的Roots 写了:不是很懂俄,
线程有线程1其实主要是敢干的事,定时每秒一次,因为主程序要用到一个时间判断,为方便所以把这个加线程里了
例如
代码: 全选
if(g_num <= 15)
{
# 另一个线程在这时 g_num++ ...
g_num++;
}
^_^ ~~~
要理解递归,首先要理解递归。
地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
要理解递归,首先要理解递归。
地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
- kofshower
- 帖子: 1343
- 注册时间: 2007-03-13 11:23
- 联系:
下面给你贴的是Netscape的代码
为什么人家不和你一样直接
xxx++了事.
你贴的代码问题很多.
------------------------------------------------------------------
当然人人家的代码是为了跨平台,这样的实现不是很高效
代码: 全选
165 PRInt32
166 _PR_MD_ATOMIC_INCREMENT(PRInt32 *val)
167 {
168 PRInt32 rv;
169 PRInt32 idx = _PR_HASH_FOR_LOCK(val);
170
171 pthread_mutex_lock(&atomic_locks[idx]);
172 rv = ++(*val);
173 #ifdef DEBUG
174 hash_lock_counts[idx]++;
175 #endif
176 pthread_mutex_unlock(&atomic_locks[idx]);
177 return rv;
178 }
xxx++了事.
你贴的代码问题很多.
------------------------------------------------------------------
当然人人家的代码是为了跨平台,这样的实现不是很高效
"We are all in the mud, but some of us are looking at the stars." (Oscar Wilde)
We are not born for ourselves.
人生天地间,并非为自己
Homepage:http://sites.google.com/site/polarisnotme/
We are not born for ourselves.
人生天地间,并非为自己
Homepage:http://sites.google.com/site/polarisnotme/
- Roots
- 帖子: 662
- 注册时间: 2005-08-17 19:20
- kofshower
- 帖子: 1343
- 注册时间: 2007-03-13 11:23
- 联系:
小心循环锁
"We are all in the mud, but some of us are looking at the stars." (Oscar Wilde)
We are not born for ourselves.
人生天地间,并非为自己
Homepage:http://sites.google.com/site/polarisnotme/
We are not born for ourselves.
人生天地间,并非为自己
Homepage:http://sites.google.com/site/polarisnotme/
-
- 帖子: 2841
- 注册时间: 2006-09-11 22:47
- shellex
- 帖子: 2180
- 注册时间: 2007-02-18 19:33
- 系统: OSX
- 来自: lyric.im
- 联系:
- kofshower
- 帖子: 1343
- 注册时间: 2007-03-13 11:23
- 联系:
我也认为这个编码风格不行poet 写了:你这个地方,因为只有一个线程进行写操作,不锁是没有问题的。虽然++不是原子操作但对此没有影响。
不过因为你一边用 >= 15, 另一边用 <= 15,导致了实际运行结果的不确定(15还是16次,都有可能)。也许你可以无所谓一个tick的sleep,但是这种散漫的编程习惯似乎很有问题。
"We are all in the mud, but some of us are looking at the stars." (Oscar Wilde)
We are not born for ourselves.
人生天地间,并非为自己
Homepage:http://sites.google.com/site/polarisnotme/
We are not born for ourselves.
人生天地间,并非为自己
Homepage:http://sites.google.com/site/polarisnotme/
- Roots
- 帖子: 662
- 注册时间: 2005-08-17 19:20