pthread返回意外结果 [英] pthread returns unexpected results

查看:69
本文介绍了pthread返回意外结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我试图在linux中启动一些线程,由于某种原因,我试图在启动项目之前测试pthread.在我的测试中,我编写了一个简单的代码,其中有2个线程在其中启动,其中一个增加了一个通用计数器,另一个减少了该计数器,最后该计数器应为零,但有时为-10,-7, 10,6等等.有什么问题?我认为有时其中一个线程失败或某些原因.我该怎么做?
谢谢.

代码:

Hi,
I am trying to start some threads in linux, for some reason I tried to test pthread before starting my project. in my test I have written a simple code that 2 thread starts in it and one of them increases a universal counter and other one decreases it, at the end the counter should be zero but some times it''s -10,-7,10,6 and so. what''s the problem? I think some times one of the threads fails or something. How can I make it right?
Thanks.

Code:

#include<iostream>
using namespace std;
#include<pthread.h>

int count=0;
void* func1(void *arg)
{
for(int i=0;i<10;i++)
   count++;
cout<<endl;
}

void* func2(void *arg)
{
for(int i=0;i<10;i++)
   count--;
cout<<endl;
}

int main()
{
pthread_t t1;
pthread_t t2;
for(int i=0;i<10;i++)
{
pthread_create(&t2,NULL,func2,NULL);
pthread_create(&t1,NULL,func1,NULL);
pthread_join(t1,NULL);
pthread_join(t2,NULL);
cout<<count<<endl;
count=0;
}
return(0);
}

推荐答案

修改来自两个或多个不同线程的任何类型的数据时,都必须使用锁来保护数据.您只能在当前线程成功获取锁之后修改数据,然后在对数据执行写操作后必须释放锁.这样,一次只有一个线程可以写入数据.在您的情况下,这种灾难可能会发生:
1.您的计数器为零.
2.线程1和2同时将这个零值读出到处理器中的某个寄存器.
3.线程1将值增加到1,线程2将值减少到负1.
4.两个线程都写回自己的值,并且其中一个将获胜(随机结果为-1或1).
使用锁,您可以实现在一个线程正在读取/修改/写入int的同时,另一个线程阻塞了锁的获取方法.
对于简单的整数值,您可以使用gcc原子内建函数,因为它们通过锁定递增/递减整数值,因此将在您的情况下很好地起作用:http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html [
When you modify any kind of data from two or more different threads, then you have to protect the data with a lock. You can only modify the data when the current thread has successfully acquired the lock, then you have to release the lock after performing the write on the data. This way only one thread can write the data at a time. In your situation this disaster can happen:
1. Your counter is zero.
2. Both thread 1 and 2 read out this zero value at the same time to some register in the processor.
3. Thread 1 increases the value to one, and thread 2 decreses it to minus one.
4. Both threads write back their own value and one of them will win (random result, either -1 or 1).
With a lock you can achieve that while one of the threads is reading/modifying/writing the int, the other thread is blocking on the acquire method of the lock.
For simple integral values you can use gcc atomic builtins that will work well in your situation because they increment/decrement the integer values with locking: http://gcc.gnu.org/onlinedocs/gcc-4.1.1/gcc/Atomic-Builtins.html[^]

I also recommend you to get some lessons on multithreading because with your current knowledge you will produce hell buggy multithreaded code and threading bugs are often hard to reproduce/find. Some threading bugs occur only once a month or so but then they can deadlock your program!


您假设您的线程将按顺序执行...
像Thread1 .... Thread2 ..... Thread1 ..... Thread2 ...等,

但是在多线程的情况下,线程的执行取决于操作系统的时间表.线程可以任何方式执行.无法保证每个线程将获得相等的cpu时间.因此,线程之一可能比其他线程获得更多的cpu时间.

经验法则是,不要在线程的执行顺序上建立程序逻辑.

如果您有相互依赖的线程,则需要使用某种同步机制,例如互斥体,信号.小心使用.(如果使用不当,可能会导致死锁)
You are assuming that your threads will execute in a sequence .....
like Thread1 .... Thread2.....Thread1.....Thread2......and so on

But in case of multithreading , execution of thread is dependent on the OS schedular. Threads can execute in any manner. There is no guarantee that each thread will get equal amount of cpu time. Hence it is possible that one of thread might get more cpu time than other one.

Thumb rule is , do not build your program logic on the sequence of execution of threads.

If you have threads which are dependent on each other, you will need to use some kind of synchronization mechanism like mutex, signals. Use them cautiously.(They may cause deadlock if used carelessly)


这篇关于pthread返回意外结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆