pthread_cond_wait唤醒很多线程的例子

时间:2015-04-08 06:06:57

标签: c++ pthreads mingw mutex mingw32

pthread_cond_wait唤醒许多线程示例

唤醒线程1&的代码3来自线程0的一些广播。

设置:带有mingw32的Win7,带有mingw32-pthreads-w32的g ++ 4.8.1 pthread条件变量

解决方案: http://pastebin.com/X8aQ5Fz8

#include <iostream>
#include <string>
#include <list>
#include <map>
#include <pthread.h>
#include <fstream>

#include <sstream> // for ostringstream

#define N_THREAD 7

using namespace std;

// Prototypes
int main();
int scheduler();
void *worker_thread(void *ptr);
string atomic_output(int my_int, int thread_id);

// Global variables
//pthread_t thread0, thread1, thread2, thread3, thread4, thread5, thread6, thread7;

pthread_t m_thread[N_THREAD];
int count = 1;
pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t  condition_var   = PTHREAD_COND_INITIALIZER;


// Main
int main() {

    cout << "Launching main. \n";

    //Start to monitor for exceptions
    register_exception_handler();

    //Start scheduler
    scheduler();

    return 0;
}


// Scheduler
int scheduler() {
    // Starting scheduler log file
    ofstream scheduler_log;
    scheduler_log.open ("scheduler_log.txt");
    //scheduler_log << "[Scheduler] Starting." << endl;
    cout << "[Scheduler] Starting.  \n";

    // Scheduler::Main Section

    int thread_id[N_THREAD];

    for(int i=0;i<N_THREAD;i++) {
        thread_id[i] = i;
        pthread_create( &m_thread[i], NULL, worker_thread, (void *) &thread_id[i]);
    }

    for(int i=0;i<N_THREAD;i++)
        pthread_join(m_thread[i], NULL);


    cout << "[Scheduler] Ending. \n";
    // Closing scheduler log file
    scheduler_log.close();

    return 0;
}

string atomic_output(int my_int, int thread_id) {
    ostringstream stm;
    stm << "Thread ";
    stm << thread_id;
    stm << ": ";


    //count fn
    stm << my_int;
    stm << "\n";


    //stm << "Finished. \n";

    return stm.str();
}

void *worker_thread(void *ptr) {
    string line;
    //int boo = 0;

    int thread_id = *(int *) ptr;

    //if(thread_id == 0)
    //  pthread_mutex_lock( &count_mutex );

    for(int i=0;i<10;i++) {
        //boo++;

        if (thread_id == 1) {

            pthread_mutex_lock(&count_mutex);
            while (count == 1) {
                cout << "[Thread 1] Before pthread_cond_wait...\n";
                pthread_cond_wait( &condition_var, &count_mutex );
                cout << "[Thread 1] After pthread_cond_wait...\n";
            }
            pthread_mutex_unlock(&count_mutex);

        }

        if (thread_id == 3) {

            pthread_mutex_lock(&count_mutex);
            while (count == 1) {
                cout << "[Thread 3] Before pthread_cond_wait...\n";
                pthread_cond_wait( &condition_var, &count_mutex );
                cout << "[Thread 3] After pthread_cond_wait...\n";
            }
            pthread_mutex_unlock(&count_mutex);
        }

        //count fn
        line = atomic_output(i, *(int *)ptr);
        cout << line;   

        if (i == 5) {
            if(thread_id == 0) {
                pthread_mutex_lock( &count_mutex );
                count = 0;
                pthread_mutex_unlock( &count_mutex );
                pthread_cond_broadcast(&condition_var);
            }
        }



    }

    //line = atomic_output(0, *(int *)ptr);
    //cout << line;
}

(旧) - =我尝试了什么= -

*编辑:代码中的早期问题有while(0)而不是while(谓词)。保持它在那里以便于参考评论。

代码1:http://pastebin.com/rCbYjPKi

我尝试了while(0)pthread_cond_wait(&amp; condition_var,&amp; count_mutex); 使用pthread_cond_broadcast(&amp; condition_var); ......线程不尊重条件。

条件证明不尊重:http://pastebin.com/GW1cg4fY

Thread 0: 0
Thread 0: 1
Thread 0: 2
Thread 0: 3
Thread 2: 0
Thread 6: 0
Thread 1: 0 <-- Here, Thread 1 is not supposed to tick before Thread 0 hit 5. Thread 0 is at 3.

代码2:http://pastebin.com/g3E0Mw9W

我试过pthread_cond_wait(&amp; condition_var,&amp; count_mutex);在线程1和3中,程序不会返回。

线程1或线程3永远等待。甚至使用广播说它应该唤醒所有等待的线程。显然有些东西不起作用,代码还是lib?

更多:

我先尝试解锁互斥锁,然后播放。我试过播放然后解锁。两个都不工作。

我试图使用信号而不是广播,同样的问题。

我无法开展工作的参考资料(热门谷歌搜索)

http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html

http://docs.oracle.com/cd/E19455-01/806-5257/6je9h032r/index.html

http://www-01.ibm.com/support/knowledgecenter/ssw_i5_54/apis/users_76.htm

代码3:http://pastebin.com/tKP7F8a8

尝试使用谓词变量计数来修复竞争问题。还是一个问题,当thread0介于0和5之间时,不会阻止thread1和thread3运行。

唤醒线程1&amp;的代码是什么? 3来自thread0的某些函数调用

1 个答案:

答案 0 :(得分:2)

if(thread_id == 0)
    pthread_mutex_lock( &count_mutex );

for(int i=0;i<10;i++) {
    //boo++;

    if (thread_id == 1) {
        while(0)
            pthread_cond_wait( &condition_var, &count_mutex );
    }

这些都没有任何意义。等待条件变量的正确方法是:

pthread_mutex_lock(&mutex_associated_with_condition_variable);
while (!predicate)
    pthread_cond_wait(&condition_variable, mutex_associated_with_condition_variable);

注意:

  1. 必须锁定互斥锁。
  2. 在等待之前必须检查谓词(您正在等待的事物)。
  3. 等待必须循环。
  4. 违反这三条规则中的任何一条都会导致您遇到的问题。你的主要问题是你打破了第二条规则,等待你想要等待的东西已经发生。