Skip to content

std::this_thread::sleep_for(std::chrono::milliseconds) #788

Description

@srikanthkampati

Is it mandatory to call sleep in the thread after calling a callback function?

Activity

  1. devsnek commented on Aug 8, 2020

    @devsnek
    Member

    nope, it's just there as an example of a task that blocks while the main thread continues to run

  2. srikanthkampati commented on Aug 9, 2020

    @srikanthkampati
    Author

    I tired with thread safe function example with my native code in it and I didn't give any sleep in the code when I am running the code in simply exiting without any execution.
    But when i give some sleep after calling the call back for some time it executing and stoped,
    // Create a ThreadSafeFunction
    tsfn = ThreadSafeFunction::New(
    env,
    info[0].As(), // JavaScript function called asynchronously
    "Resource Name", // Name
    0, // Unlimited queue
    1, // Only one thread will use this initially
    { // Finalizer used to clean threads up
    nativeThread.join();
    });

    // Create a native thread
    nativeThread = std::thread([async_stream_data_ex] {
    	auto callback = [](Napi::Env env, Function jsCallback, BaseMessageData* BMD) {
    			
    			Object local_obj = Object::New(env);
    			/*Object js_obj = Object::New(env);
    			BMDToJSObj(env, local_obj, BMD);*/
    			
    			BMDToJSObj(env, local_obj, BMD);
    			Object js_obj = Object::New(env);
    			js_obj.Set("Message", local_obj);
    		// Transform native data into JS data, passing it to the provided 
    		// `jsCallback` -- the TSFN's JavaScript function.
    		jsCallback.Call({ js_obj });
    		
    	};
    		StreamReadStatus srs;
    		async_stream_data_ex->pAdvDecoder->EnableUnknownData(TRUE);
    		do {
    				BaseMessageData *BMD = NULL;
    				UINT msgid;
    				srs = async_stream_data_ex->pAdvDecoder->ReadMessage(&BMD);
    
    				if (srs.bEOS == false)
    				{
    					if (tsfn.Acquire() == napi_ok)
    					{
    
    						if (BMD->IsAdvancedMessage() == TRUE)
    						{
    							napi_status status = tsfn.BlockingCall(BMD, callback);
    							tsfn.Release();
    						}
    						else
    						{
    							napi_status status = tsfn.BlockingCall(BMD, callback);
    							tsfn.Release();
    						}
    					}
    				}
    				delete BMD;
    				BMD = NULL;
    		} while (srs.bEOS == false);
    		
    	// Release the thread-safe function
    	//tsfn.Release();
    });
    
  3. github-actions commented on Dec 15, 2020

    @github-actions
    Contributor

    This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.

  4. mhdawson commented on Dec 15, 2020

    @mhdawson
    Member

    @srikanthkampati you might want to try recreating with the latest to see if #853 fixed it and if not and seeing if nodejs/node#36510 is related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions