pthread_cond_wait 唤醒多线程示例
pthread_cond_wait wake many threads example
pthread_cond_wait 唤醒多线程示例
在线程 0 的广播中唤醒线程 1 和 3 的代码。
设置:Win7 和 mingw32,g++ 4.8.1 和 mingw32-pthreads-w32
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(predicate)。将其保留在那里以便于参考评论。
代码 1:http://pastebin.com/rCbYjPKi
我试着 while(0) pthread_cond_wait( &condition_var, &count_mutex );
用 pthread_cond_broadcast(&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( &condition_var, &count_mutex );在线程 1 和 3 中并且程序没有 return.
线程 1 或线程 3 永远等待。即使使用广播,它也应该唤醒所有等待的线程。显然有些东西不工作,代码或库?
更多:
我试过先解锁互斥量,然后广播。我试过广播然后解锁。两个都不行。
我试过使用信号而不是广播,同样的问题。
我无法完成的参考文献(热门 google 搜索)
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 运行。
在线程 0 的某些函数调用中唤醒线程 1 和 3 的代码是什么
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 );
}
None 这句话说得通。等待条件变量的正确方法是:
pthread_mutex_lock(&mutex_associated_with_condition_variable);
while (!predicate)
pthread_cond_wait(&condition_variable, mutex_associated_with_condition_variable);
通知:
- 必须锁定互斥量。
- 在等待之前必须检查谓词(你正在等待的东西)。
- 等待必须在循环中。
违反这三个规则中的任何一个都会导致您所看到的问题。你的主要问题是你违反了第二条规则,即使你想等待的事情已经发生了,你仍然在等待。
pthread_cond_wait 唤醒多线程示例
在线程 0 的广播中唤醒线程 1 和 3 的代码。
设置:Win7 和 mingw32,g++ 4.8.1 和 mingw32-pthreads-w32 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(predicate)。将其保留在那里以便于参考评论。
代码 1:http://pastebin.com/rCbYjPKi
我试着 while(0) pthread_cond_wait( &condition_var, &count_mutex ); 用 pthread_cond_broadcast(&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( &condition_var, &count_mutex );在线程 1 和 3 中并且程序没有 return.
线程 1 或线程 3 永远等待。即使使用广播,它也应该唤醒所有等待的线程。显然有些东西不工作,代码或库?
更多:
我试过先解锁互斥量,然后广播。我试过广播然后解锁。两个都不行。
我试过使用信号而不是广播,同样的问题。
我无法完成的参考文献(热门 google 搜索)
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 运行。
在线程 0 的某些函数调用中唤醒线程 1 和 3 的代码是什么
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 );
}
None 这句话说得通。等待条件变量的正确方法是:
pthread_mutex_lock(&mutex_associated_with_condition_variable);
while (!predicate)
pthread_cond_wait(&condition_variable, mutex_associated_with_condition_variable);
通知:
- 必须锁定互斥量。
- 在等待之前必须检查谓词(你正在等待的东西)。
- 等待必须在循环中。
违反这三个规则中的任何一个都会导致您所看到的问题。你的主要问题是你违反了第二条规则,即使你想等待的事情已经发生了,你仍然在等待。