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 8873381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:27:03+00:00 2026-06-14T18:27:03+00:00

basically I got two threads running.. one broadcasts music via a network (let’s call

  • 0

basically I got two threads running.. one broadcasts music via a network (let’s call it broadcaster).. the other communicates with clients to know when to start broadcasting, play music etc.. (runs on main)

The broadcasting thread keeps on running even while the server is talking to the client, there is only one part when I want to pause the broadcaster thread, and that’s when I want to send a packet to the client to start playing music (ie I want both server and client to play in sync)..

I’ve used pretty much everything (ie NSLocks, pthread_mutex_t and pthread_cond_t, thread sleep etc).. but I keep on running on this scenario:

basically I succeed in locking out the broadcaster thread.. but it always creeps in a command that seems to have a different clock setting

notice the logs:

12:02:59.288 Snap[30700:7a0f] broadcaster--> sending packet number 69 to all peers
12:02:59.294 Snap[30700:7a0f] broadcaster--> going through packets loop
12:02:59.306 Snap[30700:707] communicator--> SERVER: WILL LOCK BROADCASTING
12:02:59.312 Snap[30700:707] communicator--> we are inside serverReceivedPacket
12:02:59.314 Snap[30700:707] communicator--> SERVER: JUST RECEIVED A PRIMED PACKET!
12:02:59.316 Snap[30700:707]communicator-->  we are inside allPlayersArePrimed and we got 2 players
12:02:59.318 Snap[30700:707] communicator--> CLIENT: players are primed!



12:02:59.320 Snap[30700:707] communicator--> all players are primed now!.. pausing broadcast
12:02:59.322 Snap[30700:707] communicator--> will fire music player
12:02:59.311 Snap[30700:7a0f] broadcaster--> sending packet number 70 to all peers
12:02:59.335 Snap[30700:707] communicator--> SERVER: about to play [UNLOCK]
12:02:59.452 Snap[30700:7a0f] broadcaster--> going through packets loop
12:02:59.454 Snap[30700:7a0f] broadcaster--> sending packet number 71 to all peers

my problem is specifically with this line:

12:02:59.311 Snap[30700:7a0f] broadcaster--> sending packet number 70 to all peers

if you follow the broadcaster thread logs.. you’ll see that its last log statement stopped at 12:02:59.288, then it was locked out.. but before the communicator unlocked the lock.. the broadcaster threw in a command at 12:02:59.311.. although it came after a communicator command that was later in time!

I’ve seen this scenario repeat time and time again.. and I don’t know how to address it?

here is some of the code:

instance variable shared with both communicator and broadcaster:

@implementation Game
{
    ... 

    NSLock *broadcastLock;

}

communicator:

    case PacketTypeClientPrimed:
    {

        player.isPrimed = true;  
        if (_state == GameStateWaitingForPrimed && [self allPlayersArePrimed])
        {
            [Logger Log:@"SERVER: WILL LOCK BROADCASTING"];
            broadcastLock = [[NSLock alloc] init];
            [broadcastLock lock];

            _broadCastState = BroadCastStatePaused;
            [hostViewController.musicPlayer skipToBeginning];

            Packet *packet = [Packet packetWithType:PacketTypePlayMusicNow];                

            NSError *error;
            NSLog(@"all players are primed now!.. pausing broadcast ");
            [_session sendDataToAllPeers:[packet data] withDataMode:GKSendDataUnreliable error:&error];
            NSLog(@"will fire music player");

            [self performSelector:@selector(startMusic:) withObject:hostViewController.musicPlayer afterDelay:0.01];
            _state =  GameStatePlayBackCommenced;
        }
    }

....

-(void)startMusic:(MPMusicPlayerController *)player 
{
    NSLog(@"SERVER: about to play [UNLOCK]");
    [player play];
    _broadCastState = BroadCastStateInProgress;
    [broadcastLock unlock];
}

broadcaster:

-(void)broadcastSample
{
    [broadcastLock lock];

        CMSampleBufferRef sample;
        sample = [readerOutputcopyNextSampleBuffer];            
        ....                        

        for (int i = 0; i < inNumberPackets; ++i)
        {
            // ..
            // setup audio packet
            // ..
            if ((packetSpaceRemaining < (dataSize + AUDIO_STREAM_PACK_DESC_SIZE)) || 
                (packetDescrSpaceRemaining < AUDIO_STREAM_PACK_DESC_SIZE))
            {
                if (![self encapsulateAndShipPacket:packet packetDescriptions:packetDescriptions packetID:assetOnAirID])
                    break;
            }                        
        }

    [broadcastLock unlock];
}
  • 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-14T18:27:04+00:00Added an answer on June 14, 2026 at 6:27 pm

    Why are you allocating the lock object dynamically? When the broadcaster thread goes through there’s no lock object, so it doesn’t lock it. Then the lock is allocated and locked in the communicator thread (which can’t tell the broadcaster has already entered its critical region), and the two run in parallel.

    You should allocate the lock object globally, and it should persist from before the time either thread might attempt to enter its critical region.

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

Sidebar

Related Questions

I got two different lists (let's call them A and B) of lists and
Basically, I got one activity has two layouts, one is gridview, which has mBaseAdapter
I have two classes, basically one holds Members and the other Sessions. They are
I've got basically two arrays of objects in one view: App.List = Ember.View.extend({ students:
Let's say I've got two tables: one with customer data, one with location data.
I got two gridviews in the same page, which basically displays the same type
I've got a conversion utility that basically copies values from one table to another.
im trying to make a simple game on cocos2d box2d, its basically got two
here's what i have got two box one win 2003 server IIS6 another one
Basically I've got this current url and this other key that I want to

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.