【发布时间】:2013-11-24 18:37:01
【问题描述】:
我想创建两个输出交错的线程,如下所示
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。
提前致谢。
【问题讨论】:
-
只需创建两个线程一次。也许使用互斥量而不是信号量(信号量也可以像互斥量一样使用)。或者使用邮箱/队列在线程之间发送数据。
-
每个线程都应该有自己的功能。线程阻塞在互斥锁或队列上...
-
但是我如何保证第一次执行会被 ping 通?
-
先创建它并给它时间先获取互斥锁...
标签: c linux multithreading pthreads posix