I use threads in C as follows:
pthread_t thread;
if (pthread_create (& thread, NULL, thread_func (in, out), NULL)! = 0)
{
return -1;
}
// code goes further
So everything works, but the code after the announcement thread is executed only after the entire thread, but not immediately. How to make the thread started and along with him was the code further?
EDIT:
I meant that the code that I wrote after updating the flow begins to play only after the function thread_funts Completely work is over. I need to simultaneously work flow and the code after the update stream. For example:
static void * thread_func ()
{
int i;
for (i = 0; i!=40; i++)
{
__android_log_print(ANDROID_LOG_INFO, "SDL", "OK");
}
}
JNIEXPORT jint JNICALL Java_org_divenvrsk_android_hellondk_HelloNDK_work (JNIEnv * env, jobject obj, jbyteArray array)
{
pthread_t thread;
if (pthread_create (& thread, NULL, thread_func (), NULL)! = 0)
{
return -1;
}
__android_log_print(ANDROID_LOG_INFO, "SDL", "gggggggg");
}
I get this:
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
Here 40 times OK
06-11 20:01:20.955: INFO/SDL(5238): gggggggg
Ie, first performed the entire thread and then a further code, and I need to satisfy simultaneously, and thread and the code after the announcement thread.
Ie it is necessary that happened somewhere like this:
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.955: INFO/SDL(5238): gggggggg
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
06-11 20:01:20.951: INFO/SDL(5238): OK
Here 40 times OK
So you’re saying you’re starting a thread and that thread runs till completion before you’re ready for it to, and you want to better control that? Look into thread synchronization — tools such as semaphores and mutexes (which the pthreads library can help you with). Those are some of the tools used to allow threads to inter-operate in a more controlled manner.