2016-03-08 49 views
1

我有这个C程序,它必须以0-6的顺序显示线程。我正在使用互斥锁,但是当我尝试运行我的代码时,没有任何反应,没有任何显示。此外,编译器显示没有错误使用互斥对订单中的线程进行同步

我用锁和解锁互斥量,但我不知道如果我在正确的地方创建它。 任何建议和帮助表示赞赏。

#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <pthread.h> 
#include <time.h> 


void *text(void *arg); 
long code[] = { 4, 6, 3, 1, 5, 0, 2 }; // Order in which to start   threads 
int num = 0; 

pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER; 


int main() 
{ 
int i; 
pthread_t tid[7]; 
// Initialize random number generator 
time_t seconds; 
time(&seconds); 
srand((unsigned int) seconds); 

int rc; 



// Create our threads 
for (i = 0; i < 7; i++) 
{ 
    pthread_create(&tid[i], NULL, text, (void*)code[i]); 

    for (i = 0; i < 7; i++) 

    { rc = pthread_mutex_lock(&a_mutex); 

     for (i = 0; i < 7; i++) 
     { 
      rc = pthread_mutex_unlock(&a_mutex); 
     } 
    } 

} 
//join threads 
for (i=0; i<7; i++) 
{ 
    if (pthread_join(tid[i], NULL)); 
       } 

rc = pthread_mutex_destroy(&a_mutex); 

// Exit main 
return 0; 

} 
void *text(void *arg) 
{ 
long n = (long)arg; 
int rand_sec = rand() % (3 - 1 + 1) + 1; // Random num seconds to  sleep 
while (num != n) {} // Busy wait used to wait for our turn 
num++; // Let next thread go 
sleep(rand_sec); // Sleep for random amount of time 
printf("This is thread %d.\n", n); 
// Exit thread 
pthread_exit(0); 
} 

回答

1

找出问题的关键是问自己“问题是我的6个线程中共享的数据是什么?” 是在锁定的互斥体块中需要互斥保护(读取和写入)的变量。目前,您只能锁定和解锁来自单个主线程的互斥锁,这实际上什么都不做。

你可能想要的东西更接近这个(虽然这可以大大简化 - 例如,可以彻底删除休眠):

#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <pthread.h> 
#include <time.h> 

void *text(void *arg); 
long code[] = { 4, 6, 3, 1, 5, 0, 2 }; // Order in which to start threads 
int num = 0; 

pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER; 

int main() 
{ 
    int i; 
    pthread_t tid[7]; 
    // Initialize random number generator 
    time_t seconds; 
    time(&seconds); 
    srand((unsigned int) seconds); 
    // Create our threads 
    for (i = 0; i < 7; i++) 
     pthread_create(&tid[i], NULL, text, (void*)code[i]); 

    //join threads 
    for (i=0; i<7; i++) 
     if (pthread_join(tid[i], NULL)); 

    pthread_mutex_destroy(&a_mutex); 

    // Exit main 
    return 0; 
} 

void *text(void *arg) 
{ 
    long n = (long)arg; 
    long localNum = -1; 
    int rand_sec = rand() % (3 - 1 + 1) + 1; // Random num seconds to  sleep 
    int rc; 
    while (localNum != n) 
    { 
     rc = pthread_mutex_lock(&a_mutex); 
     localNum = num; 
     rc = pthread_mutex_unlock(&a_mutex); 
     sleep(rand_sec); // Sleep for random amount of time 
    } 

    printf("This is thread %d.\n", n); 

    rc = pthread_mutex_lock(&a_mutex); 
    num++; // Let next thread go 
    rc = pthread_mutex_unlock(&a_mutex); 

    pthread_exit(0); 
} 
1

这里去的代码的最小版本,去掉了等待和随机化因为它们将注意力从锁的工作方式转移。

线程自动排队等待互斥锁。

您需要询问常规功能中的锁。

一定要记住关闭线程并释放所有可能的代码路径上的锁。

#include <stdlib.h> 
#include <stdio.h> 
#include <unistd.h> 
#include <pthread.h> 
#include <time.h> 


void *text(void *arg); 
long code[] = { 4, 6, 3, 1, 5, 0, 2 }; // Order in which to start   threads 
int num = 0; 

pthread_mutex_t a_mutex = PTHREAD_MUTEX_INITIALIZER; 


int main() 
{ 
int i; 
pthread_t tid[7]; 

// Create our threads 
for (i = 0; i < 7; i++) 
{ 
    pthread_create(&tid[i], NULL, text, (void*) & code[i]); 
} 



//join threads 
for (i=0; i<7; i++) 
{ 
    pthread_join(tid[i], NULL); 
} 

pthread_mutex_destroy(& a_mutex); 

// Exit main 
return 0; 
} 

void *text(void *arg) 
{ 
while(1){ 
    pthread_mutex_lock(&a_mutex); 
    if (num == *(int *) arg){ 
     printf("This is thread has the code %d\n",*(int *) arg); 
     num++; 
     pthread_mutex_unlock(&a_mutex); 
     pthread_exit(0); 
     } 
    pthread_mutex_unlock(&a_mutex); 
    } 
} 
0

@Anastasia Netz。您只需在代码的关键部分使用pthread_mutex_lock and pthread_mutex_unlock互斥锁,而不是仅在主线程中使用。请在您的代码中仔细确定您的关键部分(部分,其中重要的工作已完成并且对所有正在执行的线程都很常见,但其中只有一个可以访问),并在那里使用这些互斥锁。 现在让我来解释一下你的代码: 在你的代码中,你只需创建7个线程。在创建每个线程后,您只需锁定一次互斥锁,并无故解锁七次。因此,在所有七次迭代完成之后(for创建了这七个线程),您已经锁定了七次互斥锁,并且已经为(7 * 7 = 49)四十九次解锁了互斥锁。 这里是做这件工作的代码:

for (i = 0; i < 7; i++) { pthread_create(&tid[i], NULL, text, (void*)code[i]);

for (i = 0; i < 7; i++) 
{ 
rc = pthread_mutex_lock(&a_mutex); 

    for (i = 0; i < 7; i++) 
    { 
     rc = pthread_mutex_unlock(&a_mutex); 
    } 
}} 

请参阅Xvan的代码。

相关问题