开发者

The problem with threads in C

开发者 https://www.devze.com 2023-03-12 18:38 出处:网络
I use threads in C as follows: pthread_t thread; if (pthread_create (& thread, NULL, thread_func (in, out), NULL)! = 0)

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:开发者_JAVA技巧 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.


It is already started. What makes you think, that it isn't? It's totally asynchronous.


This would not be unusual behavior on an Android class device. You only get true concurrency when you run your code on a machine that has multiple cores. Which is not so likely on an Android device, especially when you run your code on an emulator. Concurrency is simulated by switching context periodically, letting the cpu execute the code for a thread for a while. These context switches don't happen very frequently. Certainly less than once every 4 milliseconds.

Furthermore, the code suffers from the Heisenberg problem. The way you observe the threads is affecting the way they run. The kind of logging function you use has a lock to ensure that logging output doesn't get intermingled. A very likely scenario, especially since you don't do any real work in the threads, is that the operating system scheduler induces a thread context switch while the lock is held. The other thread is trying to acquire the lock, that won't work until the first thread is releasing it. Which it rarely does.

You won't observe concurrency until you let the threads live longer, performing more work. And won't hold a lock for a very long time that other threads try to acquire.

0

精彩评论

暂无评论...
验证码 换一张
取 消