线程同步,计算unix中boost多线程程序的时差

时间:2016-05-28 09:08:58

标签: c++ multithreading boost posix

boost::mutex time_;
void checkForDays(string dir,string hdir,int i)
{
..................
..................
boost::mutex::scoped_lock lock(time_);
if(lock)
    {
     try
     {
        t = last_write_time( <file> ) ;
     }
     catch (const boost::filesystem::filesystem_error& e)
     {
        out << currentDateTime() << " [ERROR]: " << e.what() << std::endl;//throw the exceptions if any
     }

     n = time( 0 ) ;
     diff = n - t;
    **diff = (diff / 86400);**
    } //scope of lock ends here

    **if(diff < <somedate>)**
    {
     .. do something ..
    }
    else{
    .. do something 
    }
}

int main()
{
........................
........................
    while(k<totalcount)
    {

     for (int i=0 ; i<2 ; i++ , k++)
     {
     workerThreads[i]=boost::thread (boost::bind(checkForDays,str_temp_out,str_temp_hist,k));
     }
     for (int z=0 ; z<2 ; z++)
     {
     workerThreads[z].join();
      }
    }
}

对于较小的输入,计算的时差似乎完全正常。但是对于指数级增长的大量投入,计算出的时差似乎并不合适。

我们如何为这部分关键部分保持完美的线程同步?我们应该static_cast&lt;&gt;在为每个线程创建的本地堆栈空间上从time_t到long int的diff值?

0 个答案:

没有答案