Example of a Multithreaded C program In answering password cracker in c with multithreading, I ended up writing a sample in C, which is not my forte.

Is there anything that I missed which should have been included in a responsible sample (read: likely to be copied and pasted into production code)?

// compile with `gcc counter.c -o counter -lpthread -Wall -std=gnu99`
#include &lt;pthread.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdbool.h&gt;
#include &lt;limits.h&gt;

#define SEARCH_THREAD_COUNT (10)
#define SEARCH_VALUE (21325)
#define SEARCH_MAX (INT_MAX)

typedef struct {
    int start;
    int end;
    int search;

    bool* shouldStop;
    int* answer;
    pthread_mutex_t* answerMutex;
    pthread_cond_t* answerFound;

    pthread_t thisThread;
} find_worker_init;

void* find_value(void* vpInitInfo) {
    find_worker_init* pUnitOfWork = (find_worker_init*)vpInitInfo;

    int cValue = pUnitOfWork-&gt;start;
    while (!(*(pUnitOfWork-&gt;shouldStop))) {
        if (cValue == pUnitOfWork-&gt;search) {
            printf("found value\n");

            // found the search value
            pthread_mutex_lock(pUnitOfWork-&gt;answerMutex);
            if (!(*(pUnitOfWork-&gt;shouldStop))) {
                *(pUnitOfWork-&gt;shouldStop) = true;
                *(pUnitOfWork-&gt;answer) = cValue;

                pthread_cond_broadcast(pUnitOfWork-&gt;answerFound);
            }
            pthread_mutex_unlock(pUnitOfWork-&gt;answerMutex);
            return NULL;
        }

        cValue++;
        if (cValue == pUnitOfWork-&gt;end) {
            // we exhausted our search space, end.
            printf("exhausted\n");
            return NULL;
        }
    }

    // we were usurped by another thread
    printf("usurped\n");
    return NULL;
}

int main( int argc, const char* argv[] ) {
    pthread_mutex_t answerMutex = PTHREAD_MUTEX_INITIALIZER;
    pthread_cond_t answerFound = PTHREAD_COND_INITIALIZER;
    bool shouldStop = false;
    int answer = -1;

    // initialize thread jobs
    find_worker_init startInfo[SEARCH_THREAD_COUNT];
    int current_search_start = 0;
    int search_unit = SEARCH_MAX / SEARCH_THREAD_COUNT;
    for (int i = 0; i &lt; SEARCH_THREAD_COUNT; i++) {
        startInfo[i].start = current_search_start;

        // set the search space for this thread to be either the standard
        // search space, or for the last thread, whatever is remaining
        // (this is to prevent integer-division from skipping a portion
        // at the end)
        int current_size = (i == SEARCH_THREAD_COUNT - 1) 
            ? (SEARCH_MAX - current_search_start) 
            : search_unit;

        startInfo[i].end = current_search_start + current_size;
        startInfo[i].search = SEARCH_VALUE;

        startInfo[i].shouldStop = &amp;shouldStop;
        startInfo[i].answer = &amp;answer;
        startInfo[i].answerMutex = &amp;answerMutex;
        startInfo[i].answerFound = &amp;answerFound;
    }

    // start threads
    for (int i = 0; i &lt; SEARCH_THREAD_COUNT; i++) {
        if (pthread_create(&amp;(startInfo[i].thisThread), NULL, find_value, 
            &amp;(startInfo[i]))) 
        {
            fprintf(stderr, "Error creating thread\n");
            return 1;
        }
    }

    // wait for answer to be found
    pthread_mutex_lock(&amp;answerMutex);
    while (!shouldStop) {
        pthread_cond_wait(&amp;answerFound, &amp;answerMutex);
    }
    printf("signaled\n");

    // join
    for (int i = 0; i &lt; SEARCH_THREAD_COUNT; i++) {
        if (pthread_join(startInfo[i].thisThread, NULL)) {
            fprintf(stderr, "Error joining thread\n");
            return 1;
        }
    }

    // answer
    printf("answer: %d\n", answer);

    pthread_mutex_unlock(&amp;answerMutex);

    return 0;
}
Mitch, you have a nice program there.  As I pointed out above, current_search_start is not updated which means 9 of 10 threads are searching the same space and the last searches the whole space.  Minor error though.

Your use of condition variables and mutexes is nicely done.  Overall, nothing significant to comment on. 

Some minor comments:


The precedence of -&gt; is higher than that of * so this:

if (!(*(pUnitOfWork-&gt;shouldStop))) {


can be written: 

if (!(*pUnitOfWork-&gt;shouldStop)) {


or some might prefer:

if (*pUnitOfWork-&gt;shouldStop == false) {


And 

*(pUnitOfWork-&gt;shouldStop) = true;


can omit the parenthesis and be neater.
I would change some variable names.  cValue seems meaningless. pUnitOfWork would be just work.  The p (and vp) prefixes you used look like Hungarian.  I would never use such things unless forced to by an outdated coding convention.
find_value should strictly be static
The loop in find_value would be more normal as a for:

for (int c = work-&gt;start; c &lt; work-&gt;end; ++c) {
    if (*work-&gt;shouldStop) { ... }
    if (c == work-&gt;search) { ... }
}



Also I would add a thread number and some debug to see which thread number is successful (especially with the current_search_start bug, as the thread is not always the same :-)