Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Semaphore

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use Semaphore". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

Every full-fledged development tool comes with a number of examples that illustrate the use of the tool itself and related languages. Programmers tend to ignore these examples and go straight to their development goals. Basically, this is not a big problem, because most of the examples are very simple, and you can just look at them when you use them. But the example of Semaphore in Qt should not be ignored anyway.

code illustrates

global variables

//Amount of data

const int DataSize = 100000;

//buffer size

const int BufferSize = 8192;

//buffer

char buffer[BufferSize];

//buffer free space Semaphore

QSemaphore freeBytes(BufferSize);

//buffer data Semaphore

QSemaphore usedBytes;

write data thread

class Producer : public QThread

{

public:

void run() override

{

for (int i = 0; i < DataSize; ++i) {

freeBytes.acquire();

buffer[i % BufferSize]

= "ACGT"[(int)qrand() % 4];

usedBytes.release();

}

}

};

Skip the two Semaphore to read the program first. The run method writes data to the buffer. Since the index of buffer is i % BufferSize, this is a circular queue.

Let's look at the two semaphores. FreeBytes is a Semaphore associated with an empty domain. As long as there is free space in the circular queue, acquire will pass smoothly, otherwise the write data thread will block at the acquire method. When the data is written successfully, call the release method of usedBytes to increment the count value of usedBytes.

read data thread

class Consumer : public QThread

{

public:

void run() override

{

for (int i = 0; i < DataSize; ++i) {

usedBytes.acquire();

fprintf(stderr, "%c",

buffer[i % BufferSize]);

freeBytes.release();

}

}

};

The run method reads data from a circular queue. The key is the use of two Semaphore. usedBytes is the Semaphore associated with the data domain. As long as there is data in the circular queue, acquire will pass smoothly, otherwise the read data thread will block at the acquire method. When the data is read successfully, call the release method of freeBytes to increment the count value of freeBytes.

main program

int main(int argc, char *argv[])

{

Producer producer;

Consumer consumer;

producer.start();

consumer.start();

producer.wait();

consumer.wait();

return 0;

}

The main program is very simple, start two threads separately, and then quietly wait for the two threads to end.

"How to use Semaphore" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report