多线程程序未产生所需的输出
Multithreaded program not producing desired output
我正在编写一个代码,它创建 10 个线程并首先执行那些线程 ID 为偶数的线程,然后执行所有线程 ID 为奇数的线程。我正在使用 POSIX 线程库。这是我写的代码:
#include "stdlib.h"
#include "pthread.h"
#include "stdio.h"
#define TRUE 1
#define FALSE 0
int EVEN_DONE = FALSE;
int evenThreads, oddThreads = 0;
int currentThread = 0;
//the mutex for thread synchronization
static pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
//the condition variable;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void * printEven(unsigned long id)
{
pthread_mutex_lock(&mymutex);
evenThreads++;
printf("TID: %lu, Hello from even\n", id);
// this condition checks whether even threads have finished executing
if(evenThreads + oddThreads >= 10) {
EVEN_DONE = TRUE;
pthread_cond_broadcast(&cond);
}
pthread_mutex_unlock(&mymutex);
return NULL;
}
void * printOdd(unsigned long id)
{
pthread_mutex_lock(&mymutex);
while (!EVEN_DONE) {
oddThreads++;
pthread_cond_wait(&cond, &mymutex);
printf("TID: %lu, Hello from odd\n", id);
}
pthread_mutex_unlock(&mymutex);
return NULL;
}
void * threadFunc(void *arg)
{
unsigned long id = (unsigned long)pthread_self();
if (id % 2 == 0)
{
printEven(id);
}
else
{
printOdd(id);
}
return NULL;
}
int main()
{
pthread_t* threads;
int num_threads = 10;
int i, j;
threads = malloc(num_threads * sizeof(threads));
for ( i = 0; i < 10; i++) {
pthread_create(&threads[i], NULL, threadFunc, NULL);
}
for ( j = 0; j < 10; j++) {
pthread_join(threads[j], NULL);
}
printf("Finished executing all threads\n");
return 0;
}
但是,当我 运行 代码时,它不会产生所需的输出。我得到的输出是这样的:
貌似所有的线程ID都是偶数。但是,我确实认为我的代码有问题。我究竟做错了什么?我怎样才能达到预期的输出?
(注意:就 POSIX 线程和一般的多线程而言,我处于初学者水平)
提前致谢。
在 POSIX 中无法保证 pthread_self()
返回的 pthread_t
类型是可以转换为 unsigned long
的数字类型 - 允许例如,是结构类型。
如果您想以符合 POSIX 的方式编写代码,您需要自己分配数字线程 ID。例如,您可以:
unsigned long allocate_id(void)
{
static unsigned long next_id = 0;
static pthread_mutex_t id_lock = PTHREAD_MUTEX_INITIALIZER;
unsigned long id;
pthread_mutex_lock(&id_lock);
id = next_id++;
pthread_mutex_unlock(&id_lock);
return id;
}
然后在你的线程中使用:
unsigned long id = allocate_id();
您自己控制 ID 的分配也允许您控制顺序 - 例如在这种情况下,您可以确保 ID 是按顺序分配的,这样您将同时拥有奇数和偶数 ID。
我正在编写一个代码,它创建 10 个线程并首先执行那些线程 ID 为偶数的线程,然后执行所有线程 ID 为奇数的线程。我正在使用 POSIX 线程库。这是我写的代码:
#include "stdlib.h"
#include "pthread.h"
#include "stdio.h"
#define TRUE 1
#define FALSE 0
int EVEN_DONE = FALSE;
int evenThreads, oddThreads = 0;
int currentThread = 0;
//the mutex for thread synchronization
static pthread_mutex_t mymutex = PTHREAD_MUTEX_INITIALIZER;
//the condition variable;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void * printEven(unsigned long id)
{
pthread_mutex_lock(&mymutex);
evenThreads++;
printf("TID: %lu, Hello from even\n", id);
// this condition checks whether even threads have finished executing
if(evenThreads + oddThreads >= 10) {
EVEN_DONE = TRUE;
pthread_cond_broadcast(&cond);
}
pthread_mutex_unlock(&mymutex);
return NULL;
}
void * printOdd(unsigned long id)
{
pthread_mutex_lock(&mymutex);
while (!EVEN_DONE) {
oddThreads++;
pthread_cond_wait(&cond, &mymutex);
printf("TID: %lu, Hello from odd\n", id);
}
pthread_mutex_unlock(&mymutex);
return NULL;
}
void * threadFunc(void *arg)
{
unsigned long id = (unsigned long)pthread_self();
if (id % 2 == 0)
{
printEven(id);
}
else
{
printOdd(id);
}
return NULL;
}
int main()
{
pthread_t* threads;
int num_threads = 10;
int i, j;
threads = malloc(num_threads * sizeof(threads));
for ( i = 0; i < 10; i++) {
pthread_create(&threads[i], NULL, threadFunc, NULL);
}
for ( j = 0; j < 10; j++) {
pthread_join(threads[j], NULL);
}
printf("Finished executing all threads\n");
return 0;
}
但是,当我 运行 代码时,它不会产生所需的输出。我得到的输出是这样的:
貌似所有的线程ID都是偶数。但是,我确实认为我的代码有问题。我究竟做错了什么?我怎样才能达到预期的输出?
(注意:就 POSIX 线程和一般的多线程而言,我处于初学者水平)
提前致谢。
在 POSIX 中无法保证 pthread_self()
返回的 pthread_t
类型是可以转换为 unsigned long
的数字类型 - 允许例如,是结构类型。
如果您想以符合 POSIX 的方式编写代码,您需要自己分配数字线程 ID。例如,您可以:
unsigned long allocate_id(void)
{
static unsigned long next_id = 0;
static pthread_mutex_t id_lock = PTHREAD_MUTEX_INITIALIZER;
unsigned long id;
pthread_mutex_lock(&id_lock);
id = next_id++;
pthread_mutex_unlock(&id_lock);
return id;
}
然后在你的线程中使用:
unsigned long id = allocate_id();
您自己控制 ID 的分配也允许您控制顺序 - 例如在这种情况下,您可以确保 ID 是按顺序分配的,这样您将同时拥有奇数和偶数 ID。