Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8703657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:55:02+00:00 2026-06-13T02:55:02+00:00

AFAIK TChan acts as a hub, every message sent is seen by others right

  • 0

AFAIK TChan acts as a hub, every message sent is seen by others right ?!
i want a TChan that acts as a switch to send a message to specific thread, and also support broadcasting.
is there such thing ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:55:03+00:00Added an answer on June 13, 2026 at 2:55 am

    Edit: I re-read your question. This answer doesn’t quite address “selective send”, though it clarifies what a TChan can do.

    The “broadcast” approach described below will wake up all listeners (though on the bright side, it won’t make 1000 copies of each item). To avoid this, use the Map approach as @Mikhail suggested. I did this in my chat server example.


    A TChan is a FIFO queue:

    • writeTChan adds an item to the end.

    • readTChan reads an item from the beginning.

    For example, the following example forks 10 threads which fight over a single channel:

    import Control.Concurrent
    import Control.Concurrent.STM
    import Control.Monad
    
    main = do
        chan <- newTChanIO
    
        forM_ [1..10] $ \i ->
            forkIO $
                forever $ do
                    x <- atomically $ readTChan chan
                    putStrLn $ "Thread " ++ show i ++ ": " ++ show x
    
        mapM_ (atomically . writeTChan chan) [1..1000]
    
        -- Wait for channel to empty out
        atomically $ do
            empty <- isEmptyTChan chan
            when (not empty) retry
    

    Here, each item is read by exactly one thread.

    In contrast, the following example “broadcasts” a stream of items to 10 threads, by making ten copies of the channel using dupTChan:

    import Control.Concurrent
    import Control.Concurrent.STM
    import Control.Monad
    
    main = do
        master <- newTChanIO
    
        forM_ [1..10] $ \i -> do
            chan <- atomically $ dupTChan master
            forkIO $
                forever $ do
                    x <- atomically $ readTChan chan
                    putStrLn $ "Thread " ++ show i ++ ": " ++ show x
    
        mapM_ (atomically . writeTChan master) [1..100]
    
        -- Give threads time to complete
        threadDelay 1000000
    

    Now each thread gets all of the items written to the channel.

    A couple subtleties to note:

    • Items written to a channel prior to dupTChan will not appear in the new channel. If we called dupTChan from the child threads rather than the main thread, some writeTChans could happen first, meaning the children might not see all the items.

    • Since nobody is reading the master channel, items written to it will pile up and will likely not be garbage collected. To avoid this caveat, use newBroadcastTChan to create the master channel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

AFAIK Android decompilation allows one to extract every bit of information from an apk.
I've seen a couple of questions over the last few days that have used
I'm having an odd issue with an assembly that is targeting .NET 3.5. When
I'm looking for a scripting language that works on 32-bit and 64-bit machines as
AFAIK, there are two ways of passing data to the view from controller :
I have a strange problem. Afaik I can inject a SessionScoped bean into a
I am looking creating some custom controls that need to be compiled as a
As described in another message of mine , it is not possible to compare
I'm expecting that the load on the software I'm developing will increase over the
I have a Python extension to the Nautilus file browser (AFAIK this runs exclusively

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.