| |

VerySource

 Forgot password?
 Register
Search
View: 1279|Reply: 9

The problem of multi-threaded access to global variables is too difficult

[Copy link]

3

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-12-14 13:30:01
| Show all posts |Read mode
I have a Tqueue queue,
One thread keeps adding data, and the other thread keeps reading and deleting. This way, address errors often occur from time to time, how to solve it. I have used critical sections, but I am not good at critical sections.
  It seems that the critical section is only valid for multi-threaded access to the same piece of code. What should I do if global variables are used everywhere?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-12-15 11:15:02
| Show all posts
Try syncing
Reply

Use magic Report

2

Threads

16

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-12-15 14:45:01
| Show all posts
Thread lock
Reply

Use magic Report

0

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-12-17 17:45:01
| Show all posts
The critical section is used to deal with this problem
Reply

Use magic Report

3

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-12-19 00:30:01
| Show all posts
1 Critical section: access to a common piece of code through the serialization of multiple threads
My code is not common, each thread accesses different places, shouldn't it be a critical section?
Reply

Use magic Report

0

Threads

9

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-12-19 09:30:01
| Show all posts
You should be useless!
Critical section is a way to achieve synchronization!
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 Invalid IP Address

Post time: 2020-12-20 14:00:01
| Show all posts
Try TThreadList
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 India

Post time: 2020-12-20 19:15:01
| Show all posts
TThreadList seriously affects performance! When the processing speed is required to be very fast, TThreadList is not competent!
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-12-20 19:30:01
| Show all posts
Try with semaphore
Reply

Use magic Report

0

Threads

6

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-12-20 22:45:01
| Show all posts
Use circular queues. The queues are implemented by static arrays. Don't use dynamic memory allocation. This speed will increase a lot and there will be no memory allocation errors.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list