?
Solved

translating some win32 thread stuff to pthreads

Posted on 2007-10-12
2
Medium Priority
?
182 Views
Last Modified: 2012-05-05
Hi,

I have this bit of code that I've been using for awhile which creates a thread pool of 100 threads. When requests come in to create a new thread, the new thread is created only if there is an open slot in the thread pool. Otherwise the function waits for a slot to become available and will create the thread in that slot:


MyClass::MyClass()
{
    m_nOpenSlot = 0;
    m_nOpenThreads = 0;
    m_vThreads.resize(100, 0); // vector<HANDLE>, the thread pool.
}    

int MyClass::GetMaxNumberOfAllowedThreads()
{
    return 100;
}

void MyClass::CreateNewThread()
{
    // If there are already 100 threads open,  wait until one finishes then use that opened slot.
    if (m_nOpenThreads >= GetMaxNumberOfAllowedThreads()) {
        DWORD dwRes = WaitForMultipleObjects(GetMaxNumberOfAllowedThreads(), &m_vThreads[0], FALSE, INFINITE);
        m_nOpenSlot = dwRes - WAIT_OBJECT_0;
        m_nOpenThreads--;
        CloseHandle(m_vThreads[m_nOpenSlot]);
    }

    // Let derived classes create their own specialized thread data to handle the association.
    // It is treated as a base class type.
    SomeData* p = new SomeData();

    m_vThreads[m_nOpenSlot] = (HANDLE)_beginthreadex( (void *) 0,
                                                    (unsigned) 0,
                                              (PTHREAD_START) &ThreadFunction,
                                                      (void *) p,
                                                    (unsigned) 0,
                                                               0);

    m_nOpenSlot++;
    m_nOpenThreads++;
}


I don't know what the equivalents is in pthreads for:

    DWORD dwRes = WaitForMultipleObjects(GetMaxNumberOfAllowedThreads(), &m_vThreads[0], FALSE, INFINITE);
    m_nOpenSlot = dwRes - WAIT_OBJECT_0;

is there some one to one mapping for it or a completely new methodology?

Thanks
0
Comment
Question by:DJ_AM_Juicebox
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 20067832
Unfortunately, there is no such equivalent for pthreads (see e.g. the discussion at http://developers.slashdot.org/article.pl?sid=07/02/26/1211220). As an alternative, take a look at the article http://www.codeproject.com/threads/threads.asp ("A Thread Pool compatible with Win32 and pthreads API")
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 20079128
that is a shame!

Thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question