Skip to content
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

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

Closed
srikanthkampati opened this issue Aug 8, 2020 · 4 comments
Closed

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

srikanthkampati opened this issue Aug 8, 2020 · 4 comments
Labels

Comments

@srikanthkampati
Copy link

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

@devsnek
Copy link
Member

devsnek commented Aug 8, 2020

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

@srikanthkampati
Copy link
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();
});

@github-actions
Copy link
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.

@github-actions github-actions bot added the stale label Dec 15, 2020
@mhdawson
Copy link
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
Labels
Projects
None yet
Development

No branches or pull requests

3 participants