multithreading - Synchronizing threads with conditional variables in C -


I have one application, which contains some threaded stuff, which I have difficulty implementing i pthread.h () As a guideline.

I need to synchronize two threads, which repeat the predetermined number of a certain task, and with each repetition it is necessary to start two tasks on it. My idea is that starting and syncing each thread should work before the work begins and when this thread is one (we call this thread) will prompt Thread 2 (Rx) that it starts to work Can do.

Here's an example: Fixed pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; Static pthread_cond_t tx_condvar = PTHREAD_COND_INITIALIZER; Constant force tx_ready = false;

These are declared in the header file. TX thread is shown below:

  while (reps & gt; 0); pthread_mutex_lock (& ​​amp; mutex;); Tx_ready = True; Pthread_cond_signal (& amp; tx_condvar); pthread_mutex_unlock (& ​​amp; mutex); Position = do_stuff (); If (status! = 0) {print_error (); Goto tx_task_out; } Representative -; // Do one thing, wait for the next time to sleep (delay _value_US); Tx_ready = false; }   

and then rx <(paused; function & gt; left) {// tx_ready wait for the signal Move to Pthread_mutex_lock (& ​​amp; mutex); While (! Tx_ready) {pthread_cond_wait (& amp; tx_condvar, & amp; mutex); } Pthread_mutex_unlock (and mutex); Position = accessories (); If (status! = 0) {print_error (); Goto rx_task_out; } N = fwrite (samples, 2 * size (samples [0]), to_rx, p- & gt; out_file); num_rx + = to_rx; If (num_rx == s-> rx_length) {done = true; }}

Is there a better way to handle this, and am I doing it correctly? It is incredibly important that for each recurrence two tasks of Tx / Rx thread are started at the same time.

Thank you in advance for your input!

What you are looking for is called a. Actually, it blocks the threads until a certain number of threads are entered and then it releases all.

I believe pthreads are an obstacle, although it can be an extension.



Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -