Skip to main content
 首页 » 编程设计

c之输出值与两个 pthread 交错

2026年05月17日20mfrbuaa

我想创建两个输出交错的线程,如下所示

 Thread1:1=>Ping! 
 Thread2:2=>Pong! 
 Thread1:3=>Ping! 
 Thread1:4=>Ping! 
 Thread2:5=>Pong! 
 Thread2:6=>Pong! 
 Thread1:7=>Ping! 
 Thread2:8=>Pong! 
 Thread1:9=>Ping! 
 .......... 
 until 50 

我的代码在下面

#include <stdio.h> 
#include <stdlib.h>  
#include <pthread.h> 
#include <semaphore.h> 
#include <unistd.h> 
void* increment1(void* arg); 
void* increment2(void* arg); 
int count = 0; 
sem_t sem; 
int main() { 
    //variable initialize 
    pthread_t thread1, thread2; 
    int res1 = 0, res2 = 0; 
    int number = 0; 
    int i = 0; 
    //create semaphore 
    if (sem_init(&sem, 0, 1) == -1){ 
        printf("Semaphore creation failed!!\n"); 
        exit(EXIT_FAILURE);      
    } 
    for (i = 0; i < 25; ++i){ 
        //create thread 
        res1 = pthread_create(&thread1, NULL, increment1, NULL); 
        if (res1 != 0) { 
            printf("Thread1 creation failed!!\n"); 
            exit(EXIT_FAILURE); 
        } 
        //wait thread synchronization 
        pthread_join( thread1, NULL); 
        res2 = pthread_create(&thread2, NULL, increment2, NULL); 
        if (res2 != 0) { 
            printf("Thread2 creation failed!!\n"); 
            exit(EXIT_FAILURE); 
        } 
        //wait thread synchronization 
        pthread_join( thread2, NULL); 
    } 
        exit(EXIT_SUCCESS); 
} 
void* increment1(void* arg) {  
    sem_wait(&sem); 
    count ++; 
    printf("Thread1:%d\nPing!\n",count); 
    fsync(fileno(stdout)); 
    sem_post(&sem); 
} 
void* increment2(void* arg) {  
    sem_wait(&sem); 
    count ++; 
    printf("Thread2:%d\nPong!\n",count); 
    fsync(fileno(stdout)); 
    sem_post(&sem); 
} 

但我认为我做的不是并行使用两个线程是错误的,我使用的是顺序替代执行两个线程而不是不并行。(通过使用 pthread_join,thread2 将在 thread1 之后执行完成)。

我尝试使用信号量,它似乎不能保证线程执行顺序。

我想问的是

1.如何使用信号量保证两个线程的顺序?

2.如何暂停和恢复线程?我认为我所做的是在循环中创建新的两个 pthread。

提前致谢。

请您参考如下方法:

添加第二个信号量并将其初始化为零,以便线程 1 被强制为第一个。然后 increment1 和 increment2 保持信号,它是其他线程转去。你在连接的地方遇到了一些小问题,你可以弄清楚。

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 
#include <semaphore.h> 
#include <unistd.h> 
 
void* increment1(void* arg); 
void* increment2(void* arg); 
int count = 0; 
sem_t sem1, sem2; 
 
int main() 
{ 
    pthread_t thread[2]; 
 
    int res = 0; 
    int number = 0; 
    int i = 0; 
 
    if (sem_init(&sem1, 0, 1) == -1) 
    { 
        printf("Semaphore creation failed!!\n"); 
        exit(EXIT_FAILURE); 
    } 
 
    if (sem_init(&sem2, 0, 0) == -1) 
    { 
        printf("Semaphore creation failed!!\n"); 
        exit(EXIT_FAILURE); 
    } 
 
    for (i = 0; i < 25; ++i) 
    { 
         res = pthread_create(&thread[0], NULL, increment1, NULL); 
         if (res != 0) 
         { 
             printf("Thread creation failed!!\n"); 
             exit(EXIT_FAILURE); 
         } 
 
         res = pthread_create(&thread[1], NULL, increment2, NULL); 
         if (res != 0) 
         { 
             printf("Thread creation failed!!\n"); 
             exit(EXIT_FAILURE); 
         } 
 
        for (int j = 0; j < 2; ++j) 
        { 
            pthread_join(thread[j], NULL); 
        } 
    } 
 
    exit(EXIT_SUCCESS); 
} 
 
void* increment1(void* arg) 
{ 
    sem_wait(&sem1); 
    count ++; 
    printf("Thread1:%d Ping!\n", count); 
    fsync(fileno(stdout)); 
    sem_post(&sem2); 
} 
 
void* increment2(void* arg) 
{ 
    sem_wait(&sem2); 
    count ++; 
    printf("Thread2:%d Pong!\n", count); 
    fsync(fileno(stdout)); 
    sem_post(&sem1); 
}