Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Threading: Mechanisms for multi-threaded communication, which can refer to Go's chan or Node.js's postMessage. #2952

Closed
Tracked by #2188
winlinvip opened this issue Mar 5, 2022 · 0 comments
Labels
EnglishNative This issue is conveyed exclusively in English. Feature It's a new feature.

Comments

@winlinvip
Copy link
Member

winlinvip commented Mar 5, 2022

Multi-threaded communication is a series of mechanisms:

  1. First is the efficient communication queue.
  2. Then there is the mechanism for communication between threads.

Mutex vs CAS Int

We only consider the comparison between locks and CAS, that is, the case of int increment.

  • trunk/research/lockless/mutex-int.cpp: Use Mutex, int increment.
  • trunk/research/lockless/lockless-int.cpp: Use CAS, int increment.

First, look at single-threaded, so that waiting and conflict can be eliminated, data is as follows:

Number of threads Synchronization Number of loops Actual number of loops Time consumed
Single thread None 300M 300M 646ms
Single thread Mutex 300M 300M 5373ms
Single thread CAS 300M 300M 1911ms

Then, it's double-threaded, at this time Mutex or CAS must be used, no synchronization will cause data abnormalities and can only be used as a reference, data is as follows:

Number of threads Synchronization Number of loops Actual number of loops Time consumed
2 threads None 60M 65M 468ms
2 threads Mutex 60M 120M 5009ms
2 threads CAS 60M 120M 3295ms

Note: The actual number of loops should be 120M, because there are 2 threads, each with 60M.

Next, test 3 threads, data is as follows:

Number of threads Synchronization Number of loops Actual number of loops Time consumed
3 threads None 50M 60M 677ms
3 threads Mutex 50M 150M 7909ms
3 threads CAS 50M 150M 4317ms

Mutex vs CAS Queue

Consider the circular queue.

  • trunk/research/lockless/mutex-queue.cpp: Use Mutex.
  • trunk/research/lockless/lockless-queue.cpp: Use CAS.

Data is as follows:

Write thread count Read thread count Synchronization Number of loops Time consumed
1 thread 1 thread Mutex 30M 2872ms
1 thread 1 thread CAS 30M 3254ms
2 threads 2 threads Mutex 30M 6233ms
2 threads 2 threads CAS 30M 10387ms
3 threads 3 threads Mutex
@winlinvip winlinvip added the Feature It's a new feature. label Mar 7, 2022
@winlinvip winlinvip linked a pull request Mar 12, 2022 that will close this issue
@winlinvip winlinvip changed the title Threading: 多线程通信的机制,可以参考Go的chan,或者Nodejs的postMessage Threading: Mechanisms for multi-threaded communication, which can refer to Go's chan or Node.js's postMessage. Jul 18, 2023
@ossrs ossrs locked and limited conversation to collaborators Jul 18, 2023
@winlinvip winlinvip converted this issue into discussion #3639 Jul 18, 2023
@winlinvip winlinvip added the EnglishNative This issue is conveyed exclusively in English. label Jul 29, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
EnglishNative This issue is conveyed exclusively in English. Feature It's a new feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant