也许我对线程感到困惑,但我对线程的理解却相互冲突。
我创建了一个使用POSIX pthread
的程序。如果不使用这些线程,程序运行需要0.061723秒,并且线程需要0.081061秒才能运行。
起初我认为这是应该发生的事情,因为线程允许某些事情发生,而其他事情应该能够发生。即,在一个线程上处理大量数据,同时仍然在另一个线程上具有响应UI,这意味着当CPU在处理UI和处理数据之间划分时间时,数据的处理将花费更长的时间。
但是,多线程的关键是让程序利用多个CPU /核心?
正如你所知道的那样,如果这是一个简单的问题,那我就是一个中间人,请原谅。
但是我应该期待该计划做什么?
我在2012年中期的Macbook Pro 13“基础型号上运行它。 CPU是22纳米“Ivy Bridge”2.5 GHz英特尔“酷睿i5”处理器(3210M),在单个硅芯片上有两个独立的处理器“核心”
更新代码
这是主要功能。为方便起见,我没有添加变量声明,但我确信你可以通过它的名称来解决每个人所做的事情:
// Loop through all items we need to process
//
while (totalNumberOfItemsToProcess > 0 && numberOfItemsToProcessOnEachIteration > 0 && startingIndex <= totalNumberOfItemsToProcess)
{
// As long as we have items to process...
//
// Align the index with number of items to process per iteration
//
const uint endIndex = startingIndex + (numberOfItemsToProcessOnEachIteration - 1);
// Create range
//
Range range = RangeMake(startingIndex,
endIndex);
rangesProcessed[i] = range;
// Create thread
//
// Create a thread identifier, 'newThread'
//
pthread_t newThread;
// Create thread with range
//
int threadStatus = pthread_create(&newThread, NULL, processCoordinatesInRangePointer, &rangesProcessed[i]);
if (threadStatus != 0)
{
std::cout << "Failed to create thread" << std::endl;
exit(1);
}
// Add thread to threads
//
threadIDs.push_back(newThread);
// Setup next iteration
//
// Starting index
//
// Realign the index with number of items to process per iteration
//
startingIndex = (endIndex + 1);
// Number of items to process on each iteration
//
if (startingIndex > (totalNumberOfItemsToProcess - numberOfItemsToProcessOnEachIteration))
{
// If the total number of items to process is less than the number of items to process on each iteration
//
numberOfItemsToProcessOnEachIteration = totalNumberOfItemsToProcess - startingIndex;
}
// Increment index
//
i++;
}
std::cout << "Number of threads: " << threadIDs.size() << std::endl;
// Loop through all threads, rejoining them back up
//
for ( size_t i = 0;
i < threadIDs.size();
i++ )
{
// Wait for each thread to finish before returning
//
pthread_t currentThreadID = threadIDs[i];
int joinStatus = pthread_join(currentThreadID, NULL);
if (joinStatus != 0)
{
std::cout << "Thread join failed" << std::endl;
exit(1);
}
}
处理功能:
void processCoordinatesAtIndex(uint index)
{
const int previousIndex = (index - 1);
// Get coordinates from terrain
//
Coordinate3D previousCoordinate = terrain[previousIndex];
Coordinate3D currentCoordinate = terrain[index];
// Calculate...
//
// Euclidean distance
//
double euclideanDistance = Coordinate3DEuclideanDistanceBetweenPoints(previousCoordinate, currentCoordinate);
euclideanDistances[index] = euclideanDistance;
// Angle of slope
//
double slopeAngle = Coordinate3DAngleOfSlopeBetweenPoints(previousCoordinate, currentCoordinate, false);
slopeAngles[index] = slopeAngle;
}
void processCoordinatesInRange(Range range)
{
for ( uint i = range.min;
i <= range.max;
i++ )
{
processCoordinatesAtIndex(i);
}
}
void *processCoordinatesInRangePointer(void *threadID)
{
// Cast the pointer to the right type
//
struct Range *range = (struct Range *)threadID;
processCoordinatesInRange(*range);
return NULL;
}
更新:
以下是我的全局变量,为了简单起见,它们只是全局变量 - 没有去!
std::vector<Coordinate3D> terrain;
std::vector<double> euclideanDistances;
std::vector<double> slopeAngles;
std::vector<Range> rangesProcessed;
std::vector<pthread_t> threadIDs;
答案 0 :(得分:0)
如果我错了,请纠正我,但是,我认为问题在于时间的流逝是如何衡量的。我没有使用clock_t
移动到gettimeofday()
,而是报告的时间更短,从22.629000 ms
的非线程时间到8.599000 ms
的线程时间。
这对人们来说是否正确?
当然,我最初的问题是基于多线程程序是否应该更快,所以我不会因为这个原因将这个答案标记为正确答案。