【发布时间】:2018-01-03 06:54:29
【问题描述】:
我正在编写一个简单的程序来演示信号量的使用。 (后来测试自定义编写的信号量是否有效)。
我有 4 个线程同时运行一个函数。每个函数等待一段随机的时间,然后打印:Hello, world! This is thread n I slept for uSuS。
如您所料,消息以随机顺序打印到标准输出。这表明线程是并发运行的,因为如果它们按顺序执行,它们将按顺序出现。
我想在此演示中使用信号量来强制执行命令。但是,它目前不起作用。
这是我的代码:
sem = sem_open("mutex" , O_CREAT | O_RDWR , S_IRWXU | S_IRWXG | S_IRWXO, 1); // name, oflag, mode, initial value
Poco::Thread thread[5];
class HelloRunnable: public Poco::Runnable
{
public:
HelloRunnable(int arg) //constructor for the runnable
{
n = arg;
}
int n;
virtual void run() //entry point for the threads
{
sem_wait(sem); //the semaphore
timeval t;
gettimeofday(&t, NULL);
srand(t.tv_usec * t.tv_sec);
int uS = rand()%100000;
usleep(uS); //sleep for random length of time
std::cout << "Hello, world! This is thread " << n << " I slept for "<< uS << "uS" <<std::endl;
sem_post(sem);
return;
}
};
int main()
{
HelloRunnable runnable1(1); //construct a runnable with arg = 1
thread[1].start(runnable1); //execute that runnable
HelloRunnable runnable2(2); //construct a runnable with arg = 2
thread[2].start(runnable2); //execute that runnable
HelloRunnable runnable3(3); //...
thread[3].start(runnable3);
HelloRunnable runnable4(4);
thread[4].start(runnable4);
//wait for all threads to finish
thread[1].join();
thread[2].join();
thread[3].join();
thread[4].join();
return 0;
}
但是,线程仍然以随机顺序将消息打印到标准输出。例如:
//Hello, world! This is thread 2 I slept for 15001uS
//Hello, world! This is thread 1 I slept for 51124uS
//Hello, world! This is thread 4 I slept for 60884uS
//Hello, world! This is thread 3 I slept for 86137uS
我应该在代码中的什么位置放置信号量,以确保消息按顺序打印?抱歉,如果这很简单。我不是编码背景。
编辑
我将sem_wait 移到了usleep 之前。现在它工作得更好,但不是所有的时间。它按大约 %45 的时间顺序打印,大约 45% 的时间以相反的顺序打印,大约 %10 的时间以随机顺序打印。这是为什么!?
【问题讨论】:
标签: c++ multithreading semaphore poco-libraries