getThreadNum() always returns 0 (Bug #3529)


Added by nicolas martin about 11 years ago. Updated about 11 years ago.


Status:Cancelled Start date:2014-02-07
Priority:Normal Due date:
Assignee:nicolas martin % Done:

0%

Category:core
Target version:-
Affected version:branch 'master' (3.0-dev) Operating System:Mac OSX
Difficulty: HW Platform:x64
Pull request:

Description

Hi,

I know that it depends on the underlying parallelism library chosen.
In my case, I am using tbb, on OsX.

The following code :

#include <opencv2/core/core.hpp>
#if CV_MAJOR_VERSION >= 3
#include <opencv2/core/utility.hpp>
#endif
#
#include <iostream>
#include <unistd.h>

using namespace std;
using namespace cv;

struct ThreadIdGetter
{
    ThreadIdGetter() {
        AutoLock lock(mutex);
        int i, count = used.size();
        for (i=0; i<count; ++i) if (!used[i]) { used[i] = true; break; }
        if (i==count) used.push_back(true);
        id = i;
    }
    ~ThreadIdGetter() {
        AutoLock lock(mutex);
        used[id] = false;
    }
    int operator()() const {
        return id;
    }
    int id;
    static vector<bool> used;
    static Mutex mutex;
};
vector<bool> ThreadIdGetter::used;
Mutex ThreadIdGetter::mutex;

struct Doer : public ParallelLoopBody {
    void operator()(const Range& range) const {
        ThreadIdGetter id;
        for (int i=range.start; i<range.end; ++i) {
            mutex.lock();
            cout << "Thread id : " << getThreadNum() << " " << id() << endl;
            mutex.unlock();
            usleep(rand()%5000+5000);
        }
    }
    mutable Mutex mutex;
};

int main(int argc, char *argv[]) {
    parallel_for_(Range(0,10), Doer(), 10);
    return 0;
}

yields, e.g, the following output :

Thread id : 0 0
Thread id : 0 1
Thread id : 0 2
Thread id : 0 3
Thread id : 0 2
Thread id : 0 1
Thread id : 0 0
Thread id : 0 3
Thread id : 0 1
Thread id : 0 2

The first number is returned by getThreadNum() and is always 0, while the second is returned from my code.
I used a simple structure with a critical section to get a unique id for each concurrent thread.
I guess this is not the best solution, but at least, it gives a different id for each thread that is executing in parallel.

Fixing this implies having a look at each implementation of getThreadNum() ? or maybe just tbb's is wrong ?


Associated revisions

Revision fd6ef87c
Added by Vadim Pisarevsky about 10 years ago

Merge pull request #3529 from jet47:fix-linux-install

History

Updated by Ilya Lavrenov about 11 years ago

Hi Nicolas,
getThreadNum() returns not 0 only if

#if TBB_INTERFACE_VERSION >= 6100 && defined TBB_PREVIEW_TASK_ARENA && TBB_PREVIEW_TASK_ARENA

is false. Are you sure that the condition is true in your case?

Updated by Kirill Kornyakov about 11 years ago

Nicolas, please reopen the ticket if you think there is an issue. Here is the complete implementation for the function, so it is easy for you to debug it:


int cv::getThreadNum(void)
{
#if defined HAVE_TBB
    #if TBB_INTERFACE_VERSION >= 6100 && defined TBB_PREVIEW_TASK_ARENA && TBB_PREVIEW_TASK_ARENA
        return tbb::task_arena::current_slot();
    #else
        return 0;
    #endif
#elif defined HAVE_CSTRIPES
    return pix();
#elif defined HAVE_OPENMP
    return omp_get_thread_num();
#elif defined HAVE_GCD
    return (int)(size_t)(void*)pthread_self(); // no zero-based indexing
#elif defined HAVE_CONCURRENCY
    return std::max(0, (int)Concurrency::Context::VirtualProcessorId()); // zero for master thread, unique number for others but not necessary 1,2,3,...
#else
    return 0;
#endif
}
  • Status changed from New to Cancelled
  • Assignee set to nicolas martin
  • Category set to core

Also available in: Atom PDF